【问题标题】:Displaying winner's name in picture box在图片框中显示获奖者姓名
【发布时间】:2016-09-15 23:43:41
【问题描述】:

下面是我正在编写的一个简单投票系统的代码。

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

【问题讨论】:

  • 图片框没有Text 属性。使用更合适的东西,比如标签

标签: vb.net picturebox


【解决方案1】:

您的代码实际上可以很好地进行初始绘制,但是当图片框图像没有自己的位图集时,许多事件可以在幕后重新绘制其图形(甚至像最小化/最大化表单一样简单,以及一大堆其他的),因此实际上您的文本似乎根本不会出现或几乎立即消失,而实际上它可能会被重新绘制。要解决此问题,请使用位图作为图形对象的参考,绘制位图的图形,然后将位图分配给图片框的图像属性。这将使图像持久化...在 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,然后将最大值增加 1。如果您想坚持那种选择获胜者的逻辑,你会想要遍历所有的候选人(不仅仅是那些从索引 0 到当前选择的候选人),如果他们的投票数高于最大值,那么将 max 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

这样,该程序允许将任意数量的名字添加到候选人列表中,并且每次双击他们的名字时都会为他们的名字添加投票计数。然后,当你的获胜者像素框被点击时,它会找到票数最高的字典,并在获胜者框中显示他们的名字。

【讨论】:

  • 代码肯定会显示,尽管它会显示最后点击的候选者
  • 那是candidateWinner函数中for循环的结果,见上面的EDIT
  • 有效!谢谢!!
【解决方案2】:

你可以试试这个来抽奖:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多