Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42329

Sequential Access file

$
0
0
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]

Viewing all articles
Browse latest Browse all 42329


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>