CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2016
    Posts
    35

    ListBox auto keyboard select

    Hello,

    I have a strange problem with auto-select using the keyboard in a multiple selection ListBox.

    Normally, when entering a letter on the keyboard will select the first occurrence of the string containing the letter.

    However, when I enter another letter, it selects the new letter but DOES NOT deselect the previous selection!

    In the attached pictures, the first selection is found by entering "T" on the keyboard.
    The second selection is found by entering the letter "j" but does not deselect the previous one.

    See attached pictures.

    However, selection using the mouse deselects previous selection.

    Any advice?Name:  first selectoin.PNG
Views: 216
Size:  5.0 KB
    Attached Images Attached Images  
    Last edited by robertzp; November 15th, 2017 at 01:12 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ListBox auto keyboard select

    Quote Originally Posted by robertzp View Post
    Hello,

    I have a strange problem with auto-select using the keyboard in a multiple selection ListBox.
    ...
    1. What do you mean by "auto-select"?
    2. What styles does your listbox have?
    3. What version of VS do you use?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2016
    Posts
    35

    Re: ListBox auto keyboard select

    VictorN,

    See my response interspered in your questions.

    What do you mean by "auto-select"?

    By auto-select, I mean entering a letter key on the keyboard automatically selects the first occurrence of the string contained in the list box. I think the attachments illustrated this quite clearly.

    If I press the letter “T” on the keyboard , the entry for “Twelve balls entry.pdf” is highlighted.

    If I entered the letter lowercase “j”, the first entry containing that letter “jim mosely solution edited.jpg” is highlighted.

    What styles does your listbox have?

    WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | LBS_NOTIFY | LBS_SORT | LBS_NOINTEGRALHEIGHT | LBS_HASSTRINGS | LBS_EXTENDEDSEL | LBS_DISABLENOSCROLL | LBS_WANTKEYBOARDINPUT

    What version of VS do you use?
    Visual Studio 2015

    Basically, I would like to duplicate the behavior of Windows Explorer in selecting the first occurence of a file in a folder which matches a keyboard character but deselects the previous one.

    I am not talking about multiple selection using the ctrl or shift keys.
    Last edited by robertzp; November 16th, 2017 at 12:43 AM. Reason: more info

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ListBox auto keyboard select

    Well, I tested it with VS2015 and confirm that sometimes it looks like undefined behaviour...
    However, with these styles:
    Code:
    LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT | LBS_WANTKEYBOARDINPUT | LBS_EXTENDEDSEL | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP
    it works a little better.

    Also, please read this MSDN article: CListBox::CharToItem
    if you'd like to extend the listbox behaviour using keyboard.
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2016
    Posts
    35

    Re: ListBox auto keyboard select

    Quote Originally Posted by VictorN View Post
    Well, I tested it with VS2015 and confirm that sometimes it looks like undefined behaviour...
    However, with these styles:
    Code:
    LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT | LBS_WANTKEYBOARDINPUT | LBS_EXTENDEDSEL | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP
    it works a little better.

    Also, please read this MSDN article: CListBox::CharToItem
    if you'd like to extend the listbox behaviour using keyboard.
    VictorN,

    I'll read up on CharToItem.

    FYI, previous keyboard selection is deselected if LBS_EXTENDEDSEL is removed. But this style is so essential for my needs I can't remove it.

    I also tried unsuccessfully to deselect the previous keyboard selection using
    SetSel(prev_index, FALSE);

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ListBox auto keyboard select

    Did you try with LBS_EXTENDEDSEL but without LBS_SORT?
    Victor Nijegorodov

  7. #7
    Join Date
    Jul 2016
    Posts
    35

    Re: ListBox auto keyboard select

    Quote Originally Posted by VictorN View Post
    Did you try with LBS_EXTENDEDSEL but without LBS_SORT?
    VictorN,

    Disabling LBS_SORT works!

    The only restriction is that you populate the listbox in sorted order!

    My app now works like Windows Explorer!

    Thanks very for the tip!

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: ListBox auto keyboard select

    You are welcome!
    Victor Nijegorodov

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