disable the button if combobox item is already selected

mark103

Member
Joined
Apr 13, 2008
Messages
11
Programming Experience
3-5
Hi guys

I have a problem with loops, I have five list of items on the combobox. When I click the item on combobox like "Item 1", the button enable set to true which it easy to write so when I point down on the list on combobox and pick "Item 2" to set the button enable to true. If I return to "Item 1" and the button enable set to true once again but if I select "Item 1" again that have already been selected, the button enable should set to false. How can I block the loops of getting pass in each of those lines??



Here it the code:

VB.NET:
          If Combobox1.SelectedItem = "Item 2" Then
                If Combobox1.SelectedItem = "Item 1" Then
                Button1.Enabled = True
          Else
          If Combobox1.SelectedItem = "Item 1" Then
                If Combobox1.SelectedItem = "Item 1" Then
                Button1.Enabled = False
          End If
     End If



Hope you guys will be able to advance me with this.



Thanks,
Mark
 
I think your indentation is a bit messed up... It will run like this :

VB.NET:
If Combobox1.SelectedItem = "Item 2" Then
    If Combobox1.SelectedItem = "Item 1" Then
        Button1.Enabled = True
    Else
        If Combobox1.SelectedItem = "Item 1" Then
            If Combobox1.SelectedItem = "Item 1" Then
                Button1.Enabled = False
            End If
        End If

Now it's clearer that this code won't do much (and there must be 2 "end if" after that somewhere)... The SelectedItem would have to be both "Item 1" and "Item 2" at the same time to do anything in this!

From what I understand, you want that the first time an item is selected, the button is enabled and any subsequent time that same item is selected, the button is disabled. Correct?

To do that, you'd have to keep a Dictionary with the item as a key and the number of times it was selected as value. Then you see how many times the current item was selected and enable/disable the button as needed. Then as long as the user selects a same item less than 2,147,483,647 times, things should work fine (Windows users should not worry because their computer is likely to randomly crash before they had the time to do that ;) )...
 
The code you post do not work, when I click Item1 and the button should set to true but if I click item 1 again then button should set enable to false.




Hope you can help!!!!


Thanks,
Mark
 
Last edited:
Try something like this:
VB.NET:
Button1.Enabled = Combobox1.SelectedItem = "Item 1"
or
VB.NET:
Button1.Enabled = Combobox1.Text = "Item 1"
 
Back
Top