pharaon
pharaon

Reputation: 9

combobox selected index between two values

how can I make select case for index between two values like

Select Case True
        Case ComboBox1.SelectedIndex >= 0 and <= 3
            messagebox.show("Done")
   End Select

Upvotes: 1

Views: 888

Answers (1)

Steve
Steve

Reputation: 216293

Your answer is in the MSDN documentation about SELECT ... CASE

Dim index = ComboBox1.SelectedIndex 
Select Case index
    Case 0 To 3
        MessageBox.Show("Done")
    Case Else
        MessageBox.Show("What? Else??")
End Select

EDIT
An interesting situation is the following that has let me without a clear explanation, so if someone could add to this answer is well accepted: Suppose to have this code.... (and OPTION STRICT OFF)

Sub Main
    Dim cbo = new ComboBOx()
    cbo.Items.Add("Steve")
    cbo.Items.Add("John")
    cbo.Items.Add("Mark")
    cbo.Items.Add("Andrew")
    cbo.Items.Add("Luke")
    cbo.Items.Add("Robert")
    cbo.Items.Add("Max")
    cbo.Items.Add("Carl")
    cbo.Items.Add("Paul")  ' added 9 items
    cbo.SelectedIndex = 0  ' set Steve as selected
    Select cbo.SelectedIndex
        case cbo.SelectedIndex = 2 to 3
            Console.WriteLine("DONE 2-3")
        case cbo.SelectedIndex = 0 to 1
            Console.WriteLine("DONE 0-1")
        case else
            Console.WriteLine("ELSE")
        End Select
End Sub

I would expect the DONE 0-1 message but instead this code returns DONE 2-3, as well for every other SelectedIndex less than 4. It returns ELSE only when setting the SelectedIndex to 4.
I have not found a way to return DONE 0-1 unless moving the case block 0-1 before the 2-3 one.

Upvotes: 3

Related Questions