Results 1 to 9 of 9

Thread: Sequential Access file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    Sequential Access file

    Hi Everybody,

    I have been working on this assignment for quite a long time and now seeking help. I need to create a visual basic application where an organization needs to save each respondent's information (political party and age) to a sequential access file. The application also should calculate and display the number of voters in each political party (Democratic, Republican, and Independent).

    Please see below code. When I tried to run, I'm getting the "System.Windows.Forms.ListBox+ObjectCollection" in my text file. Can you please provide me guidance what I'm doing wrong? Many thanks for your help!!!!


    vb.net Code:
    1. [HIGHLIGHT]Public Class frmMain
    2.  
    3.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    4.         Me.Close()
    5.     End Sub
    6.     Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    7.         ' fill the list box with values
    8.  
    9.         lstParty.Items.Add("Democrats")
    10.         lstParty.Items.Add("Republicans")
    11.         lstParty.Items.Add("Independents")
    12.     End Sub
    13.     Private Sub txtAge_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAge.KeyPress
    14.         ' allows the text box to accept only numbers, the period, and the Backspace key
    15.  
    16.         If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
    17.             e.KeyChar <> ControlChars.Back Then
    18.             ' cancel the key
    19.             e.Handled = True
    20.         End If
    21.     End Sub
    22.     Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
    23.  
    24.         ' writes a political party to a sequential access file
    25.         ' declare a StreamWriter variable
    26.         Dim outFile As IO.StreamWriter
    27.         Dim intnum As Integer
    28.  
    29.         ' open the file for append
    30.         outFile = IO.File.AppendText("Party.txt")
    31.  
    32.         ' write the name on a separate line in the file
    33.         outFile.WriteLine(lstParty.Items)
    34.  
    35.         ' Loop through the items in the listbox
    36.         For intnum = 0 To lstParty.Items.Count - 1
    37.             ' write the name on a separate line in the file
    38.             outFile.WriteLine(lstParty.Items)
    39.  
    40.         Next
    41.  
    42.         ' close the file
    43.         outFile.Close()
    44.  
    45.         ' set focus
    46.         txtAge.Focus()
    47.     End Sub
    48.  
    49.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    50.         ' reads political party from a sequential access file
    51.         ' and displays them in the interface
    52.  
    53.         ' declare variables
    54.         Dim inFile As IO.StreamReader
    55.  
    56.         ' determine whether the file exists
    57.         If IO.File.Exists("party.txt") Then
    58.             'open the file for input
    59.             inFile = IO.File.OpenText("party.txt")
    60.             inFile.Close()
    61.         Else
    62.             MessageBox.Show("Can't find the file", "Political Party", MessageBoxButtons.OK, MessageBoxIcon.Information)
    63.         End If
    64.     End Sub
    65.     Private Sub txtAge_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAge.Enter
    66.         txtAge.SelectAll()
    67.     End Sub
    68. End Class
    [/HIGHLIGHT]
    Last edited by amarlv07; Apr 20th, 2013 at 06:01 PM.

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    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.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    Re: Sequential Access file

    Sorry!!! Thank you for the tip!!!

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Sequential Access file

    outFile.WriteLine(lstParty.Items)

    Think about what this line does (ie. what lstParty.Items is!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    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....

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    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:
    1. Public Class frmMain
    2.  
    3.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    4.         Me.Close()
    5.     End Sub
    6.     Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    7.         ' fill the list box with values
    8.  
    9.         lstParty.Items.Add("Democrats")
    10.         lstParty.Items.Add("Republicans")
    11.         lstParty.Items.Add("Independents")
    12.     End Sub
    13.     Private Sub txtAge_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAge.KeyPress
    14.         ' allows the text box to accept only numbers, the period, and the Backspace key
    15.  
    16.         If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
    17.             e.KeyChar <> ControlChars.Back Then
    18.             ' cancel the key
    19.             e.Handled = True
    20.         End If
    21.     End Sub
    22.     Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
    23.  
    24.         ' writes a political party to a sequential access file
    25.         ' declare a StreamWriter variable
    26.         Dim outFile As IO.StreamWriter
    27.  
    28.         ' open the file for append
    29.         outFile = IO.File.AppendText("Party.txt")
    30.  
    31.         ' write the name on a separate line in the file
    32.         outFile.Write(lstParty.Text)
    33.         outFile.Write(",")
    34.         outFile.Write(txtAge.Text)
    35.         outFile.WriteLine()
    36.  
    37.         ' close the file
    38.         outFile.Close()
    39.  
    40.         ' set focus
    41.         txtAge.Focus()
    42.     End Sub
    43.  
    44.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    45.         ' reads political party from a sequential access file
    46.         ' and displays them in the interface
    47.  
    48.         ' declare variables
    49.         Dim inFile As IO.StreamReader
    50.         Dim strParty As String
    51.         Dim intDem As Integer
    52.         Dim intRep As Integer
    53.         Dim intInd As Integer
    54.  
    55.         If IO.File.Exists("Party.txt") Then
    56.             'open the file for input
    57.             inFile = IO.File.OpenText("Party.txt")
    58.             ' process loop instructions until the end of file
    59.             Do Until inFile.Peek = -1
    60.                 ' read a name
    61.                 strParty = inFile.ReadLine
    62.                 Select Case strParty
    63.                     Case "Democrats"
    64.                         intDem += 1
    65.                         'lblDemocrats.Text = intDem.ToString
    66.                     Case "Republicans"
    67.                         intRep += 1
    68.                         'lblRepublicans.Text = intRep.ToString
    69.                     Case "Independents"
    70.                         intInd += 1
    71.                         'lblIndependents.Text = intInd.ToString
    72.                 End Select
    73.                 ' add name to list box
    74.                 lblDemocrats.Text = intDem.ToString
    75.                 lblRepublicans.Text = intRep.ToString
    76.                 lblIndependents.Text = intInd.ToString
    77.             Loop
    78.             ' close the file
    79.             inFile.Close()
    80.         Else
    81.             MessageBox.Show("Can't find the file", "Political Party", MessageBoxButtons.OK, MessageBoxIcon.Information)
    82.         End If
    83.     End Sub
    84.     Private Sub txtAge_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAge.Enter
    85.         txtAge.SelectAll()
    86.     End Sub
    87. End Class

    Name:  form.jpg
Views: 602
Size:  31.6 KB

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Sequential Access file

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width