[RESOLVED] ComboBox.SelectedIndex can't go higher than 9?
Hello everyone.
I'm working with a ComboBox which has 14 items.
Using the following code I got a problem
vb Code:
If Me.ComboBox1.SelectedIndex = 0 Then
MsgBox("item 1")
ElseIf Me.ComboBox1.SelectedIndex = 1 Then
MsgBox("item 2")
'...
ElseIf Me.ComboBox1.SelectedIndex = 9 Then
MsgBox("item 9")
ElseIf Me.ComboBox1.SelectedIndex = 10 Then
MsgBox("item 10")
'...
End If
The problem is that after the SelectedIndex = 9 it won't read properly the SelectedIndex = 10, it seems like it consider only the first number (1) instead of the whole one (10). In fact get the same result as if it was SelectedIndex = 1
What am I doing wrong?
Re: ComboBox.SelectedIndex can't go higher than 9?
What does this show?
vb Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles ComboBox1.SelectedIndexChanged
MessageBox.Show("item " & ComboBox1.SelectedIndex.ToString())
End Sub
Re: ComboBox.SelectedIndex can't go higher than 9?
Quote:
Originally Posted by
Dendari
Hello everyone.
I'm working with a ComboBox which has 14 items.
Using the following code I got a problem
Code:
If Me.ComboBox1.SelectedIndex = 0 Then
MsgBox("item 1")
ElseIf If Me.ComboBox1.SelectedIndex = 1 Then
MsgBox("item 2")
'...
ElseIf If Me.ComboBox1.SelectedIndex = 9 Then
MsgBox("item 9")
ElseIf If Me.ComboBox1.SelectedIndex = 10 Then
MsgBox("item 10")
'...
End If
The problem is that after the SelectedIndex = 9 it won't read properly the SelectedIndex = 10, it seems like it consider only the first number (1) instead of the whole one (10). In fact get the same result as if it was SelectedIndex = 1
What am I doing wrong?
Don't you have too many If's in there? Is that just a typo? If not then I'm guessing that that has something to do with the problem. If it is then why are you typing at all and not copying and pasting?
Re: ComboBox.SelectedIndex can't go higher than 9?
this is one of those cases where a Select Case would probably be the better mouse trap.
-tg
Re: ComboBox.SelectedIndex can't go higher than 9?
Quote:
Originally Posted by
jmcilhinney
Don't you have too many If's in there? Is that just a typo? If not then I'm guessing that that has something to do with the problem. If it is then why are you typing at all and not copying and pasting?
Yeah, you're right. My IDE is identifying that If after the ElseIf as the If function which does something different.
@Op
Remove that extra If after your ElseIf. In fact it would be better to follow tech's advice and use Select Case instead.
Re: ComboBox.SelectedIndex can't go higher than 9?
Quote:
Originally Posted by
Niya
@Op
Remove that extra If after your ElseIf. In fact it would be better to follow tech's advice and use Select Case instead.
I would advise to follow cicatrix's advice of concatenating the SelectedIndex and forgetting all of the If-Then-Else or Select-Case statements altogether.
If he's planning on having different code for each different index then Select-Case would be a better choice for readability and maintainability.
Re: ComboBox.SelectedIndex can't go higher than 9?
Quote:
Originally Posted by
cicatrix
What does this show?
vb Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles ComboBox1.SelectedIndexChanged
MessageBox.Show("item " & ComboBox1.SelectedIndex.ToString())
End Sub
It show me the current item correctly.
Quote:
Originally Posted by
jmcilhinney
Don't you have too many If's in there? Is that just a typo? If not then I'm guessing that that has something to do with the problem. If it is then why are you typing at all and not copying and pasting?
Yes that was a typo when I wrote it here I didn't copy it directly from the source code.
EDIT: find the error, resolved it.