4 Replies - 845 Views - Last Post: 16 April 2008 - 03:22 AM Rate Topic: -----

#1 carlysarah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 03-April 08

Extremely stuck- reading and writing to text file

Post icon  Posted 15 April 2008 - 09:13 AM

I am creating a sort of radio interface...



At the moment i have 2 forms...

the first form on load is automatically set to power off, the user must hit the power button to put the power on.

when the power button is pressed, the radio should automatically start playing the last "station" it was playing before the power was switched off



the last station must be held in an external text file.



im not very sure how to go about this.



at the moment, i just have the radio playing an mp3 which is in the code of the first form.



the user must be able to change the "station" at any time, and if the power is switched off, this station (the last played) must be saved in the text file, and played again when the radio is switched on again.



This is the code for form1:




Private Sub btnPower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPower.Click

If Mode = ModeEnum.OffMode Then

Mode = ModeEnum.OnMode

MediaPlayer1.URL = "c:\cwmusic\ChrisBrown.mp3"

Else

Mode = ModeEnum.OffMode

MediaPlayer1.Ctlcontrols.stop()




End If

SetMode(Me, Mode)

End Sub



Private Mode As ModeEnum = ModeEnum.OffMode

Enum ModeEnum

OnMode

OffMode

End Enum

Sub SetMode(ByVal ctl As Control, ByVal mode As ModeEnum)

Dim blnEnabled As Boolean = (mode = ModeEnum.OnMode)

For Each subCtl As Control In ctl.Controls

If Not subCtl.Name = "btnPower" Then

subCtl.Enabled = blnEnabled

End If

If subCtl.HasChildren Then SetMode(subCtl, mode)

Next

End Sub





Private Sub TckbarVolume_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TckbarVolume.Scroll

MediaPlayer1.settings.volume = TckbarVolume.Value * 10

End Sub





Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SetMode(Me, ModeEnum.OffMode)

Dim fileName As String = "C:\lastStation.txt"

Dim textLine As String = String.Empty

Try

If System.IO.File.Exists("C:\lastStation.txt") Then

Dim reader As New System.IO.StreamReader(fileName)

Do While reader.Peek() <> -1

textLine += reader.ReadLine()

textLine += Environment.NewLine()

Loop

Else

textLine = "File not found!"

End If

Catch Ex As Exception

textLine = Ex.Message

End Try

lblStationInfo.Text = textLine

End Sub





Private Sub btnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScan.Click

MediaPlayer1.settings.mute = True

Dim oForm As New Form2

oForm.ShowDialog()

MediaPlayer1.settings.mute = False

End Sub



Private Sub btnMute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMute.Click

If MediaPlayer1.settings.mute Then

MediaPlayer1.settings.mute = False

Else

MediaPlayer1.settings.mute = True

End If

End Sub

Private Sub radiobtnAM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radiobtnAM.CheckedChanged

If radiobtnAM.Checked Then

Module1.AM = True

Else

Module1.AM = False

End If

End Sub





Private Sub radiobtnFM_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radiobtnFM.CheckedChanged

If radiobtnFM.Checked Then

Module1.AM = False

Else

Module1.AM = True

End If

End Sub



Private Sub Station1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Station1.Click

MediaPlayer1.URL = "c:\cwmusic\Ronan.mp3"

lblStationInfo.Text = "CFM 96.4 FM"

radiobtnFM.Checked = True

End Sub

Private Sub Station2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Station2.Click

MediaPlayer1.URL = "c:\cwmusic\Snowpatrol.mp3"

lblStationInfo.Text = "Garrison FM 98.5 MW"

radiobtnAM.Checked = True

End Sub

End Class






and this is the code for the second form:




Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

Timer1.Enabled = False

MediaPlayer1.URL = ""

Me.Close()

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



Dim fileName As String = "C:\yourLocation.txt"

Dim textLine As String = String.Empty

Try

If System.IO.File.Exists("C:\yourLocation.txt") Then

Dim reader As New System.IO.StreamReader(fileName)

Do While reader.Peek() <> -1

textLine += reader.ReadLine()

textLine += Environment.NewLine()

Loop

Else

textLine = "File not found!"

End If

Catch Ex As Exception

textLine = Ex.Message

End Try

lblStationInfo.Text = textLine

End Sub



Private Sub scrollbarStations_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles scrollbarStations.ValueChanged

Select Case scrollbarStations.Value

Case 1

If Module1.AM Then

lblStationInfo.Text = "First Radio 2345 MW"

MediaPlayer1.URL = "c:\cwmusic\Kylie.mp3"

Else

lblStationInfo.Text = "Testing Radio 2335 FM"

MediaPlayer1.URL = "c:\cwmusic\Robyn.mp3"

End If

Case 2

lblStationInfo.Text = "Second Radio 6654 MW"

MediaPlayer1.URL = "c:\cwmusic\Cascada.mp3"

Case 3

lblStationInfo.Text = "Third Radio 7563 MW"

MediaPlayer1.URL = "c:\cwmusic\Nickleback.mp3"



End Select

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

If scrollbarStations.Value < scrollbarStations.Maximum Then

scrollbarStations.Value = scrollbarStations.Value + 1

End If

End Sub

Private Sub radiobtnAM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radiobtnAM.CheckedChanged





If radiobtnAM.Checked Then

Module1.AM = True

Else

Module1.AM = False

End If





End Sub





Private Sub radiobtnFM_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radiobtnFM.CheckedChanged





If radiobtnFM.Checked Then

Module1.AM = False

Else

Module1.AM = True

End If





End Sub





Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

End Sub

End Class








really hope someone can help me here, i am really really stuck!

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Extremely stuck- reading and writing to text file

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Extremely stuck- reading and writing to text file

Posted 15 April 2008 - 11:59 AM

Hi again Carly!

Here is a function I wrote for you which will take in a string of the filename playing and write it to a file. Use this example for your program and feel free to change the location and name of the file you are writing to.

    ' Writes a path to a music file, passed to it, to file.
    Private Sub WriteTrack(ByVal musicfilename As String)

        Try
            ' Test if a music file name has been provided
            If Not String.IsNullOrEmpty(musicfilename) Then

                ' Create a streamwriter to write to a file called "c:\track.txt" and open it it in create mode
                ' which means it will be erased before writing. Feel free to change the name.
                Dim Writer As StreamWriter
                Writer = New StreamWriter(New FileStream("c:\track.txt", FileMode.Create))
                writer.WriteLine(musicfilename)
                writer.Close()

            End If
        Catch e As IOException
            ' If a file operation fails, we present the error to the user.
            MessageBox.Show("Error opening file: " + e.Message.ToString())

        End Try
    End Sub



Essentially the way it works is that we call the function WriteTrack and give it a path to the file that is playing like WriteTrack("c:\cwmusic\ChrisBrown.mp3") which will write that path to the file located at c:\track.txt. We open the FileStream in "Create" mode so it will create the file if it doesn't exist and it will also erase the file each time to record the new filename. That way at any one time there is only one song recorded in the file.

Now you just have to go about reading it. This will involve a similar method using a StreamReader in which you again will give it a FileStream and open the file, but then once open you will call a method called "ReadLine" to read the line back into your program. Use the example above as a template and put it in its own function. I will leave that part up to you.

Looks like the program is coming right along. Good for you. :)
Was This Post Helpful? 0
  • +
  • -

#3 carlysarah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 03-April 08

Re: Extremely stuck- reading and writing to text file

Posted 15 April 2008 - 12:54 PM

View PostMartyr2, on 15 Apr, 2008 - 11:59 AM, said:

Hi again Carly!

Here is a function I wrote for you which will take in a string of the filename playing and write it to a file. Use this example for your program and feel free to change the location and name of the file you are writing to.

    ' Writes a path to a music file, passed to it, to file.
    Private Sub WriteTrack(ByVal musicfilename As String)

        Try
            ' Test if a music file name has been provided
            If Not String.IsNullOrEmpty(musicfilename) Then

                ' Create a streamwriter to write to a file called "c:\track.txt" and open it it in create mode
                ' which means it will be erased before writing. Feel free to change the name.
                Dim Writer As StreamWriter
                Writer = New StreamWriter(New FileStream("c:\track.txt", FileMode.Create))
                writer.WriteLine(musicfilename)
                writer.Close()

            End If
        Catch e As IOException
            ' If a file operation fails, we present the error to the user.
            MessageBox.Show("Error opening file: " + e.Message.ToString())

        End Try
    End Sub



Essentially the way it works is that we call the function WriteTrack and give it a path to the file that is playing like WriteTrack("c:\cwmusic\ChrisBrown.mp3") which will write that path to the file located at c:\track.txt. We open the FileStream in "Create" mode so it will create the file if it doesn't exist and it will also erase the file each time to record the new filename. That way at any one time there is only one song recorded in the file.

Now you just have to go about reading it. This will involve a similar method using a StreamReader in which you again will give it a FileStream and open the file, but then once open you will call a method called "ReadLine" to read the line back into your program. Use the example above as a template and put it in its own function. I will leave that part up to you.

Looks like the program is coming right along. Good for you. :)


Thank you again for your help but my main problem was where i was placing the code. Because i have in if statement to determine whether the power is on or off and then events happening depending on this. So, can i just put the code within the IF statement or am i going to have to re-do the entire power event? :(

Im getting too confused with this project now, getting too stressed :(
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Extremely stuck- reading and writing to text file

Posted 15 April 2008 - 12:57 PM

I can tell you are stressed. You are making very easy things a bit hard. You call this new function whenever you load a new song. That is, whenever you set your media player (not when scanning, when actually playing). That way the media player is playing the same song that is in your text file. So when you turn off the power and turn it back on, it loads the song that was last playing.

:)

This post has been edited by Martyr2: 15 April 2008 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 carlysarah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 03-April 08

Re: Extremely stuck- reading and writing to text file

Posted 16 April 2008 - 03:22 AM

View PostMartyr2, on 15 Apr, 2008 - 12:57 PM, said:

I can tell you are stressed. You are making very easy things a bit hard. You call this new function whenever you load a new song. That is, whenever you set your media player (not when scanning, when actually playing). That way the media player is playing the same song that is in your text file. So when you turn off the power and turn it back on, it loads the song that was last playing.

:)


yeah,but doesnt this mean i'll have to change the code on the form load button and the power button as it automatically starts playing a certain track
im almost wanting to give up on this now :(
gonna end up in labour early with this stupid project! its just stressing and annoying me now :( starting to not enjoy it anymore!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1