[HIGHLIGHT]Public Class frmMain
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' fill the list box with values
lstParty.Items.Add("Democrats")
lstParty.Items.Add("Republicans")
lstParty.Items.Add("Independents")
End Sub
Private Sub txtAge_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAge.KeyPress
' allows the text box to accept only numbers, the period, and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
' cancel the key
e.Handled = True
End If
End Sub
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
' writes a political party to a sequential access file
' declare a StreamWriter variable
Dim outFile As IO.StreamWriter
Dim intnum As Integer
' open the file for append
outFile = IO.File.AppendText("Party.txt")
' write the name on a separate line in the file
outFile.WriteLine(lstParty.Items)
' Loop through the items in the listbox
For intnum = 0 To lstParty.Items.Count - 1
' write the name on a separate line in the file
outFile.WriteLine(lstParty.Items)
Next
' close the file
outFile.Close()
' set focus
txtAge.Focus()
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
' reads political party from a sequential access file
' and displays them in the interface
' declare variables
Dim inFile As IO.StreamReader
' determine whether the file exists
If IO.File.Exists("party.txt") Then
'open the file for input
inFile = IO.File.OpenText("party.txt")
inFile.Close()
Else
MessageBox.Show("Can't find the file", "Political Party", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub txtAge_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAge.Enter
txtAge.SelectAll()
End Sub
End Class