locked
Lots of buttons , on press button color change last button reset color. RRS feed

  • Question

  • Sorry to bother you on this really simple one, but I keep on getting it wrong.

    I have some buttons, when I press the button the colour of the button changes to indicate selection I then press a new button and the colour changes to indicate selection and the old button needs to reset to the default (original) colour.

    Can you help?

    Tuesday, July 19, 2016 11:10 AM

Answers

  • Sorry to bother you on this really simple one, but I keep on getting it wrong.

    I have some buttons, when I press the button the colour of the button changes to indicate selection I then press a new button and the colour changes to indicate selection and the old button needs to reset to the default (original) colour.

    Can you help?

    Hi

    Here is some code that will check/change backcolor of selected button in a Panel

    ' needs Panel1 with some buttons in it ' only the buttons in the Panel will be checked Option Strict On Option Infer Off Option Explicit On Class Form1 ' store backcolor of one button Dim oldColour As Color = Color.FromKnownColor(KnownColor.ControlLight) Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click Dim b As Button = DirectCast(sender, Button) For Each c As Control In Me.Panel1.Controls If c.GetType() Is GetType(Button) Then ' set all to original colour c.BackColor = oldColour End If Next ' set selected button to Red b.BackColor = Color.Red

    ' if dark background then may need
            ' light foregtound color
            b.ForeColor = Color.White End Sub End Class



    Regards Les, Livingston, Scotland


    Tuesday, July 19, 2016 11:33 AM
  • Thanks works great but how do I switch it off, so that when I go to do something else the colour changes back to neutral. I have tried adding a button to reset:

     For Each c As Control In Me.Panel1.Controls
    
                If c.GetType() Is GetType(Button) Then
                   
                    c.BackColor = Color.lightgray
    
                End If
            Next

    but it completely ignores this. Even when I add the control to the panel.


    Hi

    Here is another version of the code I previously posted. This one uses the Panel Leave event to trigger the reset of the buttons in the Panel. When you select another control the buttons revert to defaults.

    ' needs Panel1 with some buttons in it
    ' only the buttons in the Panel will be checked
    Option Strict On
    Option Infer Off
    Option Explicit On
    Class Form1
        ' store backcolor and forecolor of default button
        Dim oldBackColour As Color = Color.FromKnownColor(KnownColor.ControlLight)
        Dim oldForeColour As Color = Color.FromKnownColor(KnownColor.ControlText)
        Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
            Dim b As Button = DirectCast(sender, Button)
    
            For Each c As Control In Me.Panel1.Controls
                If c.GetType() Is GetType(Button) Then
                    ' set all to original colour
                    c.BackColor = oldBackColour
                    c.ForeColor = oldForeColour
                End If
            Next
            ' set selected button to Red
            b.BackColor = Color.Red
    
            ' if dark background then may need
            ' light foregtound color
            b.ForeColor = Color.White
        End Sub
        Private Sub Panel1_Leave(sender As Object, e As EventArgs) Handles Panel1.Leave
            For Each c As Control In Me.Panel1.Controls
                If c.GetType() Is GetType(Button) Then
                    ' set all to original colours
                    c.BackColor = oldBackColour
                    c.ForeColor = oldForeColour
                End If
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, July 20, 2016 11:50 PM
  • These Buttons are on a Form. Panels contain their own controls. You would need to modify the code to get Button controls on a Form or Button controls on a Panel or both or however you want to do it possibly.

    I didn't test this with anything else in the Button click events.

    Option Strict On
    
    Public Class Form1
    
        Dim ButtonList As New List(Of Button)
        Dim BtnBackColor As Color
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            BtnBackColor = Button1.BackColor
            For Each Ctl As Control In Me.Controls.OfType(Of Button)()
                ButtonList.Add(CType(Ctl, Button))
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub AllButton_Click(sender As Object, e As EventArgs)
            For i = 0 To ButtonList.Count - 1
                If ButtonList(i) Is DirectCast(sender, Button) Then
                    DirectCast(sender, Button).BackColor = Color.OrangeRed
                Else
                    'ButtonList(i).BackColor = SystemColors.Control
                    ButtonList(i).BackColor = BtnBackColor
                End If
            Next
            DirectCast(sender, Button).Focus()
            MessageBox.Show(DirectCast(sender, Button).Name & " Clicked")
        End Sub
    
    End Class


    La vida loca

    Thursday, July 21, 2016 12:19 AM

All replies

  • Sorry to bother you on this really simple one, but I keep on getting it wrong.

    I have some buttons, when I press the button the colour of the button changes to indicate selection I then press a new button and the colour changes to indicate selection and the old button needs to reset to the default (original) colour.

    Can you help?

    Hi

    Here is some code that will check/change backcolor of selected button in a Panel

    ' needs Panel1 with some buttons in it ' only the buttons in the Panel will be checked Option Strict On Option Infer Off Option Explicit On Class Form1 ' store backcolor of one button Dim oldColour As Color = Color.FromKnownColor(KnownColor.ControlLight) Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click Dim b As Button = DirectCast(sender, Button) For Each c As Control In Me.Panel1.Controls If c.GetType() Is GetType(Button) Then ' set all to original colour c.BackColor = oldColour End If Next ' set selected button to Red b.BackColor = Color.Red

    ' if dark background then may need
            ' light foregtound color
            b.ForeColor = Color.White End Sub End Class



    Regards Les, Livingston, Scotland


    Tuesday, July 19, 2016 11:33 AM
  • Buttons which remain selected are really acting like CheckBoxes and so you may find it easiser to use a Checkbox with its Appearance property set to Button.

    All you need to do to change the colour of the previous selection is to remember which button/checkbox was clicked last and change it when another button/checkbox is selected.

       Dim LastSelection As CheckBox
       Private Sub CheckBox1_CheckedChanged(sender As Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged
          Dim CB As CheckBox = CType(sender, CheckBox)
          If LastSelection IsNot Nothing Then
             LastSelection.BackColor = Nothing
          End If
          LastSelection = CB
          CB.BackColor = Color.LightGreen
       End Sub
    

    • Proposed as answer by IronRazerz Tuesday, July 19, 2016 4:43 PM
    Tuesday, July 19, 2016 12:03 PM
  • Hi

    This thread has been answered I believe. Please start a new thread for a new question. Thanks.


    Regards Les, Livingston, Scotland

    Tuesday, July 19, 2016 4:13 PM
  • Thanks works great but how do I switch it off, so that when I go to do something else the colour changes back to neutral. I have tried adding a button to reset:

     For Each c As Control In Me.Panel1.Controls
    
                If c.GetType() Is GetType(Button) Then
                   
                    c.BackColor = Color.lightgray
    
                End If
            Next

    but it completely ignores this. Even when I add the control to the panel.


    Wednesday, July 20, 2016 11:25 PM
  • Thanks works great but how do I switch it off, so that when I go to do something else the colour changes back to neutral. I have tried adding a button to reset:

     For Each c As Control In Me.Panel1.Controls
    
                If c.GetType() Is GetType(Button) Then
                   
                    c.BackColor = Color.lightgray
    
                End If
            Next

    but it completely ignores this. Even when I add the control to the panel.


    Hi

    Here is another version of the code I previously posted. This one uses the Panel Leave event to trigger the reset of the buttons in the Panel. When you select another control the buttons revert to defaults.

    ' needs Panel1 with some buttons in it
    ' only the buttons in the Panel will be checked
    Option Strict On
    Option Infer Off
    Option Explicit On
    Class Form1
        ' store backcolor and forecolor of default button
        Dim oldBackColour As Color = Color.FromKnownColor(KnownColor.ControlLight)
        Dim oldForeColour As Color = Color.FromKnownColor(KnownColor.ControlText)
        Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
            Dim b As Button = DirectCast(sender, Button)
    
            For Each c As Control In Me.Panel1.Controls
                If c.GetType() Is GetType(Button) Then
                    ' set all to original colour
                    c.BackColor = oldBackColour
                    c.ForeColor = oldForeColour
                End If
            Next
            ' set selected button to Red
            b.BackColor = Color.Red
    
            ' if dark background then may need
            ' light foregtound color
            b.ForeColor = Color.White
        End Sub
        Private Sub Panel1_Leave(sender As Object, e As EventArgs) Handles Panel1.Leave
            For Each c As Control In Me.Panel1.Controls
                If c.GetType() Is GetType(Button) Then
                    ' set all to original colours
                    c.BackColor = oldBackColour
                    c.ForeColor = oldForeColour
                End If
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, July 20, 2016 11:50 PM
  • These Buttons are on a Form. Panels contain their own controls. You would need to modify the code to get Button controls on a Form or Button controls on a Panel or both or however you want to do it possibly.

    I didn't test this with anything else in the Button click events.

    Option Strict On
    
    Public Class Form1
    
        Dim ButtonList As New List(Of Button)
        Dim BtnBackColor As Color
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            BtnBackColor = Button1.BackColor
            For Each Ctl As Control In Me.Controls.OfType(Of Button)()
                ButtonList.Add(CType(Ctl, Button))
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            AllButton_Click(sender, e)
        End Sub
    
        Private Sub AllButton_Click(sender As Object, e As EventArgs)
            For i = 0 To ButtonList.Count - 1
                If ButtonList(i) Is DirectCast(sender, Button) Then
                    DirectCast(sender, Button).BackColor = Color.OrangeRed
                Else
                    'ButtonList(i).BackColor = SystemColors.Control
                    ButtonList(i).BackColor = BtnBackColor
                End If
            Next
            DirectCast(sender, Button).Focus()
            MessageBox.Show(DirectCast(sender, Button).Name & " Clicked")
        End Sub
    
    End Class


    La vida loca

    Thursday, July 21, 2016 12:19 AM
  • Thank you verry much.

    best regards.

    Thursday, July 21, 2016 1:43 PM
  • Great, thank you so much.
    Thursday, July 21, 2016 1:45 PM
  • Great, thank you so much.

    If any post answered your question then please propose it or them as the answer(s).

    La vida loca

    Thursday, July 21, 2016 7:58 PM