Vb.net vb中的顺序存取文件
我正在用VisualBasic编写一个程序,该程序包含一个用于选票类型程序的顺序访问文件。用户单击“保存投票”按钮时,将存储每种类型的投票数。我要做的是在我的access文件中显示实际的投票数。按照我现在编写程序的方式,候选人的名字出现的次数与选票保存的次数一样多。例如,如果perez被投票4次,则在访问文件中,perez显示在4行不同的位置。我如何显示他们的实际投票次数。我正在考虑使用一个计数器变量,但不确定如何在中真正实现它。这就是我目前所拥有的Vb.net vb中的顺序存取文件,vb.net,winforms,Vb.net,Winforms,我正在用VisualBasic编写一个程序,该程序包含一个用于选票类型程序的顺序访问文件。用户单击“保存投票”按钮时,将存储每种类型的投票数。我要做的是在我的access文件中显示实际的投票数。按照我现在编写程序的方式,候选人的名字出现的次数与选票保存的次数一样多。例如,如果perez被投票4次,则在访问文件中,perez显示在4行不同的位置。我如何显示他们的实际投票次数。我正在考虑使用一个计数器变量,但不确定如何在中真正实现它。这就是我目前所拥有的 Public Class Voter Di
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
在btnVote_Click函数中,每次单击时都会将radiobutton文本写入文件,这就是创建问题的方式 此外,您正在计算该名称在文件中作为投票数而不是投票数出现的次数 你应该试着把名字和计数放在你的投票文件中,而不仅仅是名字 马克,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
Private Sub btnVote_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnVote。单击
lstreult.Items.Clear()
'单击了哪些按钮
如果radMark.Checked=True,则
AddCount(radMark.Text)
radMark.Checked=False
ElseIf radSheima.Checked=那么为真
AddCount(radSheima.Text)
radSheima.Checked=False
ElseIf radSam.Checked=则为真
AddCount(radSam.Text)
radSam.Checked=False
其他的
Show(“您应该在其中选择一个”)
如果结束
端接头
私有子btnResult_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnResult。单击
"结果,
lstreult.Items.Clear()
lstreult.Items.Add(“markstone”和GetCount(radMark.Text))
lstreult.Items.Add(“Shemia Patel”和CStr(radSheima.Text))
lstreult.Items.Add(“Sam Perez”和CStr(radSam.Text))
端接头
私有子AddCount(Byval VoteName作为字符串)
infle=New IO.File.StreamReader(“vows.txt”)
将整条线变暗为字符串
朦胧的人
整数形式的Dim投票计数
Dim被发现为布尔值
'通读整个文件直到结束或找到名称
当infle.Peek()>=0和person VoteName时执行
“读每一行
整行=infle.ReadLine
人员=拆分(整行,“,”)(0)
如果person=VoteName,则
投票计数=拆分(整行“,”)(1)
找到=真
如果结束
环
'使用后关闭文件。
infle.Close()
'使用StreamWriter重新打开文件
outfile=IO.File.OpenText(“voates.txt”)
如果find=True,则
'只有在行中找到人名时,才会替换该行
整行=整行。替换(VoteName&“,”&投票计数,VoteName&“,”&投票计数+1)
'回写到文件中
输出文件写入线(整行)
其他的
'如果在文件中找不到投票名
'然后创建一个新的
outfile.WriteLine(VoteName&“、1”&VbCrLf)
如果结束
outfile.Close()
端接头
私有函数GetCount(Byval VoteName作为字符串)
infle=New IO.File.StreamReader(“vows.txt”)
将整条线变暗为字符串
朦胧的人
整数形式的Dim投票计数
“通读整个文件
当infle.Peek()>=0和person VoteName时执行
“读每一行
整行=infle.ReadLine
人员=拆分(整行,“,”)(0)
如果person=VoteName,则
投票计数=拆分(整行“,”)(1)
如果结束
环
infle.Close()
返回计票
端接头
好的,这就是我想弄明白的。我怎么能做到这一点呢?我试过了,但当我运行程序时,它显示文件正被另一个进程使用。这是因为程序启动时打开文件,直到看到结果才关闭它。尝试在需要时打开文件,并在函数完成后立即关闭。我已更改了此代码。因此,我应该在单击“显示投票”按钮时打开该文件?您需要在读取或写入文件之前打开该文件,然后在读取或写入后关闭它。所以您只需要在addCount和getCount函数中打开它