在图片框中显示获胜者的名字

时间:2016-09-15 09:35:50

标签: vb.net picturebox

以下是我正在编码的简单投票系统的代码。

Public Class Form1
    Dim winner As String
    Dim maxVotes As Integer
    Dim votes() As String
    Dim index As String
    Dim candidates As String

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        If Not isValidInput(txtNewCandidate.Text) Then
            Exit Sub
        End If
        lstCandidates.Items.Add(txtNewCandidate.Text)
        txtNewCandidate.Clear()
        txtNewCandidate.Focus()
        ReDim Preserve votes(index)
        index += 1
    End Sub

    Private Function isValidInput(ByRef firstName As String) As Boolean
        If IsNumeric(txtNewCandidate.Text) Or txtNewCandidate.Text = "" Then
            MsgBox("Please input a valid candidate name.")
            txtNewCandidate.Focus()
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub btnTally_Click(sender As Object, e As EventArgs) Handles btnTally.Click
        lstTallies.Visible = True
        lblTally.Visible = True
        For i = 0 To lstCandidates.Items.Count - 1
            lstTallies.Items.Add(lstCandidates.Items(i).ToString & " - " & votes(i))
        Next
    End Sub

    Private Sub lstCandidates_DoubleClick(sender As Object, e As EventArgs) Handles lstCandidates.DoubleClick
        If lstCandidates.SelectedIndex = -1 Then
            MsgBox("Select a candidate by double-clicking")
        End If
        votes(lstCandidates.SelectedIndex) += 1
        MsgBox("Vote Tallied")
    End Sub

    Private Sub pbxWinner_Click(sender As Object, e As EventArgs) Handles pbxWinner.Click

    End Sub
End Class

选民必须在第一个列表框中双击他们对候选人的选择。然后,用户通过单击按钮来计算投票,并且将出现第二个列表框,其中每个候选人都有投票。

现在我需要在图片框pbxWinner中显示获胜者(或获胜者,如果有领带)。我不知道如何做到这一点。有线索吗?

这是我想要做的,虽然下面的代码不起作用。

Private Function candidateWinner(ByRef winner As String) As Boolean
    For i As Integer = 0 To lstCandidates.SelectedIndex - 1
        If votes(i) > maxVotes Then
            maxVotes += 1
        End If
    Next
    g = pbxWinner.CreateGraphics
    g.TranslateTransform(10.0F, 0.0F)
    g.DrawString(winner, New Font("Arial", 7, FontStyle.Regular), Brushes.DarkBlue, New PointF(0, 0))
    Return True
End Function

2 个答案:

答案 0 :(得分:1)

您的代码实际上对于初始绘图工作正常,但是当图片框图像没有自己的位图集时,许多事件可以在幕后重绘其图形(甚至像最小化/ mazimizing一样简单形式,以及一大堆其他的),所以实际上你的文字似乎永远不会出现或几乎立即消失,而实际上它很可能重新粉刷。要解决此问题,请使用位图作为图形对象的参考,绘制位图的图形,然后将位图指定给图片框的图像属性。这将使图像持久...在for循环之后,在candidateWinner函数中尝试使用此代码:

Dim bmp As New Bitmap(pbxWinner.Width, pbxWinner.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.TranslateTransform(10.0F, 0.0F)
g.DrawString(winner, New Font("arial", 7, FontStyle.Regular), Brushes.DarkBlue, 0, 0)
pbxWinner.Image = bmp

...如果您仍然没有看到文字,请确保获胜者字符串具有正确的值集,我测试了此代码并且它正确显示了我的测试字符串

编辑评论:

这是因为你用来计算胜利者的逻辑...你只是检查当前所选候选人的投票数是否高于maxVotes,然后递增如果你想坚持使用那种逻辑来挑选胜利者,你会想要遍历所有候选人(不仅仅是从索引0到当前选中的候选人),如果他们的投票数量更高比最大值,然后将最大EQUAL设置为他们的投票数。然后循环中的下一个候选者将根据先前的最大值检查其计数。但是,如果您只是使用字典,那么跟踪获胜者可以更容易,因为您允许添加候选人,并且您必须更改您的"胜利者"实际检查每个人输入的票数最多的逻辑。一个简单的例子就是这样:

Dim dctTally As Dictionary(Of String, Integer)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dctTally = New Dictionary(Of String, Integer)
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    dctTally.Add(txtNewCandidate.Text, 0)
    lstCandidates.Items.Add(txtNewCandidate.Text)
End Sub

Private Sub lstCandidates_DoubleClick(sender As Object, e As EventArgs) Handles lstCandidates.DoubleClick
    dctTally(lstCandidates.text) += 1
End Sub

Private Sub pbxWinner_Click(sender As Object, e As EventArgs) Handles pbxWinner.Click
    Dim winner = dctTally.Aggregate(Function(l, r) If(l.Value > r.Value, l, r)).Key
    Dim bmp As New Bitmap(pbxWinner.Width, pbxWinner.Height)
    Dim g As Graphics = Graphics.FromImage(bmp)
    g.TranslateTransform(10.0F, 0.0F)
    g.DrawString(winner, New Font("arial", 7, FontStyle.Regular), Brushes.DarkBlue, 0, 0)
    pbxWinner.Image = bmp
End Sub

这样,程序允许将任意数量的名称添加到候选列表中,并且每次双击其名称时,都会在其名称中添加一个投票计数。然后,当您点击获胜者pixturebox时,它会找到具有最高投票数的字典,并在获胜者框中显示其名称。

答案 1 :(得分:1)

你可以试试这个来赢得胜利者:

Private Sub candidateWinner()

    Dim y As Single = 0

    maxVotes = votes.Select(Function(x) Convert.ToInt32(x)).Max()

    For i = 0 To UBound(votes)
        If votes(i) = maxVotes.ToString() Then
            g = pbxWinner.CreateGraphics
            g.TranslateTransform(10.0F, 0.0F)
            g.DrawString(lstCandidates.Items(i).ToString(), New Font("Arial", 7, FontStyle.Regular), Brushes.DarkBlue, New PointF(0, y))
            y += 10
            g.Dispose()
        End If
    Next

End Sub