Selecting an item in a combo box with code - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Selecting an item in a combo box with code

MichaelL65MichaelL65 Posts: 14Member
It would appear from the Visual Studio help file that if I use the code:

[code] ComboBox1.SelectedIndex = index [/code]
That the combo box would display the item in it's list that coresponds to the number in 'index.' That doesn't seem to work.

I've also tried:

[code] ComboBox1.SelectedItem = strItem[/code]
and

[code] ComboBox1.Test = strItem[/code]
Where strItem is known to be a item in the combo box's list. They don't seem to work either.

Am I missing a step somewhere?

The combo box is to allow the user to select an item from the list, so it's set up to DropDownList mode. The list items are from a dataset. When I run the code I have, everything about the combo box works exactly as I'm expecting it to except for this one problem.

The reason I want this is the user can go back to here and edit their choice so I want it to default back to what they selected earlier.

Any suggestions?

Comments

  • dokken2dokken2 Posts: 532Member
    : It would appear from the Visual Studio help file that if I use the code:
    :
    : [code] ComboBox1.SelectedIndex = index [/code]
    : That the combo box would display the item in it's list that coresponds to the number in 'index.' That doesn't seem to work.
    :
    : I've also tried:
    :
    : [code] ComboBox1.SelectedItem = strItem[/code]
    : and
    :
    : [code] ComboBox1.Test = strItem[/code]
    : Where strItem is known to be a item in the combo box's list. They don't seem to work either.
    :
    : Am I missing a step somewhere?
    :
    : The combo box is to allow the user to select an item from the list, so it's set up to DropDownList mode. The list items are from a dataset. When I run the code I have, everything about the combo box works exactly as I'm expecting it to except for this one problem.
    :
    : The reason I want this is the user can go back to here and edit their choice so I want it to default back to what they selected earlier.
    :
    : Any suggestions?
    :
    :


    You should be able to use either the SelectedItem or SelectedIndex property-

    [code]

    'add items to combo
    Me.ComboBox1.Items.Add("A")
    Me.ComboBox1.Items.Add("B")
    Me.ComboBox1.Items.Add("C")
    Me.ComboBox1.Items.Add("D")

    'select item [case sensitive!]
    Me.ComboBox1.SelectedItem = "A"

    'select index
    Me.ComboBox1.SelectedIndex = 0

    'select last item
    Me.ComboBox1.SelectedIndex = Me.ComboBox1.Items.Count - 1
    [/code]

  • aamirwaziraamirwazir Posts: 108Member
    [code]
    Me.ComboBox1.SelectedItem = Me.ComboBox1.Items.IndexOf(stritem)
    [/code]
    try this
  • MichaelL65MichaelL65 Posts: 14Member
    : [code]
    : Me.ComboBox1.SelectedItem = Me.ComboBox1.Items.IndexOf(stritem)
    : [/code]
    : try this
    :

    I'm pretty sure the only difference between what either of you have told me and what I've tried is putting the "Me." at the beginning. Is it that significant? I haven't run into the need to use it except when referring to form properties and functions (and it isn't in the help file either, but then they couldn't sell all the books :-)). I'll give it a try.
  • MichaelL65MichaelL65 Posts: 14Member
    [b][red]This message was edited by MichaelL65 at 2005-12-8 11:21:53[/red][/b][hr]
    Well, that didn't change anything. At least now I know it's really supposed to work that way, which means I've got something else incorrectly set up somewhere. :-(

    EDIT:

    Found the problem. Turns out the routine was being called before the data for the ComboBox was set. D'oh!

    Thanks though for confirming that I was doing that part right to begin with. Got me to take off my stupid hat.
  • lethalmarclethalmarc dubaiPosts: 1Member
    edited February 10
        ComboBox1.Items.Add("Sunday")
        ComboBox1.Items.Add("Monday")
        ComboBox1.Items.Add("Tuesday")
        ComboBox1.Items.Add("wednesday")
        ComboBox1.Items.Add("Thursday")
        ComboBox1.Items.Add("Friday")
        ComboBox1.Items.Add("Saturday")
    
        ComboBox1.SelectedItem = ComboBox1.Items(3)
    

    full source : http://vb.net-informations.com/gui/vb.net-combobox.htm

    marc.

Sign In or Register to comment.