locked
How to use a Button text as a colour value RRS feed

  • Question

  • So I've got a set of buttons with colours as their names Eg. A button that says red, a button that says yellow.

    I want to make it so that when I press that button, it's backColor changes to whatever the button says but I don't want to have to go through each button like so:

    Button16.BackColor = color.red
    Button17.BackColor = color.Yellow

    I'd like to have something that I can just paste into each button code without having to change it like:

    ThisButton.BackColor = Color.Button.Text
    I know that doesn't work, but is there a way I can achieve this?

    Friday, October 16, 2015 7:09 AM

Answers

  • If you are using my suggestion then it's very easy.

    The form load event handler shows how to loop through all the buttons in the TableLayoutPanel.  Simply set Btn.BackColor to transparent within the loop.

       Private Sub btbResetButtonBackColors_Click(sender As System.Object, e As System.EventArgs) Handles btbResetButtonBackColors.Click
          For Each Btn As Control In TableLayoutPanel1.Controls
             Btn.BackColor = Color.Transparent
          Next
       End Sub
    

    Actually as far as I'm aware Buttons don't support transparent backcolor so setting it to Transparent is the same as setting it to Nothing.

    Friday, October 16, 2015 10:02 PM

All replies

  • If you mean changing the background colour of pressed button, then add a single function (manually) that handles several buttons, for example:

    Private Sub ButtonClicked(sender As Object, e As EventArgs) Handles Button16.Click, Button17.Click, …
        Dim button = CType(sender, Button)
        button.BackColor = Color.FromName(button.Text)
    End Sub
    

    Friday, October 16, 2015 8:11 AM
  • >.< I keep forgetting important details. I have it set that there's rows of buttons. When I press one button, the others in the row need their background colours to go transparent. So, I press red and its background turns red, then I press yellow and its background turns yellow and red's background turns transparent. Would I be able to implement that here? 
    Friday, October 16, 2015 8:26 AM
  • Try this:

    Private Sub ButtonClicked(sender As Object, e As EventArgs) Handles Button16.Click, Button17.Click, ...
        Dim clicked_button = CType(sender, Button)
    
        For Each b In {Button16, Button17, ...}
            b.BackColor = If(b Is clicked_button, Color.FromName(b.Text), SystemColors.Control)
        Next
    End Sub



    • Edited by Viorel_MVP Friday, October 16, 2015 8:33 AM
    Friday, October 16, 2015 8:32 AM
  • This works, but it resets the colour of every button, not just the ones in it's row. To achieve that, should I have one of those blocks of code for each row and just include the buttons of that row?
    Friday, October 16, 2015 9:02 AM
  • If there are several rows, then create similar routines (having different names).

    It is also possible to declare a matrix in your form, such as:

    Private buttons As Button(,) = {{Button11, Button12, Button13, . . .}, _
                                    {Button21, Button22, Button23, . . .}, _
                                    . . . }


    Then solve the problem processing the matrix (adding a unique Click event handler, determining the clicked row, etc.)



    • Edited by Viorel_MVP Friday, October 16, 2015 9:34 AM
    Friday, October 16, 2015 9:32 AM
  • A neat way of doing this would be to put all your buttons into a TableLayoutPanel.  With the following code you could then add and remove buttons from the panel if required without having to change any code.

       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          For Each Btn As Control In TableLayoutPanel1.Controls
             AddHandler Btn.Click, AddressOf ChangeColour
          Next
       End Sub
       Private Sub ChangeColour(sender As Object, e As EventArgs)
          Dim Btn As Button = CType(sender, Button)
          Dim Position As TableLayoutPanelCellPosition = TableLayoutPanel1.GetCellPosition(CType(sender, Control))
          Dim Row As Integer = Position.Row
          For Column As Integer = 0 To TableLayoutPanel1.ColumnCount - 1
             TableLayoutPanel1.GetControlFromPosition(Column, Row).BackColor = Color.Transparent
          Next
          Btn.BackColor = Color.FromName(Btn.Text)
       End Sub

    Friday, October 16, 2015 12:14 PM
  • That is too complex for my skills sadly. I like to know what I'm putting in, rather than putting in pieces of code without having a clue what it means
    Friday, October 16, 2015 2:51 PM
  • Which part would I rename? The "ButtonClicked" part? 

    Also I've got no idea what's going on with a matrix and such.

    Friday, October 16, 2015 2:52 PM
  • That is too complex for my skills sadly. I like to know what I'm putting in, rather than putting in pieces of code without having a clue what it means

    You won't advance very far if you never stretch yourself.  It's actually a lot easier than you probably realise.  Have a look at the following version which I've commented to try and help.

    Public Class Form1
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          ' the following code just adds the same event handler to every button in the TableLayoutPanel
          ' it's equivalent to adding "Handles Button1.Click, Button2.Click, Button3.Click etc to the handler sub
          For Each Btn As Control In TableLayoutPanel1.Controls
             AddHandler Btn.Click, AddressOf ChangeColour
          Next
       End Sub
       Private Sub ChangeColour(sender As Object, e As EventArgs)  ' handles all button click events
          ' Cast the sender object to type Button so that its properties can be used.
          Dim Btn As Button = CType(sender, Button)
          ' Get the position of the cell in the TableLayoutPanel which contains the button
          Dim Position As TableLayoutPanelCellPosition = TableLayoutPanel1.GetCellPosition(Btn)
          ' Get the row number for the cell
          Dim Row As Integer = Position.Row
          ' Set all the buttons in that row to have a transparent backcolour
          For Column As Integer = 0 To TableLayoutPanel1.ColumnCount - 1
             TableLayoutPanel1.GetControlFromPosition(Column, Row).BackColor = Color.Transparent
          Next
          ' then set the clicked button to the colour defined by its Text property.
          Btn.BackColor = Color.FromName(Btn.Text)
       End Sub
    End Class
    

    • Marked as answer by SilverEyes_ Friday, October 16, 2015 5:01 PM
    • Unmarked as answer by SilverEyes_ Friday, October 16, 2015 5:44 PM
    Friday, October 16, 2015 4:19 PM
  • Thanks for adding the comments, it's helped a tonne for me to understand what's going on. I'm fine with stretching myself, I just like to know what I'm doing so I can learn from it. I block of code that I don't understand isn't going to help me learn. 
    Friday, October 16, 2015 5:00 PM
  • I actually have one more question, how would I add a button to reset all of the buttons easily back to transparent backgrounds.
    Friday, October 16, 2015 5:14 PM
  • That is too complex for my skills sadly. I like to know what I'm putting in, rather than putting in pieces of code without having a clue what it means

    The New Boston Videos

    Microsoft Virtual Academy - VB fundamentals for absolute beginners

    Visual Basic

    Getting Started Tutorials


    La vida loca

    Friday, October 16, 2015 8:17 PM
  • Which part would I rename? The "ButtonClicked" part? 

    Also I've got no idea what's going on with a matrix and such.

    Getting your buttons into a matrix is just the start.  You also need to be able to identify the row that the clicked button belongs to, then change your updating into a loop that addresses only the buttons in that row. You can store the row for any button in its Tag property:

    For I As Integer = 0 to 4
      For J As Integer = 0 to 5
        buttons(I,J).Tag = I
      Next J
    Next I

    Then use a loop to loop through that row only:
        Dim Row As Integer = CType(clicked_button.Tag, Integer)
        For
    J As Integer = 0 to 5
            Dim b As Button = buttons(Row,J)
            b.BackColor = If(b Is clicked_button, Color.FromName(b.Text), SystemColors.Control)
       
    Next J

    Friday, October 16, 2015 8:38 PM
  • If you are using my suggestion then it's very easy.

    The form load event handler shows how to loop through all the buttons in the TableLayoutPanel.  Simply set Btn.BackColor to transparent within the loop.

       Private Sub btbResetButtonBackColors_Click(sender As System.Object, e As System.EventArgs) Handles btbResetButtonBackColors.Click
          For Each Btn As Control In TableLayoutPanel1.Controls
             Btn.BackColor = Color.Transparent
          Next
       End Sub
    

    Actually as far as I'm aware Buttons don't support transparent backcolor so setting it to Transparent is the same as setting it to Nothing.

    Friday, October 16, 2015 10:02 PM
  • Thank you. I'll definitely check these out 
    Saturday, October 17, 2015 4:06 PM
  • Ahh, that makes sense. Thank you
    Saturday, October 17, 2015 4:06 PM