extremely stuck- read from and write to text file

carly

New member
Joined
Apr 3, 2008
Messages
3
Programming Experience
Beginner
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:




VB.NET:
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:




VB.NET:
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
 
Back
Top