Combo Box

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

    Combo Box

    I have asked a question before some time, and a guy responded to it. His
    answer raised new questions and I replied them back, but still no answer. I
    need them urgently so I am to post the whole thing again. I've found that
    nobody looks at the older threads ;-(

    My Question was:
    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.


    And the answer:
    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.


    And my two new questions:
    1.
    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 someone declare items oCompany, Company, Companies?


    2.
    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,
    because they are defined as Object




  • Cor Ligthert

    #2
    Re: Combo Box

    "Nikolay Petrov"
    [color=blue]
    > I have asked a question before some time, and a guy responded to it. His
    > answer raised new questions and I replied them back, but still no answer.[/color]
    I[color=blue]
    > need them urgently so I am to post the whole thing again. I've found that
    > nobody looks at the older threads ;-([/color]

    Maybe the last sentence is in another context. However when it is about
    people who answer you, than this is absolute not true. Most regulars in this
    newsgroup look at the older threads when they are involved So nobody is a
    little bit overdone. However maybe is what you mean something else and than
    probably I agree with you.

    I look now at your question and the answer. In my opinion was a better
    answer to tell you that there is a combobox.findex act and even better
    combobox.findst ringexact and that you can use for the rest the normal set
    selectedvalue



    It is much easier, see this sample beneath I made for you about this, the
    start is making the table.

    I hope this helps you?

    Cor

    \\\needs one combobox and 2 buttons on a form.
    Private dt As New DataTable
    Private Sub Form2_Load(ByVa l sender As Object, _
    ByVal e As System.EventArg s) Handles MyBase.Load
    dt.Columns.Add( "CompanyNam e")
    dt.Columns.Add( "CompanyId" )
    For i As Integer = 0 To 5
    Dim dr As DataRow = dt.NewRow
    dr(0) = ChrW(i + 65)
    dr(1) = i + 1
    dt.Rows.Add(dr)
    Next
    ComboBox1.DataS ource = dt
    ComboBox1.Displ ayMember = "CompanyNam e"
    ComboBox1.Value Member = "CompanyId"
    End Sub

    Private Sub Button1_Click(B yVal sender As _
    System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    ComboBox1.Selec tedValue = 3
    If ComboBox1.Selec tedIndex <> -1 Then
    MessageBox.Show (ComboBox1.Text )
    End If
    End Sub
    Private Sub Button2_Click(B yVal sender As System.Object, _
    ByVal e As System.EventArg s) Handles Button2.Click
    Dim myindex As Integer = ComboBox1.FindS tringExact("D")
    If myindex <> -1 Then
    ComboBox1.Selec tedIndex = myindex
    MessageBox.Show (ComboBox1.Sele ctedValue.ToStr ing)
    End If
    End Sub
    End Class
    ///

    [color=blue]
    >
    > My Question was:
    > 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.
    >
    >
    > And the answer:
    > 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.
    >
    >
    > And my two new questions:
    > 1.
    > 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 someone declare items oCompany, Company, Companies?
    >
    >
    > 2.
    > 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,
    > because they are defined as Object
    >
    >
    >
    >[/color]


    Comment

    • Nikolay Petrov

      #3
      Re: Combo Box

      That works fine, thanks Cor


      "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
      news:ui3%23W$jl EHA.3104@TK2MSF TNGP14.phx.gbl. ..[color=blue]
      > "Nikolay Petrov"
      >[color=green]
      > > I have asked a question before some time, and a guy responded to it. His
      > > answer raised new questions and I replied them back, but still no[/color][/color]
      answer.[color=blue]
      > I[color=green]
      > > need them urgently so I am to post the whole thing again. I've found[/color][/color]
      that[color=blue][color=green]
      > > nobody looks at the older threads ;-([/color]
      >
      > Maybe the last sentence is in another context. However when it is about
      > people who answer you, than this is absolute not true. Most regulars in[/color]
      this[color=blue]
      > newsgroup look at the older threads when they are involved So nobody is a
      > little bit overdone. However maybe is what you mean something else and[/color]
      than[color=blue]
      > probably I agree with you.
      >
      > I look now at your question and the answer. In my opinion was a better
      > answer to tell you that there is a combobox.findex act and even better
      > combobox.findst ringexact and that you can use for the rest the normal set
      > selectedvalue
      >
      >[/color]
      http://msdn.microsoft.com/library/de...exacttopic.asp[color=blue]
      >
      > It is much easier, see this sample beneath I made for you about this, the
      > start is making the table.
      >
      > I hope this helps you?
      >
      > Cor
      >
      > \\\needs one combobox and 2 buttons on a form.
      > Private dt As New DataTable
      > Private Sub Form2_Load(ByVa l sender As Object, _
      > ByVal e As System.EventArg s) Handles MyBase.Load
      > dt.Columns.Add( "CompanyNam e")
      > dt.Columns.Add( "CompanyId" )
      > For i As Integer = 0 To 5
      > Dim dr As DataRow = dt.NewRow
      > dr(0) = ChrW(i + 65)
      > dr(1) = i + 1
      > dt.Rows.Add(dr)
      > Next
      > ComboBox1.DataS ource = dt
      > ComboBox1.Displ ayMember = "CompanyNam e"
      > ComboBox1.Value Member = "CompanyId"
      > End Sub
      >
      > Private Sub Button1_Click(B yVal sender As _
      > System.Object, ByVal e As System.EventArg s) Handles Button1.Click
      > ComboBox1.Selec tedValue = 3
      > If ComboBox1.Selec tedIndex <> -1 Then
      > MessageBox.Show (ComboBox1.Text )
      > End If
      > End Sub
      > Private Sub Button2_Click(B yVal sender As System.Object, _
      > ByVal e As System.EventArg s) Handles Button2.Click
      > Dim myindex As Integer = ComboBox1.FindS tringExact("D")
      > If myindex <> -1 Then
      > ComboBox1.Selec tedIndex = myindex
      > MessageBox.Show (ComboBox1.Sele ctedValue.ToStr ing)
      > End If
      > End Sub
      > End Class
      > ///
      >
      >[color=green]
      > >
      > > My Question was:
      > > 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.
      > >
      > >
      > > And the answer:
      > > You could use a hashtable to store the index-ID mappings. something[/color][/color]
      like:[color=blue][color=green]
      > >
      > > 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),[/color][/color]
      it[color=blue][color=green]
      > > wouldn't be too bad to loop through the combobox to retrieve the index[/color]
      > since[color=green]
      > > populating and maintaining the hashtable does add some overhead anyway.
      > >
      > >
      > > And my two new questions:
      > > 1.
      > > 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 someone declare items oCompany, Company, Companies?
      > >
      > >
      > > 2.
      > > 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,
      > > because they are defined as Object
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...