INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...I have tons of books, have book marked tons of tutorials, which have helped, but this forum has answered those "impossible to find" solutions. I am thrilled with this site..."
Geography
Where in the world do Tek-Tips members come from?
|
Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ
|
How-to
|
Adding objects to the items collection
Posted: 13 Oct 03
|
To add objects to your listbox's item collection you need to do the following.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '% Step 1 '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
First Create your object:
Public Class Customer 'Private member variables to hold the data Private mFirstName As String Private mLastName As String Private mAcctNumber As Integer
'Your class constructor Public Sub New(ByVal fname As String, ByVal lname As String, ByVal acctNum As Integer) mFirstName = fname mLastName = lname mAcctNumber = acctNum
End Sub
'Your public access to your private variables Public Property FirstName() As String Get Return mFirstName End Get Set(ByVal Value As String) mLastName = Value End Set End Property
Public Property LastName() As String Get Return mLastName End Get Set(ByVal Value As String) mLastName = Value End Set End Property Public Property AcctNumber() As Int32 Get Return mAcctNumber End Get Set(ByVal Value As Int32) mAcctNumber = Value End Set End Property
'Very important. This is what will show in the list box when 'you add the object. 'if you do not override the ToString method 'the name of the object will show in the list box Public Overrides Function ToString() As String 'This can be whatever you want to show in the listbox Return LastName & ", " & FirstName End Function
End Class
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '% Step 2 '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Create and instance of the object Dim myCustomer as New Customer("John","Doe",1234)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '% Step 3 '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Add object to the listbox\combobox
Listbox1.Items.Add(myCustomer)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '% Step 4 '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'To access all the properties of the object
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim listCustomer As Customer = ComboBox1.SelectedItem
'You can then access its properties 'listCustomer.Firstname 'listCustomer.LastName 'listCustomer.AcctNum ListBox1.Items.Add(stritem & "has in dex of " & intItem) End Sub
|
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ Index
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 Forum
My FAQ Archive
Email This FAQ To A Friend |
|
 |
|