早上好,
我正在使用Visual Basic中的投票程序(使用Visual Studio 2013,如果这有任何区别),而且我似乎无法获得每个候选人的投票功能。 这是一个赋值,所以我必须使用所选索引的listbox.DoubleClick事件处理程序来计算投票。
到目前为止,我已经按照自己的意愿编写了整个程序,但我无法与候选人一致得票。我认为这与这段代码有关
Try
'Selected candidate gets a vote tallied
vote(candidateList.SelectedIndex) += 1
candidateList.SelectedIndex = -1
Catch exc As IndexOutOfRangeException
MessageBox.Show("Please click on a candidate to vote.", "Attention!")
End Try
如果需要,可以在此处查看我的整个项目 - Voting Machine source code
知道什么会帮助我的程序协调数据?或者我的错误是什么? 谢谢!
编辑 - 我现在大部分时间都在工作,每位候选人都获得了准确的投票数,但现在我需要弄清楚如何在同一个列表框中同时获得候选人姓名和总票数。 上面的代码以正确的顺序显示正确的投票,但如果我尝试将candidateList.selectedItem添加到混合中,则会抛出无效的强制转换异常,因为字符串(候选名称)无法转换为整数。如何获取selectedItem并增加所选索引的计数?在今天的大部分时间里,我现在都陷入困境,非常感谢帮助。 谢谢!
答案 0 :(得分:0)
我看了你的代码。在开始使用它之前,需要初始化“vote”数组。你有这一行:
Dim vote() As Integer
实际上并没有初始化整数数组。以下是一些如何初始化的示例,其长度为3或3个变量。
Dim vote(3) As Integer
Dim vote As Integer() = {1,2,3}
顺便说一下,最好检查“candidateList.SelectedIndex”是否有实际有效值(不是-1)。
答案 1 :(得分:0)
在您的代码中,声明了投票数组但未初始化,这导致NullReferenceException
请使用适当的维度初始化数组,例如,您可以在代码中使用" showCandidates()"功能如下,
/views/controller_name/get_config_values.json
每当您将候选人添加到列表框(i-e提名候选人)时,请重新调整投票数组,否则您可能会获得IndexOutOfBounds异常。
答案 2 :(得分:0)
我找到了解决问题的方法。非常感谢那些帮助我的人!
Imports System.IO
公共班级投票
Dim Candidates() As String
Dim votes() As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get default values and views for Voting Machine
resultsList.Visible = False
candidateList.Location = New Point(143, 71)
tallyVotes.Enabled = False
resultsList.Enabled = False
Label1.Text = "Click 'Nominate Candidate' to enter a candidate, or 'Start Voting'" & vbCrLf & "to end nominations and start the voting."
End Sub
Private Sub candidateList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles candidateList.SelectedIndexChanged
End Sub
Private Sub resultsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles resultsList.SelectedIndexChanged
End Sub
Private Sub nominateCandidate_Click(sender As Object, e As EventArgs) Handles nominateCandidate.Click
'Instructions
MessageBox.Show("When finished entering candidates, simply press enter on a blank line.", "Instructions")
'Gather list of Candidates
Dim candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500)
Dim i As Integer = 0
'Loops until a null string is entered signaling the end of input
Do Until String.IsNullOrEmpty(candidateName)
ReDim Preserve Candidates(i)
Candidates(i) = candidateName
i += 1
candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500)
Loop
End Sub
Private Sub startVoting_Click(sender As Object, e As EventArgs) Handles startVoting.Click
'Disable the Nomination button
nominateCandidate.Enabled = False
'Enable the tally votes button
tallyVotes.Enabled = True
'Set the label text to give instructions for tallying votes.
Label1.Text = "Vote for a candidate by double-clicking his or her name." & vbCrLf & "End the voting by clicking on 'Tally Votes'."
'Call sub to display the candidates for voting
showCandidates()
End Sub
Private Sub tallyVotes_Click(sender As Object, e As EventArgs) Handles tallyVotes.Click
'Makes results listbox visible and moves the candidate list to the left
resultsList.Visible = True
candidateList.Location = New Point(14, 71)
'Call the sub to tally the votes and display the winner
getResults()
End Sub
Private Sub candidateList_DoubleClick(sender As Object, e As EventArgs) Handles candidateList.DoubleClick
'Selected candidate gets a vote tallied
Try
'Selected candidate gets a vote tallied
votes(candidateList.SelectedIndex) += 1
candidateList.SelectedIndex = -1
Catch exc As IndexOutOfRangeException
MessageBox.Show("Please click on a candidate to vote.", "Attention!")
End Try
End Sub
Sub showCandidates()
'Display the candidates in the listbox and sort alphabetically by last name
Dim query = From candidate In Candidates
Let firstName = candidate.Split(" "c)(0)
Let lastName = candidate.Split(" "c)(1)
Let name = firstName & " " & lastName
Order By lastName
Select name
For Each Name As String In query
candidateList.Items.Add(Name)
Next
ReDim Preserve votes(candidateList.Items.Count - 1)
End Sub
Sub getResults()
'Add the results to the Results Listbox
For Each i In votes
resultsList.Items.Add(i)
Next
'Declare Winner
Dim mostVotes As Integer = 0
For Each item In resultsList.Items
If item > mostVotes Then
mostVotes = item
End If
Next
resultsList.SelectedItem = mostVotes
candidateList.SelectedIndex = resultsList.SelectedIndex
Dim winner = candidateList.SelectedItem
MessageBox.Show("The winner is " & winner)
End Sub
结束班