Results 1 to 10 of 10

Thread: How to make the button as selected (background color)?

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    22

    How to make the button as selected (background color)?

    Hello,

    I thought it would be a simple question but I really searched for long time and I didn't get the answer.

    Q: How can I make a button as selected with this background color?

    Name:  vb1.jpg
Views: 257
Size:  11.3 KB

    In mouse hover the background will be light blue (see Fuel button in the screenshot), I want to stick this light blue background when I click on the button so the user will know he is exploring the content under this button.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: How to make the button as selected (background color)?

    A really simple solution could be to change the Button's BackColor to signify a selected state.

    It seems what you're asking for is a Button that behaves like a RadioButton. While it is simple to describe, it's not really that simple to implement the exact way you described.

    You could subclass the control and override different events to control how it behaves and looks. However, the amount of effort required would be about the same as writing a Button control yourself. And it would take a lot more effort if you wanted it to look just like any other Button since you would have to deal with the somewhat complicated Theming API.

    That being said, you should create your own control from scratch and give it the behaviors and look you desire. It's the most painless solution I could think of that doesn't involve trying to change how the Button behaves. This is what I would do if I had the time. If time was a factor and I needed a quick and dirty solution, I'd simply change the BackColor like I suggested at the start of this post.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    22

    Re: How to make the button as selected (background color)?

    Quote Originally Posted by Niya View Post
    A really simple solution could be to change the Button's BackColor to signify a selected state.

    It seems what you're asking for is a Button that behaves like a RadioButton. While it is simple to describe, it's not really that simple to implement the exact way you described.

    You could subclass the control and override different events to control how it behaves and looks. However, the amount of effort required would be about the same as writing a Button control yourself. And it would take a lot more effort if you wanted it to look just like any other Button since you would have to deal with the somewhat complicated Theming API.

    That being said, you should create your own control from scratch and give it the behaviors and look you desire. It's the most painless solution I could think of that doesn't involve trying to change how the Button behaves. This is what I would do if I had the time. If time was a factor and I needed a quick and dirty solution, I'd simply change the BackColor like I suggested at the start of this post.
    Thanks Niya for the detailed reply. I know how to change the button background when selected but the background color will be solid not same the light blue in my screenshot.

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: How to make the button as selected (background color)?

    I'm sure there's plenty of room for optimization, but here is a quick example of what it sounds like you are describing:

    Code:
      Private btnCollection As New Collection()
      Private btnSelected As System.Object
    
    
      Private Sub frmButtonColorTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        btnCollection.Add(btnAvailability)
        btnCollection.Add(btnFuel)
        btnCollection.Add(btnEnergy)
    
      End Sub
    
      Private Sub btnButtons_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles btnAvailability.MouseEnter, btnFuel.MouseEnter, _
          btnEnergy.MouseEnter
    
        CType(sender, Button).BackColor = Color.LightBlue
    
      End Sub
    
      Private Sub btnButtons_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles btnAvailability.MouseLeave, btnFuel.MouseLeave, _
          btnEnergy.MouseLeave
    
        If Not btnSelected Is sender Then
          CType(sender, Button).BackColor = Control.DefaultBackColor
        End If
    
      End Sub
    
      Private Sub btnAvailability_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvailability.Click
    
        SetButtonClickColor(sender)
        ' Do other Availability button click events
    
      End Sub
    
      Private Sub btnFuel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFuel.Click
    
        SetButtonClickColor(sender)
        ' Do other Fuel button click events
    
      End Sub
    
      Private Sub btnEnergy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnergy.Click
    
        SetButtonClickColor(sender)
        ' Do other Energy button click events
    
      End Sub
    
      Private Sub SetButtonClickColor(ByVal sender As System.Object)
    
        Dim objButton As Object
    
        For Each objButton In btnCollection
          CType(objButton, Button).BackColor = Control.DefaultBackColor
        Next
    
        CType(sender, Button).BackColor = Color.LightBlue
        btnSelected = sender
    
      End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    22

    Re: How to make the button as selected (background color)?

    Quote Originally Posted by OptionBase1 View Post
    I'm sure there's plenty of room for optimization, but here is a quick example of what it sounds like you are describing:

    Code:
      Private btnCollection As New Collection()
      Private btnSelected As System.Object
    
    
      Private Sub frmButtonColorTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        btnCollection.Add(btnAvailability)
        btnCollection.Add(btnFuel)
        btnCollection.Add(btnEnergy)
    
      End Sub
    
      Private Sub btnButtons_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles btnAvailability.MouseEnter, btnFuel.MouseEnter, _
          btnEnergy.MouseEnter
    
        CType(sender, Button).BackColor = Color.LightBlue
    
      End Sub
    
      Private Sub btnButtons_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles btnAvailability.MouseLeave, btnFuel.MouseLeave, _
          btnEnergy.MouseLeave
    
        If Not btnSelected Is sender Then
          CType(sender, Button).BackColor = Control.DefaultBackColor
        End If
    
      End Sub
    
      Private Sub btnAvailability_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvailability.Click
    
        SetButtonClickColor(sender)
        ' Do other Availability button click events
    
      End Sub
    
      Private Sub btnFuel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFuel.Click
    
        SetButtonClickColor(sender)
        ' Do other Fuel button click events
    
      End Sub
    
      Private Sub btnEnergy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnergy.Click
    
        SetButtonClickColor(sender)
        ' Do other Energy button click events
    
      End Sub
    
      Private Sub SetButtonClickColor(ByVal sender As System.Object)
    
        Dim objButton As Object
    
        For Each objButton In btnCollection
          CType(objButton, Button).BackColor = Control.DefaultBackColor
        Next
    
        CType(sender, Button).BackColor = Color.LightBlue
        btnSelected = sender
    
      End Sub

    Thanks OptionBase1 for the code, it is working but the background color is not what I am looking for

    This is the result for your code:

    Name:  vb2.png
Views: 210
Size:  4.5 KB


    And This is the background I want:

    Name:  vb1.jpg
Views: 160
Size:  11.3 KB

    The second background looks more professional and it will be changed according to the windows theme. How to get this background?

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: How to make the button as selected (background color)?

    Quote Originally Posted by Sadiq6210 View Post
    And This is the background I want:

    Name:  vb1.jpg
Views: 160
Size:  11.3 KB

    The second background looks more professional and it will be changed according to the windows theme. How to get this background?
    You don't need to do anything special, just use a RadioButton set to Button style and let Windows do the rest as you have already shown in this screenshot. That way it looks professional conforming to the user's theme too.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: How to make the button as selected (background color)?

    Quote Originally Posted by JuggaloBrotha View Post
    You don't need to do anything special, just use a RadioButton set to Button style and let Windows do the rest as you have already shown in this screenshot. That way it looks professional conforming to the user's theme too.
    Didn't know about this one. Just confirmed it and it is the best solution. Set the Appearance property of a RadioButton to Button.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to make the button as selected (background color)?

    Quote Originally Posted by JuggaloBrotha View Post
    You don't need to do anything special, just use a RadioButton set to Button style and let Windows do the rest as you have already shown in this screenshot. That way it looks professional conforming to the user's theme too.
    I was coming here to suggest this myself. For CheckBoxes and RadioButtons both, you can set the Appearance property to Button and then control will look exactly like a regular Button. When the Checked property is True, the control will look like a depressed Button, differentiating it from the others. That means that would want to handle the CheckedChanged event instead of the Click event and test that that Checked property was True. It also means that, if you wanted to be able to "unselect" the "button" you would need to uncheck the RadioButton, i.e. set its Checked property to False.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2016
    Posts
    22

    Re: How to make the button as selected (background color)?

    Quote Originally Posted by JuggaloBrotha View Post
    You don't need to do anything special, just use a RadioButton set to Button style and let Windows do the rest as you have already shown in this screenshot. That way it looks professional conforming to the user's theme too.
    Quote Originally Posted by Niya View Post
    Didn't know about this one. Just confirmed it and it is the best solution. Set the Appearance property of a RadioButton to Button.
    Quote Originally Posted by jmcilhinney View Post
    I was coming here to suggest this myself. For CheckBoxes and RadioButtons both, you can set the Appearance property to Button and then control will look exactly like a regular Button. When the Checked property is True, the control will look like a depressed Button, differentiating it from the others. That means that would want to handle the CheckedChanged event instead of the Click event and test that that Checked property was True. It also means that, if you wanted to be able to "unselect" the "button" you would need to uncheck the RadioButton, i.e. set its Checked property to False.
    Great !
    I didn't know that I can use RadioButton as a button.
    I am trying to do this since 2 weeks using the codes, I cannot believe how simple is your solution. Absolutely, this is the best solution

    It is working as a charm, thanks guys for your help and effort.

  10. #10

    Re: How to make the button as selected (background color)?

    Thanks. I am looking something similar. I am still new to visual basic though. Hopefully, I can understand this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width