Listbox Itemdata

  • Thread starter Thread starter Merlin
  • Start date Start date
M

Merlin

Been using VS NET for a while now but never really used the Standard ListBox
control until now and whoops! where's the ItemData method gone - damn!,
can't Microsoft leave things alone.

Anyway did a quick read up and found that Listbox is not limited to strings
anymore and each item can contain an object bla bla bla...Z Z z z z...

Can someone please put me out of my misery and just quickly show me how to
add a string item and a value like in the VB6 version. Been looking for
examples but can't actually find anything that actually gets to the point.

All I want to do is add 20 text items with an associated Long number(key).
and when I select an item, to be able to also access the (key).

Many Many thanks.

Merlin
 
Hello,

Merlin said:
Been using VS NET for a while now but never really used
the Standard ListBox control until now and whoops! where's the
ItemData method gone - damn!, can't Microsoft leave things alone.

\\\
Dim p As Person = New Person()
p.Name = "Pink Panther"
p.Age = 22

Me.ComboBox1.Items.Add(p)

MessageBox.Show(DirectCast(Me.ComboBox1.Items.Item(0), Person).ToString())
..
..
..
Public Class Person
Private m_strName As String
Private m_ingAge As Integer

Public Property Name() As String
Get
Return m_strName
End Get
Set(ByVal Value As String)
m_strName = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_intAge
End Get
Set(ByVal Value As Integer)
m_intAge = Value
End Set
End Property

Public Overrides Function ToString() As String
Return m_strName & " (" & m_intAge.ToString() & ")"
End Function
End Class
///

Regards,
Herfried K. Wagner
 
Merlin my friend, I know just how you feel.

The fastest way is to use the databinding stuff (*Yuk*):
------------------
'Get yer data:
sSQL = "SELECT FruitID, FruitName FROM Fruits"
ds = GetDataset(sSQL)

With ListBox
.DataSource = ds.Tables(0) 'only table in ds
.DisplayMember = "FruitName"
.ValueMember = "FruitID"
End with

ds.dispose()
------------------
Yes, that will actually populate it, keys and all!

If you try to be really economical and do it with a
datareader, well, sorry you can't bind the thing.
(disappointing, I know).

Anyway to get your key back out from the selected:
----------------------------------
Dim nSelectedKey as integer

nSelectedKey = ListBox.SelectedValue
--------------------------------------

Remember "SELECTED" is the point here. The Item must have
the highlight (selection).

Watch out for the *Checked*ListBox, that will really make
you angry!

Good luck

Doz
___.-~@~-.___
Dorian Hill.
Microsoft Certified Solution Developer
Momentech Software Services
PO Box 348, Caulfield East, VIC 3145, Australia
E-Mail: (e-mail address removed)
 
Back
Top