Question ComboBox .ItemData from vb6 to vb.net

alecu7

New member
Joined
Sep 8, 2015
Messages
3
Programming Experience
10+
I have this code in vb6:

Public Sub SetDepartment(ByRef cbo As ComboBox, ByVal lDepartmentID As Long)
Dim lCounter As Long
With cbo
If .ListCount = 0 Then Exit Sub
For lCounter = 0 To .ListCount - 1
If lDepartmentID = .ItemData(lCounter) Then
.ListIndex = lCounter
End If
Next
End With
End Sub

So I have an ID and need to search this ID in the list of ValueMember. When I get the ID, I need to display the counterpart from DisplayMember list.

How do I do this in vb.net?

Please and thank you,
Alex
 
If you have set the ValueMember of a bound ComboBox then you can set the SelectedValue and the appropriate item will be selected and displayed. In short, assign your ID to the SelectedValue and the rest is automatic.
 
Thank you for the reply but I still am puzzled.
Combobox gets data from SQL server database:

With cboCategories
.DataSource = SQLds.Tables(0)
.ValueMember = "category_id"
.DisplayMember = "category_name"
.SelectedIndex = 0
End With

This is the data:

category_id Category_name
1 Input
2 Design Calc
3 .net
5 Distribution
7 Miscellaneous

After I populate the combobox, I ran a query to database and get ID =5
How do I show in combobox "Distribution"?
I'm very frustrated converting some vb6 code to vb.net.
In vb6 this was piece of cake.

Thank you
Alex
 
It's a piece of cake in VB.NET too. It's like you didn't even read my previous post. What did I say?
set the SelectedValue
assign your ID to the SelectedValue
I said it twice. Why haven't you done it?
 
It's simple why I didn't do it: because I didn't understand it.

I guess I must have missed the part where you said "I don't understand what you mean by 'set the SelectedValue, can you provide more detail'".
 
Back
Top