Re: Sequential Access file
Edit your post. Highlight all of the code, click the VB button and then type vb.net when it asks you for a highlight tag. This will make the code easier for us to read.
Re: Sequential Access file
Sorry!!! Thank you for the tip!!!
Re: Sequential Access file
outFile.WriteLine(lstParty.Items)
Think about what this line does (ie. what lstParty.Items is!)
Re: Sequential Access file
Ok. I have updated it to:
' 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(intnum)
Next
Still not working...please provide some more guidance, please....
Re: Sequential Access file
What is it that you actually want to write to each line of the file? It certainly isn't the value of intnum, nor is it the whole collection of lstParty.Items. What do you reckon the happy medium might be? I am of course assuming that you made some effort to understand how a Listbox works before you decided to use it.
Re: Sequential Access file
The party box list should contain three items: Democratic, Republican and Independent. The application should calculate and display the number of voters in each political party. Currently in my text file, it's listing all of the political party:
Democrats
Republicans
Independents
Democrats
Republicans
Independents
Democrats
Republicans
Independents
Democrats
Republicans
Independents
How do I get the application to write to only what the user selects in the party list box? Please... Thanks in advance for your help.
1 Attachment(s)
Re: Sequential Access file
Can someone please review my streamreader code and please let me know what I need to do in order to tabulate the selected political party? I keep having "zero" in all of the labels. Please help. Thank you very much in advance!!!
vb.net Code:
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
Attachment 99305
Re: Sequential Access file
Quote:
outFile.Write(lstParty.Text)
Oh, come on. If you're not prepared to read the documentation why should anybody want to help you? Here is as good a tutorial on Listboxes as there is.