好的所以这个代码是组合框中的项目列表,你必须选择一个项目,然后选择一个喜欢或不喜欢的单选按钮然后点击投票。结果列表框应该每次点击时总计所有投票或者不喜欢投票。当我点击喜欢并按下投票时,结果框中没有显示任何内容,但是当我点击不喜欢并按下投票时,它会显示出来并且总数是正确的,因为我按下并点击了投票,因此每个显示1。它在程序运行的整个过程中一直保持这种状态,所以如果我再打3次就不会在列表框中更新,直到我不喜欢并按下投票。我如何编码以显示和更新喜欢和不喜欢投票?
这是我的代码:
Public Class FoodSurveyForm
Dim votes As Integer(,) = New Integer(0 To 3, 0 To 1) {}
' handles Food Survey Form's Load event
Private Sub FoodSurveyForm_Load(sender As System.Object,
e As System.EventArgs) Handles MyBase.Load
foodsComboBox.SelectedIndex = 0 ' select first food in list
End Sub ' FoodSurveyForm_Load
Private Sub voteButton_Click(sender As System.Object, e As System.EventArgs) Handles voteButton.Click
Dim index As Integer = foodsComboBox.SelectedIndex
'if statement to add like and dislike votes
If likeRadioButton.Checked Then
votes(index, 0) += 1
ElseIf dislikeRadioButton.Checked Then
votes(index, 1) += 1
DisplayVotes()
End If
End Sub
Sub DisplayVotes() 'call DisplayVotes sub procedure
resultsListBox.Items.Clear()
'header for resultListBox
resultsListBox.Items.Add("Menu Item" & ControlChars.Tab & "Like" _
& ControlChars.Tab & "Dislike")
For count As Integer = 0 To foodsComboBox.Items.Count - 1
resultsListBox.Items.Add(foodsComboBox.Items(count).ToString & ControlChars.Tab & votes(count, 0) & _
ControlChars.Tab & votes(count, 1))
Next count
End Sub ' Display Votes
End Class ' FoodSurveyForm
答案 0 :(得分:1)
DisplayVotes()必须超出if块:
Private Sub voteButton_Click(sender As System.Object, e As System.EventArgs) Handles voteButton.Click
Dim index As Integer = foodsComboBox.SelectedIndex
'if statement to add like and dislike votes
If likeRadioButton.Checked Then
votes(index, 0) += 1
ElseIf dislikeRadioButton.Checked Then
votes(index, 1) += 1
End If
DisplayVotes()
End Sub