Dys
asked on
VB.NET Transfer Object Info from ListBox to Labels
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
**************************
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Idle_Mind, I got it working with this code:
Dim s As Student
s = ListBox1.Items.Item(ListBo x1.Selecte dIndex)
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 :)
Dim s As Student
s = ListBox1.Items.Item(ListBo
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 :)
Glad you got it to work.
Idle_Mind
Idle_Mind
ASKER
s = ListBox1.ObjectCollection.
**************************
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.
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