Results 1 to 3 of 3

Thread: Listbox / Multiselect / Alphabetical select

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Listbox / Multiselect / Alphabetical select

    When I Press any key on my keyboard...I want to have the item highlighted which is as near as possible...
    "A" should result in the first item which start with A...after "A" I press...."B" and I get some item which starts with AB selected...

    I found some code which works fine when you have a normal listbox..when I use a MultiSelect box it doesn't work anymore...with a normal box it works perfect

    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
    2. Const LB_FINDSTRING = &H18F
    3.  
    4. Private Sub Text1_Change()
    5.     'Retrieve the item's listindex
    6.     List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
    7. End Sub

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Im not completly sure what you are asking. If what you want it something like the Index area of a help file, then use this in a sorted ListBox.

    Function ListSearch(KeyField) As Integer
    Dim Lower As Integer, Upper As Integer, Middle As Integer
    Dim MiddleItem As String

    Lower = 0
    Upper = List1.ListCount - 1

    While 1
    Middle = Fix((Lower + Upper) / 2)
    MiddleItem = List1.List(Middle)
    If Upper < Lower Then
    ListSearch = -1
    Exit Function
    End If
    If StrComp(KeyField, Left(MiddleItem, Len(KeyField))) > 0 Then
    Lower = Middle + 1
    Else
    If StrComp(KeyField, Left(MiddleItem, Len(KeyField))) < 0 Then
    Upper = Middle - 1
    Else
    ListSearch = Middle
    Exit Function
    End If
    End If
    Wend
    End Function

    Then add a TextBox to your form, make its Visible property to false, then type this


    Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
    Text1.Text = Text1.Text + Chr(KeyCode)
    End Sub

    Private Sub Text1_Change()
    Dim pos As Integer
    pos = ListSearch(Trim$(Text1.Text))
    If pos >= 0 Then
    List1.ListIndex = pos
    Else

    End If
    End Sub

    Make sure that code is in a SORTED list box. It searches alphabetically. If what you mean is something like it selects 2 things at a time with say;


    (Selected)Apple
    (Selected)Above
    (Selected)Across
    (Not Selected)Zebra


    then the above code will not work. I doubt you will find any code if that is what you mean. But, good luck....


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Nope...

    I mean it just have to do same as the code I just gave...but it should also work with Multiselect boxes....

    It doesn't have to Multiselect items...

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