在鼠标悬停和鼠标上更改RadioButton背景颜色会在VB.net中一次全部保留

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

我有一个有很多RadioButtons的程序。我想在鼠标悬停时更改每个鼠标的背景颜色,并在鼠标离开时将背景颜色重置为透明。

我知道我可以使用每个MouseHoverMouseLeaveRadioButton事件,但如果我这样做,代码会很长,因为有太多的RadioButton。

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

这应该适合你,它可以以不同的方式完成,但我修改了一些我必须满足你需要的东西......顺便说一下这是递归,如果它包含一个radiobutton,我们可以改变它,所以挖掘一个控件,所以上...

这是经过试验和测试的

 Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim rButton As New List(Of Control)

    For Each rd As System.Windows.Forms.RadioButton In LoopControls(rButton, Me, GetType(System.Windows.Forms.RadioButton))
        AddHandler rd.MouseHover, AddressOf Me.ChangeColor
        AddHandler rd.MouseLeave, AddressOf Me.Transparent
    Next

End Sub

Public Shared Function LoopControls(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
    If parent Is Nothing Then Return list
    If parent.GetType Is ctrlType Then
        list.Add(parent)
    End If

    For Each child As Control In parent.Controls
        LoopControls(list, child, ctrlType)
    Next
    Return list
End Function

Private Sub Transparent()
    Dim rButton As New List(Of Control)

    For Each rd As System.Windows.Forms.RadioButton In LoopControls(rButton, Me, GetType(System.Windows.Forms.RadioButton))
        rd.BackColor = Color.Transparent
    Next
End Sub

Private Sub ChangeColor()
    Dim rButton As New List(Of Control)

    For Each rd As System.Windows.Forms.RadioButton In LoopControls(rButton, Me, GetType(System.Windows.Forms.RadioButton))
        rd.BackColor = Color.Red
    Next
End Sub

End Class

1
投票

另一种方法是使用MouseEnter事件而不是MouseHover:

Public Class Form

    Private radioButtonsList As List(Of RadioButton)

    Private Sub Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        For Each rb As RadioButton In Me.radioButtonsList
            RemoveHandler rb.MouseEnter, AddressOf Me.RadioButtons_MouseEnter
            RemoveHandler rb.MouseLeave, AddressOf Me.RadioButtons_MouseLeave
        Next

        Me.radioButtonsList = Nothing
    End Sub

    Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.radioButtonsList = New List(Of RadioButton)
        Me.GetAllRadioButtonsFromMe()

        For Each rb As RadioButton In Me.radioButtonsList
            AddHandler rb.MouseEnter, AddressOf Me.RadioButtons_MouseEnter
            AddHandler rb.MouseLeave, AddressOf Me.RadioButtons_MouseLeave
        Next
    End Sub

    Private Sub GetAllRadioButtonsFromMe()
        Dim ctl As Control = Me.GetNextControl(Me, True)
        While ctl IsNot Nothing
            If TypeOf ctl Is RadioButton Then
                Me.radioButtonsList.Add(ctl)
            End If
            ctl = Me.GetNextControl(ctl, True)
        End While
    End Sub

    Private Sub RadioButtons_MouseEnter(sender As Object, e As System.EventArgs)
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        rb.BackColor = Color.DodgerBlue
    End Sub

    Private Sub RadioButtons_MouseLeave(sender As Object, e As System.EventArgs)
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        rb.BackColor = Color.Transparent
    End Sub
End Class
© www.soinside.com 2019 - 2024. All rights reserved.