Results 1 to 6 of 6

Thread: [RESOLVED] when click on listbox a button gets colored

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    54

    Resolved [RESOLVED] when click on listbox a button gets colored

    hi,
    i have the following question:
    my code:
    Code:
    Private Sub lstKleur1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstKleur1.SelectedIndexChanged
            Dim Geel As String
            Dim Blauw As String
            Dim Groen As String
            Dim Rood As String
            Dim lstKleur1 As String
    
            If lstKleur1 = Rood Then
                Kleurknop1.BackColor = Color.Red
            End If
            If lstKleur1 = Geel Then
                Kleurknop1.BackColor = Color.Yellow
            End If
            If lstKleur1 = Blauw Then
                Kleurknop1.BackColor = Color.Blue
            End If
            If lstKleur1 = Groen Then
                Kleurknop1.BackColor = Color.Green
            End If
        End Sub
    okay, i want the following: there are four items in my listbox: Rood, Groen, Geel, Blauw (these are dutch for red, green, yellow, blue). and when i click one of these, the button (Kleurknop1) needs to get the right colour. but when i click it now, it just keeps getting green, no matter which one i click. so how to solve this?

    i hope i am clearly enough.

    i am using visual basic 2008

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: when click on listbox a button gets colored

    In actual fact, the button is changing all colors, green is just the last one. You have declared 5 strings but they are not set to anything, so they are all Nothing. So when you test them against each other they are all equal.

    You need to assign a value to the string.

    vb.net Code:
    1. Dim lstKleur1 As String = Me.<ListBoxName>.SelectedItem.ToString()
    2.  
    3. Select Case lstKleur1
    4.  
    5.     Case "Geel"
    6.         Kleurknop1.BackColor = Color.Yellow
    7.     Case "Blauw"
    8.         Kleurknop1.BackColor = Color.Blue
    9.     Case "Rood"
    10.         '//etc...
    11. End Select
    Last edited by ForumAccount; Jun 17th, 2010 at 11:39 AM. Reason: End Case > End Select

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    54

    Re: when click on listbox a button gets colored

    Not working.
    i had to adjust it like this to make it work

    vb Code:
    1. Dim lstKleur1 As String
    2.         Select lstKleur1
    3.             Case "Geel"
    4.                 Kleurknop1.BackColor = Color.Yellow
    5.             Case "Blauw"
    6.                 Kleurknop1.BackColor = Color.Blue
    7.             Case "Rood"
    8.                 Kleurknop1.BackColor = Color.Red
    9.             Case "Groen"
    10.                 Kleurknop1.BackColor = Color.Green
    11.         End Select

    the words "Select case" were automatically changed to select
    and end case didn't work. that had to be end select, the program said that.
    also the me.<listboxname> wasn't allowed, because of something xml.
    Last edited by stijn22; Jun 17th, 2010 at 10:41 AM.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    54

    Re: when click on listbox a button gets colored

    oops, a double post.

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: when click on listbox a button gets colored

    Alright, first, End Case was a typo since I didn't do that in the IDE I just typed it here. Second, <ListBoxName> is the name of YOUR ListBox, substitute the name in. Third, you are STILL not assigning a value to lstKleur1. So "working" is not correct, compiles is what it does. What is the name of your ListBox? Change <ListBoxName> to that and then use my previous code (changing the End Case of course).

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    54

    Re: when click on listbox a button gets colored

    aha, that way. thanks mate. problem solved.

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