Item Indexes in Lists and Combo Boxes

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
 
C

Cor

Hi David,

I think something as

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

I hope this helps?

Cor
 
T

Tom Shelton

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.
 
J

John Smith

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top