ItemData

G

Guest

I am just starting with VB 2005 . VB6 list boxes and combo boxes had ItemData
property. I utilized that feature a lot because all I want to create a
transaction or entry in a DataBase is the item ID anyway. VB.Net boxes do not
have ItemData property. VisualStudio 2005 Documentation suggests
"Microsoft.VisualBasic.Compatability.VB6.SetItemData" to gain this
capability. (Of course I'm missing something because my auto-complete does
not offer Compatability.) Hopefully VB.Net offers an imporoved way to
accomplish this task.
What is the "best practice" VB.Net method to offer my users a list of
acceptable Names/Descriptions to choose from and returns / yields the ID for
that Name/Description?
 
P

Patrick Steele

I am just starting with VB 2005 . VB6 list boxes and combo boxes had ItemData
property. I utilized that feature a lot because all I want to create a
transaction or entry in a DataBase is the item ID anyway. VB.Net boxes do not
have ItemData property. VisualStudio 2005 Documentation suggests
"Microsoft.VisualBasic.Compatability.VB6.SetItemData" to gain this
capability. (Of course I'm missing something because my auto-complete does
not offer Compatability.) Hopefully VB.Net offers an imporoved way to
accomplish this task.
What is the "best practice" VB.Net method to offer my users a list of
acceptable Names/Descriptions to choose from and returns / yields the ID for
that Name/Description?

"Adjusting to life without ItemData"
http://weblogs.asp.net/psteele/story/7534.aspx
 
G

Guest

John,

At least a couple of options:

If the data is coming from a database table, you can retrieve the data into
a datatable and then bind the datatable to the listbox by setting the
listbox's DataSource property to the datatable.

Part of the binding process is to designate a column from the datatable to
be the listbox's DisplayMember. This value gets displayed in the listbox.

You also designate a column from the datatable to be the listbox's
ValueMember. This value is then retrieved from the listbox's SelectedValue
property when the user selects a particular item.

Another option is to realize that the listbox's Items collection holds a
collection of OBJECTS, not just strings. You can add an object to the
listbox, and the listbox will display whatever property of the object that
gets returned from the object's ToString method, such as an EmployeeName
property.

Then you can retrieve the seleced object from the listbox and access
whatever property you are interested in, such as an EmployeeID property.

Kerry Moorman
 
T

Tim Patrick

ListBoxes and ComboBoxes don't just hold one string and one integer value
per entry anymore. You can store entire records in your list, and configure
it to just show you the text needed.

To simulate the old ItemData functionality, I use an "ItemData" class that
combines the text and integer portions into a single record.

Public Class ItemData
' ----- Class used to support ListBox and ComboBox items.
Public ItemText As String
Public ItemData As Long

Public Sub New(ByVal newText As String, ByVal newData As Long)
' ----- Required constructor.
ItemText = newText
ItemData = newData
End Sub

Public Overrides Function ToString() As String
' ----- Show the text in ListBox and ComboBox displays.
Return ItemText
End Function

Public Overrides Function Equals(ByVal obj As Object) As Boolean
' ----- Allow IndexOf() and Contains() searches by ItemData.
If (TypeOf obj Is Long) Then
Return CBool(CLng(obj) = ItemData)
Else
Return MyBase.Equals(obj)
End If
End Function
End Class

The ToString() method will cause the text of the record to appear in the
list box. Here's how I add an item to a list box.

ListBox1.Items.Add(New ItemData("The text part.", longValue))

The Equals override lets me quickly locate items by their embedded ID.

positionInList = ListBox1.Items.IndexOf(longValue)

To get the item value of the selected item:

longValue = CType(ListBox1.SelectedItem, ItemData).ItemData

Getting the text value is simpler.

textValue = ListBox1.SelectedItem.ToString()
 

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