CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    7

    Select item in listBox1 and corresponding item in ListBox2 is also selected


    Hi,
    Hi I have two Listboxes list1 has lets say Firstname in List1 and List2 has surnames.
    How do I select an item in List1 and have it's corresponding item in List2 selected as well?

    So if I select Item 6 in List1, I want item6 in List2 to be selected too.

    Ken

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332
    The ListIndex property can help you do this.
    Code:
    Private Sub List1_Click()
    List2.ListIndex = List1.ListIndex
    End Sub
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766
    It depends on how you match the two list boxes.

    You may want to set up a loop to search through the second list box eg:
    for x = 0 to lstbox2.listcount - 1
    ......
    next x

    If you are interogating the text of a list box (apart from the one you selected), you use the list(n) (n being one of the items in your list) property, if you want to interogate the index then use the item data property.

    Once you have found the item that you want, set the list index to the relevant value.

    The listindex may have a different value than the order in which you added the items if the list box is sorted.

    HTH
    JP

    Please remember to rate all postings.

  4. #4
    Join Date
    Nov 2002
    Posts
    82
    Hi ken1000, hier an example

    Code:
    Option Explicit
    
    Private Type TName
        strFirstName As String
        strSurname As String
    End Type
    
    
    
    Private Sub FillList(mList1 As ListBox, mList2 As ListBox)
    
    Dim mName() As TName
    Dim i As Integer
    ReDim mName(0)
    
        For i = LBound(mName) To 9
            If i > 0 Then ReDim Preserve mName(UBound(mName) + 1)
            mName(UBound(mName)).strFirstName = "FirstName N° " & CStr(i)
            mName(UBound(mName)).strSurname = "Surname N° " & CStr(i)
            
            mList1.AddItem mName(UBound(mName)).strFirstName
            mList2.AddItem mName(UBound(mName)).strSurname
        Next
    End Sub
    
    Private Sub Form_Load()
        FillList List1, List2
    End Sub
    
    Private Sub List1_Click()
        List2.Selected(List1.ListIndex) = True
        Label1 = List1.List(List1.ListIndex) & "   " & List2.List(List1.ListIndex)
    End Sub
    
    Private Sub List2_Click()
        List1.Selected(List2.ListIndex) = True
        Label1 = List1.List(List2.ListIndex) & "   " & List2.List(Me.List2.ListIndex)
    End Sub
    james

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