-
Apr 24th, 2012, 01:58 AM
#1
Thread Starter
Junior Member
[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?
Last edited by Dendari; Apr 24th, 2012 at 07:01 PM.
-
Apr 24th, 2012, 02:14 AM
#2
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
-
Apr 24th, 2012, 02:31 AM
#3
Re: ComboBox.SelectedIndex can't go higher than 9?
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?
-
Apr 24th, 2012, 06:56 AM
#4
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
-
Apr 24th, 2012, 11:23 AM
#5
Re: ComboBox.SelectedIndex can't go higher than 9?
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.
-
Apr 24th, 2012, 12:12 PM
#6
Re: ComboBox.SelectedIndex can't go higher than 9?
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.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Apr 24th, 2012, 05:21 PM
#7
Thread Starter
Junior Member
Re: ComboBox.SelectedIndex can't go higher than 9?
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.
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.
Last edited by Dendari; Apr 24th, 2012 at 08:06 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|