Home All Groups Group Topic Archive Search About
Author
26 Oct 2006 5:51 PM
John Brown
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?

--
John Brown

Author
26 Oct 2006 6:07 PM
Patrick Steele
In article <1E8F7821-66CE-4C92-9747-102F9E31B***@microsoft.com>,
JohnBr***@discussions.microsoft.com says...
> 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

Author
26 Oct 2006 6:11 PM
Kerry Moorman
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


Show quoteHide quote
"John Brown" wrote:

> 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?

> --
> John Brown
Author
26 Oct 2006 6:42 PM
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()

-----
Tim Patrick
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> 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?
Author
26 Oct 2006 10:14 PM
Herfried K. Wagner [MVP]
"John Brown" <JohnBr***@discussions.microsoft.com> schrieb:
>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.

See:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/029eaf619366a274>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>