After reading it over a few times, I get what you are trying to accomplish.
Correct me if I am wrong, but you want to have:
- a button with textboxes to add new names with password and username
- a listbox displaying the names
- a way of determining what instance is selected from the listbox selection changed
A problem with your current code is that you are not actually storing your classes.
You only know the selected name, but no instance of the previously added item is there to evaluate with.
To do this properly, make a single list variable in your Form1 class:
Then, to "sync" it with your listbox, you add a new item to both your listbox AND your user list:Code:Private users As New List(Of user)
Now you can access your "already added info" from the selected index:Code:Dim newObject As New user() newObject.setDetails(txtUsername.Text, txtPassword.Text, txtName.Text) lstUsers.Items.Add(newObject.getName()) users.Add(newObject)
Important is to keep some sort of "back buffer" with your data in the background, and not to store your info in a control. I made the same mistake before, adding all my information to a listview control. But it makes it hard to use your stored data.Code:Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged Dim selecteduser As user = users(lstUsers.SelectedIndex) 'do some stuff with this class, like setting your textboxes or labels End Sub
EDIT
In case you want some sort of global storage, you can add a shared list member to your user class:
Code:Public Class user Private Shared users As New List(Of user) Public Shared Function GetUser(ByVal name As String) As user For Each u As user In users If u.getName = name Then Return u Next Return Nothing End Function Public Shared Sub AddUser(ByVal u As user) users.Add(u) End Sub




Reply With Quote