Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • 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.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ


Adding objects to the items collection by DotNetDoc
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
            Return mFirstName
        End Get
        Set(ByVal Value As String)
            mLastName = Value
        End Set
    End Property

    Public Property LastName() As String
            Return mLastName
        End Get
        Set(ByVal Value As String)
            mLastName = Value
        End Set
    End Property
    Public Property AcctNumber() As Int32
            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


'% 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
        ListBox1.Items.Add(stritem & "has in dex of " & intItem)
    End Sub

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close