Results 1 to 7 of 7

Thread: [RESOLVED] ComboBox.SelectedIndex can't go higher than 9?

  1. #1

    Thread Starter
    Junior Member Dendari's Avatar
    Join Date
    Feb 2012
    Posts
    23

    Resolved [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:
    1. If Me.ComboBox1.SelectedIndex = 0 Then
    2.     MsgBox("item 1")
    3. ElseIf Me.ComboBox1.SelectedIndex = 1 Then
    4.     MsgBox("item 2")
    5.     '...
    6. ElseIf Me.ComboBox1.SelectedIndex = 9 Then
    7.     MsgBox("item 9")
    8. ElseIf Me.ComboBox1.SelectedIndex = 10 Then
    9.     MsgBox("item 10")
    10.     '...
    11. 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.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: ComboBox.SelectedIndex can't go higher than 9?

    What does this show?

    vb Code:
    1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _  
    2.                                    Handles ComboBox1.SelectedIndexChanged
    3.      MessageBox.Show("item " & ComboBox1.SelectedIndex.ToString())
    4. End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: ComboBox.SelectedIndex can't go higher than 9?

    Quote Originally Posted by Dendari View Post
    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?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: ComboBox.SelectedIndex can't go higher than 9?

    Quote Originally Posted by jmcilhinney View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: ComboBox.SelectedIndex can't go higher than 9?

    Quote Originally Posted by Niya View Post
    @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.

  7. #7

    Thread Starter
    Junior Member Dendari's Avatar
    Join Date
    Feb 2012
    Posts
    23

    Re: ComboBox.SelectedIndex can't go higher than 9?

    Quote Originally Posted by cicatrix View Post
    What does this show?

    vb Code:
    1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _  
    2.                                    Handles ComboBox1.SelectedIndexChanged
    3.      MessageBox.Show("item " & ComboBox1.SelectedIndex.ToString())
    4. End Sub
    It show me the current item correctly.

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width