C
CSharp
C++
Java
Visual Basic
HTML - CSS
Android
Asp
Asp.Net
Php
Python
JavaScript
SQL
XML
Assembly
ActiveX
API
Code Snippets
Database
Files Directories Drives
Forms
Graphics Games
Multimedia
Strings
Windows and Controls
More...
Visual Basic > Other sample source codes
Visual Basic Code > Use a VB listbox's ItemData property to store ID numbers
Use a VB listbox's ItemData property to store ID numbers Often, when you fill a listbox with data from a database, you want the control to display descriptive values; but you may also need it to store key values as well. For example, if you fill a listbox with employee names, no doubt you'll also want to store each employee 's ID number. Fortunately, if you use numbers as an ID, Visual Basic makes it easy to so with listbox's ItemData property. This property lets you store an additional number for each item in the list. To see how this works, add a listbox to a standard form and attach the following code. When you run the project, the listbox displays only the employee names. Click an item to see a message box that shows both the selected employee name as well as the stored ID number. Private Sub Form_Load() 'Fill List1 and ItemData array with 'corresponding items in sorted order. With List1 .AddItem "Mallik Murthy" .ItemData(.NewIndex) = 42310 .AddItem "Chien Lieu" .ItemData(.NewIndex) = 52855 .AddItem "Mauro Sorrento" .ItemData(.NewIndex) = 64932 .AddItem "Cynthia Bennet" .ItemData(.NewIndex) = 39227 End With End Sub Private Sub List1_Click() With List1 'Append the employee number and the employee name. MsgBox .ItemData(.ListIndex) & " " & .List(.ListIndex), vbInformation End With End Sub
Privacy Policy
|
Contact
|
Advertising
|
Link to Us
|
Directory