2012-04-27 13 views
0

私は、投票型プログラムのシーケンシャルアクセスファイルを使用するVisual Basicのプログラムを作成しています。ユーザーが保存投票ボタンをクリックすると、投票数が保存されます。私がしようとしていることは、実際の番号として投票数を表示することです。私のプログラムが今書かれているように、候補者の名前は投票が保存された回数だけ現れます。たとえば、perezが4回投票された場合、アクセスファイルでは4つの異なる行にperezが表示されます。投票した回数の実際の数をどのように表示するのですか。私はカウンタ変数を使うことを考えていますが、本当にそれをどのように実装するのかは本当にわかりません。これはこれまでのところです。あなたはそれをクリックし、ファイルたびにラジオボタンのテキストを書いているあなたのbtnVote_Click機能でvbのシーケンシャルアクセスファイル

Public Class Voter 

Dim file As IO.File 

Dim infile As IO.StreamReader 

Dim outfile As IO.StreamWriter 



Private Sub Voter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    outfile = IO.File.CreateText("Votes.txt") 
End Sub 

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click 
    lstResult.Items.Clear() 
    'which buttons is clicked 
    If radMark.Checked = True Then 
     outfile.WriteLine(radMark.Text) 
     radMark.Checked = False 
    ElseIf radSheima.Checked = True Then 
     outfile.WriteLine(radSheima.Text) 
     radSheima.Checked = False 
    ElseIf radSam.Checked = True Then 
     outfile.WriteLine(radSam.Text) 
     radSam.Checked = False 
    Else 
     MessageBox.Show("You should select one among them") 
    End If 
End Sub 

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click 
    'Dim Mark, Sheima, Sam As Integer 
    Dim Mark As Integer = 0 
    Dim Sheima As Integer = 0 
    Dim Sam As Integer = 0 
    Dim name As String 
    'Mark = Sheima = Sam = 0 
    outfile.Close() 
    infile = IO.File.OpenText("Votes.txt") 
    'keep track of votes 
    While Not infile.EndOfStream 
     name = infile.ReadLine() 
     If name.Equals("Mark Stone") Then 
      Mark += 1 
     ElseIf name.Equals("Sheima Patel") Then 
      Sheima += 1 
     Else 
      Sam += 1 
     End If 
    End While 
    'results 
    lstResult.Items.Clear() 
    lstResult.Items.Add("Mark Stone  " & CStr(Mark)) 
    lstResult.Items.Add("Shemia Patel " & CStr(Sheima)) 
    lstResult.Items.Add("Sam Perez  " & CStr(Sam)) 
    infile.Close() 
End Sub 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    Me.Close() 

End Sub 
End Class 

答えて

2

、それは問題を作成する方法を説明します。

また、ファイルに名前が何回表示されたのかを投票数としてカウントしますが、数値ではありません。

名前だけでなく、投票ファイルに名前を入れてカウントしてください。

マーク、1
Sheima、2
同じ、あなたが投票ボタンをクリックしたときに5

その後、あなたは1でマーク、増分の番号を読んで、それを書き戻す必要があります。

は大丈夫、それは私が把握しようとしています何

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click 
    lstResult.Items.Clear() 
    'which buttons is clicked 
    If radMark.Checked = True Then 
     AddCount(radMark.Text) 
     radMark.Checked = False 
    ElseIf radSheima.Checked = True Then 
     AddCount(radSheima.Text) 
     radSheima.Checked = False 
    ElseIf radSam.Checked = True Then 
     AddCount(radSam.Text) 
     radSam.Checked = False 
    Else 
     MessageBox.Show("You should select one among them") 
    End If 
End Sub 

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click 

    'results 
    lstResult.Items.Clear() 
    lstResult.Items.Add("Mark Stone  " & GetCount(radMark.Text)) 
    lstResult.Items.Add("Shemia Patel " & CStr(radSheima.Text)) 
    lstResult.Items.Add("Sam Perez  " & CStr(radSam.Text)) 

End Sub 

Private Sub AddCount(Byval VoteName As String) 

    infile = New IO.File.StreamReader("Votes.txt") 

    Dim whole_line As String 
    Dim person As String 
    Dim vote_count As Integer 
    Dim found as boolean 

    'read through the whole file until the end or find the name 
    Do While infile.Peek() >= 0 And person <> VoteName 

     'read each line 
     whole_line = infile.ReadLine 

     person = Split(whole_line, ",")(0) 

     If person = VoteName Then 

      vote_count = Split(whole_line, ",")(1) 


      found = True 
     End If 
    Loop 

    'Close the file after it is used. 
    infile.Close() 


    'Reopen the file with the StreamWriter 
    outfile = IO.File.OpenText("Votes.txt") 

    If found = True Then 
     'the line will only be replace if the person name is found in the line 
     whole_line = Whole_line.replace(VoteName & "," & vote_count, VoteName & "," & vote_count + 1) 

     'Write back into the file 
     outfile.WriteLine(whole_line) 
    Else 
     'if the vote name is not found in the file 
     'Then create a new one 
     outfile.WriteLine(VoteName & ",1" & VbCrLf) 
    End If 

     outfile.Close() 

End Sub 

Private Function GetCount(Byval VoteName As String) 
    infile = New IO.File.StreamReader("Votes.txt")  

    Dim whole_line As String 
    Dim person As String 
    Dim vote_count As Integer 

    'read through the whole file 
    Do While infile.Peek() >= 0 And person <> VoteName 

     'read each line 
     whole_line = infile.ReadLine 

     person = Split(Whole_line, ",")(0) 

     If person = VoteName Then 
      vote_count = Split(whole_line, ",")(1) 
     End If 
    Loop 

    infile.Close() 
    Return vote_count 
End Sub 
+0

、これを試してみてください。どうすればそのことができますか? – beginnerprogrammer

+0

番号を読み取るにはどうすればよいですか? – beginnerprogrammer

+0

私は試しましたが、プログラムを実行すると、そのファイルが別のプロセスで使用されていると表示されます – beginnerprogrammer