Writing and reading from a TXT file to add information to buttons

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 5041 Views - Last Post: 21 September 2012 - 05:56 AM Rate Topic: -----

#1 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Writing and reading from a TXT file to add information to buttons

Posted 18 September 2012 - 03:06 PM

So The radio part of my project has a AxWindowsMediaPlayer Control and a FlowLayoutPanel on the form.
Within the FlowLayoutPanel I am adding new Buttons by clicking the add channel button. My problem is this. The buttons are blank, and I want to add the button txt, and url to navigate the mediaPlayer in a txt file that I can read for a current button, and write when creating a new button. This is as far as I got.

Imports System.IO
Public Class AddChannel
    Dim SW As StreamWriter
    Dim SR As StreamReader
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 1 To 1
            Dim btn As New Button
            AddHandler btn.Click, AddressOf ClickMe
            btn.Width = 80
            btn.Height = 25
            btn.Text = TextBox1.Text
            btn.Tag = i
            btn.Name = i
            btn.Name = "Button" & i
            TVForm.FlowLayoutPanel1.Controls.Add(btn)

            Me.Close()
        Next
    End Sub

    Private Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
        Dim btn As Button
        btn = CType(Sender, Button)
        Dim str As String = btn.Tag

    End Sub
End Class


Not to familiar with Reading and Writing information in txt files But I need these bits of info to load when I open the main form.

Private Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
	        Dim btn As Button
	        btn = CType(Sender, Button)
	        Dim str As String = btn.Tag
	    End Sub


On this Click event it should also get that url info from txt and navigate the MediaPlayer

This post has been edited by AdamSpeight2008: 18 September 2012 - 03:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Writing and reading from a TXT file to add information to buttons

#2 torind_2000  Icon User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 293
  • Joined: 22-August 11

Re: Writing and reading from a TXT file to add information to buttons

Posted 18 September 2012 - 07:33 PM

see if this is helpful.
Was This Post Helpful? 1
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Writing and reading from a TXT file to add information to buttons

Posted 18 September 2012 - 07:59 PM

So what's the error?
Was This Post Helpful? 1
  • +
  • -

#4 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 18 September 2012 - 09:14 PM

Hey Trev man, no error I just havent written any code for it yet on the read and write.... I don't know the streamreader/writer yet going to spend most of tomorrow trying to better understand it.
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Writing and reading from a TXT file to add information to buttons

Posted 18 September 2012 - 09:38 PM

No problem. When you give it a shot, let us know if you get hung up on anything, we can certainly help.
Was This Post Helpful? 0
  • +
  • -

#6 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 07:07 AM

View Posttrevster344, on 18 September 2012 - 09:38 PM, said:

No problem. When you give it a shot, let us know if you get hung up on anything, we can certainly help.


Sure do appreciate that Trev.
Was This Post Helpful? 0
  • +
  • -

#7 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 09:48 AM

Ok so I looked at the resources provided by Torind, but its a bit confusing to me. And I'm not sure this is what I need.

So I have My main form

RADIOForm Contains:

1 Button (To Open a form Titled AddChannel)
1 FlowLayoutPanel (To Contain new Channel Buttons)
1 AxWindowsMediaPlayer (To Play Streaming Media)

If the Button is clicked, it opens the (AddChannel) Form

AddChannel Contains:

TextBox1 (To Enter the new button Name)
TextBox2 (To Enter the Streaming Audio link)
1 Button (To Add this information for the new Button, & place the button in the RADIOForm.FlowLayoutPanel)

I need the Information entered into these textFeilds to be saved into a text document, that can be loaded later when loading program. I Imagine my StreamWriter code should be in my click event on the Button on the AddChannel Form, then updating somewhere in the main form to show the added button with the new Button.Text, and Navigate the AxWindowsMediaPlayer to the saved url from textline in the txt file. If the line starts with the Button.Tag Value = 1, than that line belongs to Button 1 in the FlowLayoutPanel.

I just dont understand The Streamwriter yet and I am not seeming to get it.

This post has been edited by KeedonKing: 19 September 2012 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#8 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 10:47 AM

Ok so I think I have the AddChannel bit sorted out, Now if I could just figure out how to load these settings into the main form on load. I need to load them to each button by their i Value This is what I have:

Imports System.IO
Public Class AddChannel
    Dim SW As StreamWriter
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 1 To 1
            Dim btn As New Button
            AddHandler btn.Click, AddressOf TVForm.ClickMe
            btn.Width = 80
            btn.Height = 25
            btn.Text = TextBox1.Text
            btn.Tag = i
            btn.Name = i
            btn.Name = "Button" & i
            TVForm.FlowLayoutPanel1.Controls.Add(btn)
            Using SW As StreamWriter = New StreamWriter("BtnSettings.txt", True)
                SW.WriteLine(i & " " & TextBox1.Text & " " & TextBox2.Text, i)
            End Using
        Next
        Me.Close()
    End Sub
End Class

This post has been edited by KeedonKing: 19 September 2012 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 12:04 PM

The streamreader as you probably already know will allow you to read in a line at a time from your file. But... What I think you're going to find is that i is never changing. It is 1 with each button click to add a button.

The for i = 1 to 1 is not needed in this event as the event is only processed one time for each button click.

If you are indeed wanting your buttons to have an incremented number upon creation you might look into a static variable that will hold it's value and allow you to increment it each time it's needed.

So to do it upon load, you could read the file with the streamreader in a loop that checks for the end of the stream and create your buttons with the information much like you're doing above. This code would probably reside inside the Form_Load event.

Forgot to mention, in your ClickMe event for the button (btn) you should be able to get what button is pressed by from the sender object that is passed.

This post has been edited by CharlieMay: 19 September 2012 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#10 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 12:26 PM

View PostCharlieMay, on 19 September 2012 - 12:04 PM, said:

The streamreader as you probably already know will allow you to read in a line at a time from your file. But... What I think you're going to find is that i is never changing. It is 1 with each button click to add a button.

The for i = 1 to 1 is not needed in this event as the event is only processed one time for each button click.

If you are indeed wanting your buttons to have an incremented number upon creation you might look into a static variable that will hold it's value and allow you to increment it each time it's needed.

So to do it upon load, you could read the file with the streamreader in a loop that checks for the end of the stream and create your buttons with the information much like you're doing above. This code would probably reside inside the Form_Load event.

Forgot to mention, in your ClickMe event for the button (btn) you should be able to get what button is pressed by from the sender object that is passed.


I did change the code to get rid of the i = 1 To 1 with this code:

Imports System.IO
Public Class AddChannel
    Dim SW As StreamWriter
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim btn As New Button
        AddHandler btn.Click, AddressOf TVForm.ClickMe
        btn.Name = TextBox1.Text
            btn.Width = 80
            btn.Height = 25
            btn.Text = TextBox1.Text
            TVForm.FlowLayoutPanel1.Controls.Add(btn)
            Using SW As StreamWriter = New StreamWriter("BtnSettings.txt", True)
            SW.WriteLine(TextBox1.Text)
            SW.WriteLine(TextBox2.Text)
            End Using
        Me.Close()
    End Sub
End Class


I have fix the Error in my reader code I am working on. As Shown in the attached photo.
Only issue now is getting the AxWindowsMediaPlayer to play the line following the one which was found, with the reader. Instead of Displaying the MessageBox
This is the Code:

Public Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
        Dim btn As Button
        btn = CType(Sender, Button)
        Using reader As StreamReader = New StreamReader("BtnSettings.txt")
            Dim line As String
            line = reader.ReadLine.StartsWith(btn.Name)
            Do While (Not line Is Nothing)
                line = reader.ReadLine.StartsWith(btn.Name)
                If line = reader.ReadLine.StartsWith(btn.Name) Then
                    MessageBox.Show("I'm Here")
                End If
                Exit Do
            Loop

 End Using

    End Sub

Attached image(s)

  • Attached Image

This post has been edited by KeedonKing: 19 September 2012 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 12:59 PM

Right off the bat, every time you use reader.ReadLine you're moving to the next line.

Set a break point and step through the routine to see
1. What does the variables hold and
2. to see how it is responding to your If statments and what is being executed from there.

Here's a little demo that might help

Using sr as new System.IO.StreamReader("BtnSettings.txt")
  While Not sr.EndOfStream
    MessageBox.Show(sr.ReadLine)
  End While
End Using


Look at what this produces and see if that helps.

One last thing. Why are you doing this in the btnClick event?

If I understand correctly, the user click to add a button with the channel. This is then stored to a textfile so that when you start the program all the created buttons are generated with the channels. If that's correct, then when you load the program, the Form_Load is triggered and this is where you use a loop to create all the buttons that are stored in your text file.

By storing the channel information in the Text or the Tag properties, you can then use the btnClick event to get the Senders property and process by setting the channel of the button click.

This post has been edited by CharlieMay: 19 September 2012 - 01:09 PM

Was This Post Helpful? 1
  • +
  • -

#12 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 02:15 PM

I still don't get what I'm doing wrong here:

Public Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
        Dim btn As Button
        btn = CType(Sender, Button)
        Using Streams As StreamReader = New StreamReader("BtnStreams.txt")
            Dim line As String
            line = Streams.ReadLine.Contains(btn.Name)
            Do While (Not line Is Nothing)
                line = Streams.ReadLine.Contains(btn.Name)
                If line = Streams.ReadLine.Contains(btn.Name) Then
                    TV.URL = (line = Streams.ReadLine.StartsWith("Http://"))
                End If
                Exit Do
            Loop
        End Using
    End Sub

Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 02:36 PM

Can you give use a screenshot of what your textfile looks like?
Also, instead of separating the elements of the line with " " maybe use a "~" so you can split that line and keep each element separate. Space may be used in your textboxes somewhere and will throw off the split.

But you're still putting this code in the wrong place. Where you have this code is after the button is created and its click event is attached to this area.
Was This Post Helpful? 1
  • +
  • -

#14 KeedonKing  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 12-July 12

Re: Writing and reading from a TXT file to add information to buttons

Posted 19 September 2012 - 03:25 PM

View PostCharlieMay, on 19 September 2012 - 02:36 PM, said:

Can you give use a screenshot of what your textfile looks like?
Also, instead of separating the elements of the line with " " maybe use a "~" so you can split that line and keep each element separate. Space may be used in your textboxes somewhere and will throw off the split.

But you're still putting this code in the wrong place. Where you have this code is after the button is created and its click event is attached to this area.


Ok so I am guessing I got this semi right here:
First I added a Button to the main form, called "Add A Channel" When clicked it opens My "AddChannel Form"

"AddChannel Forms" Code:

Public Class AddChannel
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim btn As New Button
        AddHandler btn.Click, AddressOf TVForm.ClickMe
        btn.Name = TextBox1.Text
            btn.Width = 80
            btn.Height = 25
        btn.Text = TextBox1.Text
            TVForm.FlowLayoutPanel1.Controls.Add(btn)
        Using SWNames As StreamWriter = New StreamWriter("BtnSettings.txt", True)
            SWNames.WriteLine(TextBox1.Text & "~" & TextBox2.Text)
        End Using
        btn = CType(sender, Button)
        Using Streams As StreamReader = New StreamReader("BtnSettings.txt")
            Dim line As String
            line = Streams.ReadLine.StartsWith(btn.Name)
            Do While (Not line Is Nothing)
                Streams.ReadLine.StartsWith(btn.Name)
                If Streams.ReadLine.StartsWith(btn.Name) Then

                   'Here I imagine I need to split the First Part of the Data from the text file to add the actual url to the tag property of my new button.
                 btn.Tag = Streams.ReadLine
                End If
                Exit Do
            Loop
        End Using
        Me.Close()
    End Sub
End Class


From here, it adds the New Btn to the FlowLayoutPanel. And Now the button is Titled (Whatever text was used in TextBox1)
And The New Btns Tag property Should Contain the url. I've attached an image of how my text file looks.

Ok, so now the button is in My flowlayoutPanel. I have this for my New Btns Click event:

Public Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
        btn = CType(Sender, Button)
        TV.URL = btn.Tag
    End Sub


I am trying to figure this out but if ya could explain to me with more detail I would appreciate it :) I suck at this but I am getting better haha

THis is how my textfile is saving:

SkyRadio~http://www.181.fm/asx.php?station=181-kickincountry&style=mp3&description=Kickin%27%20Country

This post has been edited by KeedonKing: 19 September 2012 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Writing and reading from a TXT file to add information to buttons

Posted 20 September 2012 - 08:30 AM

line = Streams.ReadLine.StartsWith(btn.Name) is a boolean check not a way to populate if correct. Others have asked you what the file structure looks like. If you don't at least supply a pseudo example that matches your exact format there is no way we can help guide you on reading the data.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2