Greg
Greg

Reputation: 55

Don't run code is same item is selected from ComboBox

So, I got a combobox, and when you select an item from it, it enables a couple of buttons.

btnConvert.Enabled = True

Basically, I dont want that code to run if the user opens the combobox and selects the same item. I've tried dimming a new boolean and all that, trying to make somewhat make this possible, but it didnt go out to well. Is there a simple way to block that code if same selection is made?

Upvotes: 0

Views: 711

Answers (1)

Jeremy K
Jeremy K

Reputation: 543

Here are two different options depending on your needs. Pick the one that you need. I'm using the SelectedIndexChanged event of the ComboBox to demonstrate the idea. This event runs the code inside its method whenever an item is chosen from the ComboBox. Here and here are more information on using events if you are not familiar with them.

Option 1

If your items in the ComboBox are always in the same order, then you can store the selected items index in a variable. Then you use that variable in the if statement to check and make sure the next selected item is not the same.

' The variable we'll use to store the most recent selected index
Private _selectedItem As Integer = -1 

' The method set up to be run when an item in ComboBox1 is selected
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ' Get the combobox that triggered this event (ie: ComboBox1)
    Dim comboBox As ComboBox = CType(sender, ComboBox)

    ' Get the index of the selected item
    Dim selectedIndex As Integer = comboBox.SelectedIndex

    ' Check that the selected index > -1 and that it is also not the same as the last one
    If selectedIndex > -1 AndAlso selectedIndex <> _selectedItem Then
        ' Update the variable with the most recent selected index
        _selectedItem = selectedIndex

        ' Enable/disable the buttons
        Button1.Enabled = False
        Button2.Enabled = True
        Button3.Enabled = True
    End If
End Sub

Option 2

If your items in the ComboBox aren't in the same order each time, but are uniquely named, then you can store the item text in a variable. Then you use that variable in the if statement to check and make sure the next selected item is not the same.

' The variable we'll use to store the most recent selected text
Private _selectedItem As String

' The method set up to be run when an item in ComboBox1 is selected
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ' Get the combobox that triggered this event (ie: ComboBox1)
    Dim comboBox As ComboBox = CType(sender, ComboBox)

    ' Get the index of the selected item
    Dim selectedIndex As Integer = comboBox.SelectedIndex

    ' Get the text for that item
    Dim itemName As String = comboBox.Items(selectedIndex)

    ' Check that the selected index > -1 and that the text is also not the same as the last one
    If selectedIndex > -1 AndAlso Not itemName.Equals(_selectedItem) Then
        ' Update the variable with the most recent selected text
        _selectedItem = itemName

        ' Enable/disable the buttons
        Button1.Enabled = False
        Button2.Enabled = True
        Button3.Enabled = True
    End If
End Sub

Upvotes: 1

Related Questions