CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: ListBox

  1. #1
    Join Date
    Jul 2004
    Location
    South Africa
    Posts
    71

    ListBox

    Hi All,

    I have loaded the fields from a table into a listbox, but the problem is with the index. when I try to reference the index it gives me the wrong one. I have relationship between the two tables. I set the listbox's index into the one in the table and referencing the new index but it doesn't work.

    Can somebody please help me

  2. #2
    Join Date
    Jan 2001
    Location
    Germany
    Posts
    222
    You could use a ListView. In a list view, every item has a Tag property. You can save the item's index in the table into the Tag property. Never assume the index of the item in the table is the same as the index of the item in the ListBox or ListView.

    If you don't want to use a ListView, you could emulate a Tag property by having an array which holds the indeces of the items in the list box.
    Teamwork Software - Stuff That Does Something

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    Quote Originally Posted by nkagi
    Hi All,

    I have loaded the fields from a table into a listbox, but the problem is with the index. when I try to reference the index it gives me the wrong one. I have relationship between the two tables. I set the listbox's index into the one in the table and referencing the new index but it doesn't work.

    Can somebody please help me
    I'm not convinced youre using Index in the right way.. Index, when talking of Listbox, is used to identify which control we are talking about, in an array of controls..

    Avoid using Tag too, because that is the data for that Listbox control, not for each item inside of it.

    Have a look at the simple code from a project of mine:
    Code:
    Public Sub populate()
    
      findNextItemNumber
    
      List1.Clear
    
      'i wrote a class module called PreparedStatement so to make DB
      'interactions like that in java - ignore this part, all we care about
      'is the result set, adoRS
      ps.prepareStatement DB_SELECT
      Set adoRS = ps.executeQuery
    
    
      'are there actually any reasons there?
      If adoRS.EOF Then
        List1.AddItem "No editable reasons"
        List1.AddItem "were found. You may"
        List1.AddItem "only  use  the  Add"
        List1.AddItem "facility."
        List1.Enabled = False
        cmdCancel_Click
        Exit Sub
      End If
    
      List1.Enabled = True
    
      'yes? then add them to the list
      Dim itemDataCounter As Integer
      itemDataCounter = 0
      Do Until adoRS.EOF
        List1.AddItem adoRS!ParaDescription
    
        'here look! :)
        List1.ItemData(itemDataCounter) = CLng(adoRS!ParaCode)
        itemDataCounter = itemDataCounter + 1
        adoRS.MoveNext
      Loop
    
      'reset everything by pretending to click on cancel
      cmdCancel_Click
    
    End Sub
    it populates a list box, and each item in the list shows some text, but that item has an autonumber primarty key, and i want to keep that primary key accessible, but hide from the user's eyes, so i put it in the itemdata property.. every item in a list has its own itemdata property, and it's "as Long " type
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Jan 2001
    Location
    Germany
    Posts
    222
    Yeah, I forgot about the ItemData property.
    Teamwork Software - Stuff That Does Something

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

Featured