Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Listbox index issue

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Resolved [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?
    Last edited by Genom; May 24th, 2007 at 11:15 AM.
    Dim Me As Coder

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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)

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Dim ClientList As New Dictionary(Of Integer, String)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Listbox index issue

    vb Code:
    1. Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Start.mdb;Mode = Read;" & "Persist Security Info = False")
    2.  
    3. Dim command As New OleDbCommand("SELECT ClientID, ClientName FROM Table1", connection)
    4.  
    5. connection.Open()
    6. Dim reader As OleDbDataReader = command.ExecuteReader
    7. Dim table As New DataTable
    8.  
    9. table.Load(reader)
    10.  
    11. ' load the combobox
    12. myListBox.DisplayMember = "ClientName"
    13. myListBox.ValueMember = "ClientID"
    14. myListBox.DataSource = table
    15.  
    16. reader.Close()
    17. connection.Close()

  5. #5

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    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
    Dim Me As Coder

  6. #6

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    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
    Dim Me As Coder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width