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
' open the file for append
outFile = IO.File.AppendText("Party.txt")
' write the name on a separate line in the file
outFile.Write(lstParty.Text)
outFile.Write(",")
outFile.Write(txtAge.Text)
outFile.WriteLine()
' 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
Dim strParty As String
Dim intDem As Integer
Dim intRep As Integer
Dim intInd As Integer
If IO.File.Exists("Party.txt") Then
'open the file for input
inFile = IO.File.OpenText("Party.txt")
' process loop instructions until the end of file
Do Until inFile.Peek = -1
' read a name
strParty = inFile.ReadLine
Select Case strParty
Case "Democrats"
intDem += 1
'lblDemocrats.Text = intDem.ToString
Case "Republicans"
intRep += 1
'lblRepublicans.Text = intRep.ToString
Case "Independents"
intInd += 1
'lblIndependents.Text = intInd.ToString
End Select
' add name to list box
lblDemocrats.Text = intDem.ToString
lblRepublicans.Text = intRep.ToString
lblIndependents.Text = intInd.ToString
Loop
' close the file
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