列表框有三个候选人和一个记录按钮。每次按下记录按钮时,我都需要为列表框中选择的每个候选添加按钮点击。无论我在列表框中选择哪个候选人,我的代码都会计算所有点击次数。如何区分列表框中的每个选定项目。
以下是应用程序外观的图像:http://i.imgur.com/N8zM2.jpg
Public Class Form1
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Dim candidatevotes(2) As Integer
Dim vote
Dim total
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
candListBox.Items.Add("Mark Stone")
candListBox.Items.Add("Sheima Patel")
candListBox.Items.Add("Sam Perez")
candListBox.SelectedIndex = 0
End Sub
Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
Dim outfile As IO.StreamWriter
outfile = IO.File.AppendText("voteinfo.txt")
outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
outfile.Close()
End Sub
Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
Dim infile As IO.StreamReader
If IO.File.Exists("voteinfo.txt") = True Then
infile = IO.File.OpenText("voteinfo.txt")
infile.Close()
End If
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
End Sub
End Class
答案 0 :(得分:0)
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
应该是
candidatevotes(candListBox.SelectedIndex) += 1
和
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
应该是
markLabel.Text = candidatevotes(0)
sheimaLabel.Text = candidatevotes(1)
samLabel.Text = candidatevotes(2)