ComboBox Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikolay Petrov

    ComboBox Question

    I have a Combo box, binded to a dataset
    With cmbCompany
    .DataSource = dsSpecContact
    .DisplayMember = "Companies.Comp anyName"
    .ValueMember = "Companies.Comp anyID"
    End With

    What I want is to change the SelectedIndex of this Combo Box by using the
    ValueMember or DisplayMemver values. I mean the "Companies.Comp anyID" or
    "Companies.Comp anyName".

    Some how I have to iterate through all the item in Combo Box and find which
    item has ValueMember = "Companies.Comp anyID" or DisplayMember =
    "Companies.Comp anyName" and select it.

    Does anyone know how to do that, couse I can't figure it out



    TIA


  • Imran Koradia

    #2
    Re: ComboBox Question

    You could use a hashtable to store the index-ID mappings. something like:

    For Each oCompany as Company In Companies
    oHash.Add(oComp any.CompanyID, cmbCompany.Item s.IndexOf(oComp any))
    Next oCompany

    When you need the index for a given CompanyID, you just do:

    Dim index As Integer = CType(oHash.Ite m(CompanyID), Integer)

    This would work fine for a fixed combobox where you're not going to be
    adding-removing items at runtime. If you are going to be adding-removing
    items, you'll need to update the hashtable to reflect these changes.
    Also, for a combobox with not a lot of items (probably 10 to 15 items), it
    wouldn't be too bad to loop through the combobox to retrieve the index since
    populating and maintaining the hashtable does add some overhead anyway.

    hope this helps..
    Imran.

    "Nikolay Petrov" <johntup2@mail. bg> wrote in message
    news:uYFpGeblEH A.324@TK2MSFTNG P11.phx.gbl...[color=blue]
    > I have a Combo box, binded to a dataset
    > With cmbCompany
    > .DataSource = dsSpecContact
    > .DisplayMember = "Companies.Comp anyName"
    > .ValueMember = "Companies.Comp anyID"
    > End With
    >
    > What I want is to change the SelectedIndex of this Combo Box by using the
    > ValueMember or DisplayMemver values. I mean the "Companies.Comp anyID" or
    > "Companies.Comp anyName".
    >
    > Some how I have to iterate through all the item in Combo Box and find[/color]
    which[color=blue]
    > item has ValueMember = "Companies.Comp anyID" or DisplayMember =
    > "Companies.Comp anyName" and select it.
    >
    > Does anyone know how to do that, couse I can't figure it out
    >
    >
    >
    > TIA
    >
    >[/color]


    Comment

    • Nikolay Petrov

      #3
      Re: ComboBox Question

      How to loop through the Combo Box items.
      I know only the following method:
      Dim itm as object
      for each itm in ComboBox1.items
      ....
      ....
      next

      But this method does not provide me a way to access items properties,
      becouse they are defined as Object

      "Imran Koradia" <nospam@microso ft.com> wrote in message
      news:%23scrUubl EHA.712@TK2MSFT NGP09.phx.gbl.. .[color=blue]
      > You could use a hashtable to store the index-ID mappings. something like:
      >
      > For Each oCompany as Company In Companies
      > oHash.Add(oComp any.CompanyID, cmbCompany.Item s.IndexOf(oComp any))
      > Next oCompany
      >
      > When you need the index for a given CompanyID, you just do:
      >
      > Dim index As Integer = CType(oHash.Ite m(CompanyID), Integer)
      >
      > This would work fine for a fixed combobox where you're not going to be
      > adding-removing items at runtime. If you are going to be adding-removing
      > items, you'll need to update the hashtable to reflect these changes.
      > Also, for a combobox with not a lot of items (probably 10 to 15 items), it
      > wouldn't be too bad to loop through the combobox to retrieve the index[/color]
      since[color=blue]
      > populating and maintaining the hashtable does add some overhead anyway.
      >
      > hope this helps..
      > Imran.
      >
      > "Nikolay Petrov" <johntup2@mail. bg> wrote in message
      > news:uYFpGeblEH A.324@TK2MSFTNG P11.phx.gbl...[color=green]
      > > I have a Combo box, binded to a dataset
      > > With cmbCompany
      > > .DataSource = dsSpecContact
      > > .DisplayMember = "Companies.Comp anyName"
      > > .ValueMember = "Companies.Comp anyID"
      > > End With
      > >
      > > What I want is to change the SelectedIndex of this Combo Box by using[/color][/color]
      the[color=blue][color=green]
      > > ValueMember or DisplayMemver values. I mean the "Companies.Comp anyID"[/color][/color]
      or[color=blue][color=green]
      > > "Companies.Comp anyName".
      > >
      > > Some how I have to iterate through all the item in Combo Box and find[/color]
      > which[color=green]
      > > item has ValueMember = "Companies.Comp anyID" or DisplayMember =
      > > "Companies.Comp anyName" and select it.
      > >
      > > Does anyone know how to do that, couse I can't figure it out
      > >
      > >
      > >
      > > TIA
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nikolay Petrov

        #4
        Re: ComboBox Question

        Also I don't understand

        For Each oCompany as Company In Companies
        oHash.Add(oComp any.CompanyID, cmbCompany.Item s.IndexOf(oComp any))
        Next oCompany

        Could you please declare items oCompany, Company, Companies



        "Imran Koradia" <nospam@microso ft.com> wrote in message
        news:%23scrUubl EHA.712@TK2MSFT NGP09.phx.gbl.. .[color=blue]
        > You could use a hashtable to store the index-ID mappings. something like:
        >
        > For Each oCompany as Company In Companies
        > oHash.Add(oComp any.CompanyID, cmbCompany.Item s.IndexOf(oComp any))
        > Next oCompany
        >
        > When you need the index for a given CompanyID, you just do:
        >
        > Dim index As Integer = CType(oHash.Ite m(CompanyID), Integer)
        >
        > This would work fine for a fixed combobox where you're not going to be
        > adding-removing items at runtime. If you are going to be adding-removing
        > items, you'll need to update the hashtable to reflect these changes.
        > Also, for a combobox with not a lot of items (probably 10 to 15 items), it
        > wouldn't be too bad to loop through the combobox to retrieve the index[/color]
        since[color=blue]
        > populating and maintaining the hashtable does add some overhead anyway.
        >
        > hope this helps..
        > Imran.
        >
        > "Nikolay Petrov" <johntup2@mail. bg> wrote in message
        > news:uYFpGeblEH A.324@TK2MSFTNG P11.phx.gbl...[color=green]
        > > I have a Combo box, binded to a dataset
        > > With cmbCompany
        > > .DataSource = dsSpecContact
        > > .DisplayMember = "Companies.Comp anyName"
        > > .ValueMember = "Companies.Comp anyID"
        > > End With
        > >
        > > What I want is to change the SelectedIndex of this Combo Box by using[/color][/color]
        the[color=blue][color=green]
        > > ValueMember or DisplayMemver values. I mean the "Companies.Comp anyID"[/color][/color]
        or[color=blue][color=green]
        > > "Companies.Comp anyName".
        > >
        > > Some how I have to iterate through all the item in Combo Box and find[/color]
        > which[color=green]
        > > item has ValueMember = "Companies.Comp anyID" or DisplayMember =
        > > "Companies.Comp anyName" and select it.
        > >
        > > Does anyone know how to do that, couse I can't figure it out
        > >
        > >
        > >
        > > TIA
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...