Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I've been using the ComboBox control for quite some time now, but one thing I've never got around to finding out is whether you can track the ID of each item in the ComboBox.

 

For example, I have a table on a database with a list of People and each record is identified by a Primary Key.

What I want to be able to do is populate a ComboBox with People's names and when an item is selected from the ComboBox I want to immediately know the primary key of that item (not the SelectedIndex).

 

Is there some method of "tagging" each item in the ComboBox with an ID?

 

Up to now, I've been synchronising my combobox with an ArrayList. As you can imagine, this gets quite frustrating after implementing the nth ComboBox :-\

 

Thanks,

-John.

Posted

Hi John,

the short answer is that you'd have to have a class with a Person's name & a Primary key property. Create a new instance for each row you're adding to the combobox and then add the object.

 

When you come to retrieve the selected item, you should then be able to cast the selected item back to your Person class and retrieve the Primary key quite happily.

 

I hope this helps, sorry for not whacking some code together though, I'm a bit busy this morning.

Posted

jspencer,

 

I see - so I should be able to add an Object of Class Person to the ComboBox. But how does the ComboBox render the person (or even know to display the Person's Name)?

 

I have quickly done up some code which you might point out the necessary Changes?

 

Private Class Person
       Public sID As Integer
       Public sName As String

       Sub New(ByVal sID As Integer, ByVal sName As String)
           Me.sID = sID
           Me.sName = sName
       End Sub

   End Class

   Private Sub frmSyncComboboxes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim myPerson As Person

       myPerson = New Person(1, "John")
       Me.ComboBox1.Items.Add(myPerson)

       myPerson = New Person(2, "Joe")
       Me.ComboBox1.Items.Add(myPerson)

       myPerson = New Person(3, "Jim")
       Me.ComboBox1.Items.Add(myPerson)

       myPerson = New Person(4, "Jack")
       Me.ComboBox1.Items.Add(myPerson)
   End Sub


   Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
       Dim myPerson As Person = Me.ComboBox1.SelectedItem
       MessageBox.Show(myPerson.sID & " - " & myPerson.sName)
   End Sub

 

Thanks, I appreciate your patience.

Posted

If the combobox is bound to a database table, you could just use the DisplayMember and ValueMember attributes couldn't you? Then you should be able to access the "ID" using the .SelectedValue of the combobox.

 

On the "Person" class (if you decide to go that route), you may have to add a ToString method to get it to show in the combobox correctly (not sure though). Something like this:

 

Public Overrides Function ToString() As String
Return sName
End Function

 

I use both methods (depending on the situation) and they both work well.

Being smarter than you look is always better than looking smarter than you are.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...