Item Indexes in Lists and Combo Boxes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VB6 I used to load a comb or listbox in the following fashion
snRecordset is a recordset from a table

Do Until snRecordset.eo
combobox1.additem snRecordset![LName
combobox1.ItemData(combobox1.NewIndex) = snRecordset![ID

snRecordset.movenex
Loo

Then when the user selected the combo box I could pass the combobox1.ItemData(combobox1.ListIndex) value to a parameter for a query. Doing it this way would allow me to query on an Unique ID versus a last name that could have the same value. For example, if you pass the Text property there could be 3 Smiths

What's the equivelent in VB.NET

Thanks in advance

Davi
 
Hi David,

I think something as

combobox1.datasource = dataset1.tables(0)
combobox1.displaymember = "LName"
combobox1.valuemembr = "Id"

I hope this helps?

Cor
 
In VB6 I used to load a comb or listbox in the following fashion.
snRecordset is a recordset from a table.

Do Until snRecordset.eof
combobox1.additem snRecordset![LName]
combobox1.ItemData(combobox1.NewIndex) = snRecordset![ID]

snRecordset.movenext
Loop

Then when the user selected the combo box I could pass the combobox1.ItemData(combobox1.ListIndex) value to a parameter for a query. Doing it this way would allow me to query on an Unique ID versus a last name that could have the same value. For example, if you pass the Text property there could be 3 Smiths.

What's the equivelent in VB.NET?

Thanks in advance,

David

Cor's answer is probably the best way to go about it... But, the
equivelent to .NewIndex in .NET is the return value of the Items.Add
method...

Dim newIndex As Integer

newIndex = combobox.Items.Add(Item)


There is no direct equivalent ot ItemData. The reason is that in .NET
you can store complete objects in the combo or listbox - so you can
associate as much data as you would like with each entry.

Anyway, just wanted to trow that out.
 
to convert without surprises use the API

' COMBOBOX messages
Private Const CB_SETITEMDATA = &H151, CB_GETITEMDATA = &H150

' LISTBOX messages
Private Const LB_SETITEMDATA = &H19A, LB_GETITEMDATA = &H199

' windows messages
Private Declare Ansi Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal LParam As Integer) As Integer

' adding items and itemdata a COMBOBOX
NewIndex = Me.Items.Add(MyString)
SendMessage(ComboBox1.Handle, CB_SETITEMDATA, NewIndex, ItemData)

' retrieving the itemdata from a COMBOBOX
ItemData = SendMessage(ComboBox1.Handle, CB_GETITEMDATA, ListIndex, 0)
 
Back
Top