Results 1 to 5 of 5

Thread: Change button color with button names in ComboBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    117

    Change button color with button names in ComboBox

    I have a lot of buttons.
    All names of button placed in ComboBox.
    How to select one item, and only button with this name make different color ?

    Thanks,

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Change button color with button names in ComboBox

    Here is an example of finding the correct button and changing it's BackColor,
    Code:
            For Each ctl As Control In Me.Controls
                If TypeOf (ctl) Is System.Windows.Forms.Button Then
                    If ctl.Name = Combobox1.Text Then
                        ctl.BackColor = Color.Yellow
                    End If
                End If
            Next
    You will have to adjust the code to work with your form.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    117

    Re: Change button color with button names in ComboBox

    But, this code can't find button with my (custom) names ?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Change button color with button names in ComboBox

    Quote Originally Posted by huja View Post
    But, this code can't find button with my (custom) names ?
    wes4dbt's example loops through each control in the control collection for the active form, checks if the control is a button, then checks if the button's name matches the text in the ComboBox. If all of these conditions are met then it set's the control's color. If this is not the behavior that you want, then what is?

    Also, for what it is worth, here is a more concise version of wes4dbt's example:
    Code:
    Dim ctl As Button = Me.Controls.OfType(Of Button)().FirstOrDefault(Function(b) b.Name = ComboBox1.Text)
    If ctl IsNot Nothing Then
        ctl.BackColor = Color.Yellow
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Change button color with button names in ComboBox

    try this:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.AddRange(Array.ConvertAll(Me.Controls.OfType(Of Button).Reverse.ToArray, Function(b) b.Name))
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            For Each b As Button In Me.Controls.OfType(Of Button)()
                b.BackColor = SystemColors.Control
            Next
            If ListBox1.SelectedIndex = -1 Then Return
            Me.Controls(ListBox1.Text).BackColor = Color.Red
        End Sub
    
    End Class

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