Emerald
Emerald

Reputation: 874

Change focus color when button clicked

I have few buttons on my system and I tried to change the color focus when button is clicked. So far my coding is only able to change the button color when clicked but I want my system be able to reset the button color back to it's normal color as well when other button is clicked.

I tried to find solution on website but I don't really understand how because their sample is too complicated for me.

Here is my simple coding to change the button color focus.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.Button1.BackColor = Color.Gainsboro
    Me.Button1.ForeColor = Color.Black
End Sub

Kindly to help me. Thank you.

Upvotes: 1

Views: 3669

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125292

Since the user can focus on buttons without click, it's better to handle GotFocus and LostFocus events for buttons and put your logic there.

In below code, I assigned a handler to those events for all buttons in form and stored original ForeColor and BackColor in a data structure in Tag property. Then in GotFocus I set the ForeColor and BackColor to desired focusedForeColor and focusedBackColor. Also in LostFocus I restore original forecolor and backcolor that I stored previously in Tag.

It's enough to paste this code in your form code and it will work for all buttons:

'Change these to your desired color
Private focusedForeColor As Color = Color.Black
Private focusedBackColor As Color = Color.Gainsboro

Private Function GetAllControls(control As Control) As IEnumerable(Of Control)
    Dim controls = control.Controls.Cast(Of Control)()
    Return controls.SelectMany(Function(ctrl) GetAllControls(ctrl)).Concat(controls)
End Function

Public Sub New()
    InitializeComponent()
    Me.GetAllControls(Me).OfType(Of Button)().ToList() _
      .ForEach(Sub(b)
                   b.Tag = Tuple.Create(b.ForeColor, b.BackColor)
                   AddHandler b.GotFocus, AddressOf b_GotFocus
                   AddHandler b.LostFocus, AddressOf b_LostFocus
               End Sub)
End Sub

Private Sub b_LostFocus(sender As Object, e As EventArgs)
    Dim b = DirectCast(sender, Button)
    Dim colors = DirectCast(b.Tag, Tuple(Of Color, Color))
    b.ForeColor = colors.Item1
    b.BackColor = colors.Item2
End Sub

Private Sub b_GotFocus(sender As Object, e As EventArgs)
    Dim b = DirectCast(sender, Button)
    b.ForeColor = focusedForeColor
    b.BackColor = focusedBackColor
End Sub

Upvotes: 1

Juan Barahona
Juan Barahona

Reputation: 192

In the declarations section create 2 Color variables, one for the background property and another for the forecolor property. You have to assign the Background color and Foreground color properties of Button1 to these variables in the event Load of the form. When you click Button1 it changes with the code you did and when you click the other button it restored the Button1 colors through the use of the color variables. I hope this explanation help you. Below is the full code for further clarification.

Public Class Form1 Dim bgColor, foColor As Color

Private Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles Button1.Click
    Button1.BackColor = Color.Yellow
    Button1.ForeColor = Color.Blue
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) _
    Handles Button2.Click
    Button1.BackColor = bgColor
    Button1.ForeColor = foColor
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) _
    Handles MyBase.Load
    bgColor = Button1.BackColor
    foColor = Button1.ForeColor
End Sub

End Class

Upvotes: 1

Related Questions