[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
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:
Dim lstKleur1 As String = Me.<ListBoxName>.SelectedItem.ToString()
Select Case lstKleur1
Case "Geel"
Kleurknop1.BackColor = Color.Yellow
Case "Blauw"
Kleurknop1.BackColor = Color.Blue
Case "Rood"
'//etc...
End Select
Re: when click on listbox a button gets colored
Not working.
i had to adjust it like this to make it work
vb Code:
Dim lstKleur1 As String
Select lstKleur1
Case "Geel"
Kleurknop1.BackColor = Color.Yellow
Case "Blauw"
Kleurknop1.BackColor = Color.Blue
Case "Rood"
Kleurknop1.BackColor = Color.Red
Case "Groen"
Kleurknop1.BackColor = Color.Green
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.
Re: when click on listbox a button gets colored
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).
Re: when click on listbox a button gets colored
aha, that way. thanks mate. problem solved.