Solved

VB.NET Transfer Object Info from ListBox to Labels

Posted on 2003-11-23
4
341 Views
Last Modified: 2013-12-26
I have a ListBox populated with Student objects that uses the following code:

*****************************************
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.Items.Add(New Student("Jane", "Smith", 610523))
        Me.ListBox1.Items.Add(New Student("John", "Holland", 984872))
        Me.ListBox1.Items.Add(New Student("William", "McMillan", 371957))
        Me.ListBox1.Items.Add(New Student("Sarah", "Ferguson", 510792))
        Me.ListBox1.Items.Add(New Student("Fiona", "Gilbert", 921605))
        Me.ListBox1.Items.Add(New Student("Carol", "Vigil", 192602))
        Me.ListBox1.Items.Add(New Student("Tiffany", "Thomson", 429170))
        Me.ListBox1.Items.Add(New Student("Josh", "Albertson", 112606))
        Me.ListBox1.Items.Add(New Student("Chris", "Perkins", 209790))
        Me.ListBox1.Items.Add(New Student("Eric", "Rankin", 224571))
    End Sub

End Class

Public Class Student
    Private m_FirstName As String
    Private m_LastName As String
    Private m_ID As Long

    Public Sub New(ByVal strFName As String, ByVal strLName As String, ByVal lngID As Long)
        m_FirstName = strFName
        m_LastName = strLName
        m_ID = lngID
    End Sub

    Public Overrides Function ToString() As String
        Return m_LastName & ", " & m_FirstName
    End Function
End Class

****************************************
My list populates fine with "LastName, FirstName" items. What I want to do now is click on a list item and transfer the First Name, Last Name and ID to 3 separate labels or textboxes.

Sombody please provide a solution. Thanks,

Dys
0
Comment
Question by:Dys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 9807027
The objects in a listbox can be accessed by using the ObjectCollection method of the listbox class.
You can then use the item property to get a specific item in the collection.

The index of the currently selected item can be accessed by using the SelectedIndex property of the listbox class.

So if you put the two together like this:

Dim s As Student
s = ListBox1.ObjectCollection.Item(ListBox1.SelectedIndex)

it will return a reference to the object corresponding to the currently selected text in the listbox.

You will have to modify your Student Class to have Property Accessors for the Name an ID fields so you can get to them.

Idle_Mind
0
 

Author Comment

by:Dys
ID: 9808293
I added the code you suggested and modified the Student Class with property get/set procedures, but the code below gets a blue underline error:

     s = ListBox1.ObjectCollection.Item(ListBox1.SelectedIndex)

*******************************************************
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.Items.Add(New Student("Jane", "Smith", 610523))
        Me.ListBox1.Items.Add(New Student("John", "Holland", 984872))
        Me.ListBox1.Items.Add(New Student("William", "McMillan", 371957))
        Me.ListBox1.Items.Add(New Student("Sarah", "Ferguson", 510792))
        Me.ListBox1.Items.Add(New Student("Fiona", "Gilbert", 921605))
        Me.ListBox1.Items.Add(New Student("Carol", "Vigil", 192602))
        Me.ListBox1.Items.Add(New Student("Tiffany", "Thomson", 429170))
        Me.ListBox1.Items.Add(New Student("Josh", "Albertson", 112606))
        Me.ListBox1.Items.Add(New Student("Chris", "Perkins", 209790))
        Me.ListBox1.Items.Add(New Student("Eric", "Rankin", 224571))

    End Sub
 
  Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click

        Dim s As Student

        s = ListBox1.ObjectCollection.Item(ListBox1.SelectedIndex)

    End Sub

End Class

Public Class Student
    Private m_FirstName As String
    Private m_LastName As String
    Private m_ID As Long

    Public Sub New(ByVal strFName As String, ByVal strLName As String, ByVal lngID As Long)
        m_FirstName = strFName
        m_LastName = strLName
        m_ID = lngID
    End Sub

    Public Overrides Function ToString() As String
        Return m_LastName & ", " & m_FirstName
    End Function

    Public Property FirstName() As String
        Get
            Return FirstName
        End Get
        Set(ByVal Value As String)
            m_FirstName = Value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return LastName
        End Get
        Set(ByVal Value As String)
            m_LastName = Value
        End Set
    End Property

    Public Property ID() As Long
        Get
            Return ID
        End Get
        Set(ByVal Value As Long)
            m_ID = Value
        End Set
    End Property

End Class
0
 

Author Comment

by:Dys
ID: 9808379
Thanks Idle_Mind, I got it working with this code:

        Dim s As Student

        s = ListBox1.Items.Item(ListBox1.SelectedIndex)

        Label1.Text = s.FirstName
        Label2.Text = s.LastName
        Label3.Text = s.ID

I used "Items.Item" instead of "ObjectCollection.Item" and it worked fine. And thanks for the tip on the property procedures, that really helped too.

Dys :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9810615
Glad you got it to work.

Idle_Mind
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question