Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
more resources

Add ListBox Items
The .NET Framework lets you associate any type of value with a ListBox row.
by Fabio Claudio Ferracchiati and Jimmy Nilsson

Posted October 7, 2003

Technology Toolbox: VB.NET, SQL Server 2000

Q. Add ListBox Items
When I use VB6 and a ListBox (or a ComboBox), I can use the ItemData property to associate a value with a row different from the value that's displayed. However, ItemData isn't available in VB.NET. How can I perform the same operation?

ADVERTISEMENT

A.
You can add an item to a ListBox (or ComboBox) list in VB.NET by using the Items property's Add method, which returns a reference to the ObjectCollection class. As this class's name implies, the ListBox can contain a collection of Object objects, so you're not constrained (as you are with ItemData and VB6) to associate only an integer value with the ListBox item. The Object class is the base class for each .NET Framework class, so you can associate practically any kind of information with the ListBox item.

A ListBox that shows DataSet rows is a typical example. Suppose you use VS.NET 2003 to create an empty strongly typed DataSet (select Project | Add New Item, then select DataSet from the templates list) and add three columns: ID, FIRST_NAME, and LAST_NAME. The Form class's Load event fills the DataSet and adds the new row to the ListBox:

Private Sub frmMain_Load(_
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles MyBase.Load
Dim ds As New Dataset1
Dim r As Dataset1.EMPLOYEERow = _
   ds.EMPLOYEE.NewEMPLOYEERow()
   r.ID = 1
   r.FIRST_NAME = "Fabio Claudio"
   r.LAST_NAME = "Ferracchiati"
   ds.EMPLOYEE.AddEMPLOYEERow(r)

   lbMain.Items.Add(r)
End Sub





Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home