vb中的顺序访问文件

时间:2012-04-27 00:39:28

标签: vb.net winforms

我正在使用Visual Basic中的程序,该程序使用顺序访问文件进行投票类型程序。用户单击保存投票按钮的每种类型,存储的投票数量。我想要做的是在我的访问文件中显示投票数作为实际数字。我的程序现在的编写方式,候选人的名字会随着投票的保存而出现多次。例如,如果perez被投了4次,那么在访问文件中,perez将显示在4个不同的行上。如何显示投票时间的实际数量。我正在考虑使用计数器变量,但不确定如何真正实现它。这是我到目前为止所拥有的。

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

1 个答案:

答案 0 :(得分:2)

在你的btnVote_Click函数中,每次点击它时都会将radiobutton文本写入文件,这就是创建问题的方法。

此外,您还要计算名称在文件中显示的次数,即投票数而不是数字。

您应该尝试将名称和计数放在投票文件中,而不仅仅是名称,例如

标记,1个
Sheima,2
同样,5

然后当您点击投票按钮时,您应该读取Mark的数字,递增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