krinpit Posted July 30, 2004 Posted July 30, 2004 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. Quote
jspencer Posted July 30, 2004 Posted July 30, 2004 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. Quote
krinpit Posted July 30, 2004 Author Posted July 30, 2004 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. Quote
Mothra Posted July 30, 2004 Posted July 30, 2004 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. Quote Being smarter than you look is always better than looking smarter than you are.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.