根据变量绘制多个单选按钮

问题描述 投票:0回答:1

我有多个单选按钮,想根据变量绘制它们。

我在这里找到的基本代码: 更改图形用户界面中的“灯”颜色指示器 (Visual Studio 2019)

现在这个适用于 1 个单选按钮,但正如我所说,我有多个。

我的代码看起来:

Private Sub cbStart_CheckedChanged(sender As Object, e As EventArgs) Handles cbStartEDD.CheckedChanged

    rbEDD.ForeColor = Color.White
    rbEDD.ForeColor = Color.Black
    rbCV.ForeColor = Color.White
    rbCV.ForeColor = Color.Black

End Sub

Private Sub rbEDD_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles rbEDD.Paint, rbCV
    If cbStart = -1 Then
        e.Graphics.FillEllipse(Brushes.Red, New RectangleF(1, 1, 10, 10))
    Else If cbStart =0 then
        e.Graphics.FillEllipse(Brushes.Orange, New RectangleF(1, 1, 10, 10))
    Else
        e.Graphics.FillEllipse(Brushes.Green, New RectangleF(1, 1, 10, 10))
    End If
End Sub

因此,当我更改/单击复选框时,所有单选按钮。

我能做的是:每个单选按钮都有一个子:

Private Sub cbStart_CheckedChanged(sender As Object, e As EventArgs) Handles cbStartEDD.CheckedChanged

    rbEDD.ForeColor = Color.White
    rbEDD.ForeColor = Color.Black
    rbCV.ForeColor = Color.White
    rbCV.ForeColor = Color.Black

End Sub

Private Sub rbEDD_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles rbEDD.Paint
    If cbSV = -1 Then
        e.Graphics.FillEllipse(Brushes.Red, New RectangleF(1, 1, 10, 10))
    Else If cbSV =0 then
        e.Graphics.FillEllipse(Brushes.Orange, New RectangleF(1, 1, 10, 10))
    Else
        e.Graphics.FillEllipse(Brushes.Green, New RectangleF(1, 1, 10, 10))
    End If
End Sub

Private Sub rbCV_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles rbCV.Paint
    If cbCV = -1 Then
        e.Graphics.FillEllipse(Brushes.Red, New RectangleF(1, 1, 10, 10))
    Else If cbStart =0 then
        e.Graphics.FillEllipse(Brushes.Orange, New RectangleF(1, 1, 10, 10))
    Else
        e.Graphics.FillEllipse(Brushes.Green, New RectangleF(1, 1, 10, 10))
    End If
End Sub

我想要的是一个子程序,当它被调用时,它会改变单选按钮变量的颜色: 以下代码只是描述我想要的:

Private Sub cb<Name of the checkbox>_CheckedChanged(sender As Object, e As EventArgs) Handles cb<Name of the checkbox>.CheckedChanged

    Call buttoncolor(<name of the radiobutton>, <statusvalue(-1,0 or 1)>)

End Sub

Private Sub buttoncolor(radiobutton As Object, status As Integer) 
    If status = -1 Then
        ' do following to the radiobutton object
        radiobutton.e.Graphics.FillEllipse(Brushes.Red, New RectangleF(1, 1, 10, 10))
    Else If status = 0 then
        radiobutton.e.Graphics.FillEllipse(Brushes.Orange, New RectangleF(1, 1, 10, 10))
    Else
        radiobutton.e.Graphics.FillEllipse(Brushes.Green, New RectangleF(1, 1, 10, 10))
    End If
End Sub

有没有简单的方法可以做到这一点,或者我必须多次复制和粘贴 Paint-sub 吗?

vb.net radio-button paint
1个回答
0
投票

您需要将图形保留在 Paint 事件中,因为您可能已经看到,您只能访问那里的图形对象。您可以在其他地方访问它,但 Paint 事件将覆盖您在其外部执行的任何操作。但是您可以为两个 RadioButtons 的 Paint 事件创建一个处理程序,请注意

Handles rbCV.Paint, rbEdd.Paint

添加字典来维护RadioButtons和CheckBoxes之间的关系

Private radioToCheck As New Dictionary(Of RadioButton, CheckBox)()

Private Sub Form_Load() Handles Me.Load
    radioToCheck.Add(rbEdd, cbSV)
    radioToCheck.Add(rbCV, cbCV)
End Sub

那么Paint事件可以处理多个RadioButtons的Paint事件

Private Sub rb_Paint(sender As Object, e As PaintEventArgs) Handles rbCV.Paint, rbEdd.Paint
    Dim b As Brush
    Select Case radioToCheck(DirectCast(sender, RadioButton)).CheckState - 1
        Case -1 : b = Brushes.Red
        Case 0 : b = Brushes.Orange
        Case Else : b = Brushes.Green
    End Select
    e.Graphics.FillEllipse(b, New RectangleF(1, 1, 10, 10))
End Sub

我随意确定状态如何来自 CheckBox,因为

cbCV = -1
不起作用 - CheckBox 不能等于 -1。我猜是 CheckState - 1,因为 CheckBox 具有三种状态 {0, 1, 2},而您的状态是 {-1, 0, 1}。如果这是错误的,您可以将逻辑
radioToCheck(rb).CheckState - 1
更改为函数以获取给定 CheckBox 的状态。

理想情况下,状态不存储在 UI 中,您可以使用某种模型来表示状态,并且当状态更改时更新 UI,并用于更新状态。在小规模上,你所做的很好,但它会非常笨拙,并且当你扩大规模时,UI 可能会变得迟缓。

© www.soinside.com 2019 - 2024. All rights reserved.