Reputation: 91
I have a program that has a lot of RadioButtons
. I want to change the background color of each on mouse hover and reset the background color to transparent on mouse leave, all at once.
I know I can use the MouseHover
and MouseLeave
event of each RadioButton
but the code would be lengthy if I do it that way because there are too many RadioButtons.
Upvotes: 0
Views: 633
Reputation: 410
An alternative is to use the MouseEnter event instead of 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
Upvotes: 1
Reputation: 8004
This should work for you, it could be done in a different way, but I modified something I had to fit your needs... This is recursion by the way, dig into a control if it contains a radiobutton we can change it and so on...
This is tried and tested
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
Upvotes: 1