[RESOLVED] [2005] Listbox index issue
Hi;
I have a table in my database which contains 2 columns: ClientID, ClientName.
I want to put Clientnames to a listbox and clientid as indexes of each clientname. eg:
ClientID ClientName
1 Boosh
4 Sarkizy
7 Bleir
95 Puteen
214 Tayyeap
Now I add every client to listbox but if I try to use insert function, so that I can add them with clientid's as their indexes, I get an error which tells me that 1 is not an available value for an index. I know that arrays are beginning with 0 in listbox but I want to store the clientid's with clientnames.
I have found a solution: I can add every item like this
Boosh [1]
Sarkizy [4]
Bleir [7] etc... But I will need their ID's later and I have to write another sub to extract their ID numbers from string. But it will limit user to use [] characters for Clientnames. Do you know a better solution?
Re: [2005] Listbox index issue
Create a Class for your names:
Code:
Public Class MyObject
Private _name As String
Private _ID As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property ID() As Integer
Get
Return _ID
End Get
Set(ByVal value As Integer)
_ID = value
End Set
End Property
Public Overrides Function ToString() As String
Return _name
End Function
End Class
Then when you add them to the listbox, it will use the ToString method when it displays the items in the list. So you could do something like this:
Code:
Dim x As New MyObject
x.Name = "Bill"
x.ID = 5
ListBox1.Items.Add(x)
x = New MyObject
x.Name = "Jane"
x.ID = 24
ListBox1.Items.Add(x)
Then you can access it later:
Code:
Dim obj1 As MyObject
obj1 = DirectCast(ListBox1.Items(1), MyObject)
MessageBox.Show(obj1.ID.ToString)
Re: [2005] Listbox index issue
Just a suggestion, maybe a simple dictionary might be an alternative. Store ID in the key, and string in the value:
vb Code:
Dim ClientList As New Dictionary(Of Integer, String)
Re: [2005] Listbox index issue
vb Code:
Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Start.mdb;Mode = Read;" & "Persist Security Info = False")
Dim command As New OleDbCommand("SELECT ClientID, ClientName FROM Table1", connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader
Dim table As New DataTable
table.Load(reader)
' load the combobox
myListBox.DisplayMember = "ClientName"
myListBox.ValueMember = "ClientID"
myListBox.DataSource = table
reader.Close()
connection.Close()
Re: [2005] Listbox index issue
So these mothods are really helpful for me. I will save these objects in combobox too so I will use Negative0's method. I didnt know that I can add any object like this to a listbox Ill try it.
Thanks a lot
Re: [2005] Listbox index issue
I get this error
Client.ID = Dataset.Tables(TbClients).Rows(i).Item(0)
Object reference not set to an instance of an object.
But it is not happenning with Client object because i have written numbers or strings and it worked. It happens because of item of dataset I dont know how to solve it??
NOTE: TbClients is a string "Clients"
i is a integer and goes from 0 to dataset's table's rowcount - 1