2012-04-27 31 views
0

我正在使用Visual Basic中的一個程序,該程序結合了對一個選票類型程序使用順序訪問文件。每種類型的用戶點擊保存投票按鈕,投票數量被存儲。我想要做的是在我的訪問文件中顯示的票數爲實際的數字。我的程序現在編寫的方式,候選人的姓名出現在投票保存的次數上。例如,如果佩雷斯被投票4​​次,在訪問文件佩雷斯顯示在4個不同的行。我如何顯示他們被投票的實際次數。我在想使用一個計數器變量,但並不確定如何真正實現它。這是迄今爲止我所擁有的。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

在你btnVote_Click功能你正在寫的單選按鈕文本文件每次你點擊它,這是如何創建的問題。

此外,您還計算名稱在文件中出現的次數爲投票次數,但不是數字。

你應該嘗試把名字和數字放在你的投票文件中,而不僅僅是名字,例如

馬克,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

OK,這就是我試圖搞清楚。我將如何能夠做到這一點? – beginnerprogrammer 2012-04-27 01:02:49

+0

我如何閱讀數字? – beginnerprogrammer 2012-04-27 01:28:08

+0

我試過了,但是當我運行該程序時,它說該文件正在被另一個進程使用 – beginnerprogrammer 2012-04-27 02:08:52