Jump to content
Sign in to follow this  
idbirch

Enable multiple select in ListBox

Recommended Posts

idbirch

I've got a listbox in my GUI and I want to allow multiple select, just like the $LBS_MULTIPLESEL style does but only when the CTRL key is held down. I've played with _IsPressed() but polling this contantly causes flicker. I've looked at HotKeySet but can't see a way of knowing when the Ctrl key has been released again. Is there an easy way to accomplish this which I'm missing?

At the moment, it's looking like I may have to add a checkbox to enable and disable muliple select but that just seems....lame.

Share this post


Link to post
Share on other sites
Zedna

Princip of antiflickering:

Global $ctrl = False
Global $ctrl_prev = False

While 1
 $ctrl = _IsPressed('11') = 1  ; Ctrl
 If $ctrl <> $ctrl_prev Then 
    $ctrl_prev = $ctrl
    If $ctrl Then
        ; enable
        ; ...
    Else
        ; disable
        ; ...
    EndIf
 EndIf

 $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit 
        Case ...
 EndSwitch
WEnd
Edited by Zedna

Share this post


Link to post
Share on other sites
idbirch

Thanks for the help Zedna, your example script has solved the flickering issue. The only remaining problem is I can't get the toggle of multi-select to work. Here's an example script:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>
#Include <GuiTab.au3>

Global $CTRLisPressed = False
Global $CTRLisPressed_prev = False

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 283, 289, 192, 124)
$List1 = GUICtrlCreateList("", 16, 40, 241, 214)
GUICtrlCreateLabel("List of stuff:", 16, 16, 58, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetData ( $List1, "Item 1" )
GUICtrlSetData ( $List1, "Item 2" )
GUICtrlSetData ( $List1, "Item 3" )

While 1
    
     $CTRLisPressed = _IsPressed('11') = 1; Ctrl
     If $CTRLisPressed <> $CTRLisPressed_prev  Then 
        $CTRLisPressed_prev = $CTRLisPressed
        If $CTRLisPressed Then
           $Change = GUICtrlSetStyle ( $List1, $LBS_MULTIPLESEL )
           ConsoleWrite ( "Ctrl is held.  SetStyle result = " & $Change & @CRLF )
        Else
           $Change = GUICtrlSetStyle ( $List1, BitOr ($LBS_SORT, $WS_BORDER, $WS_VSCROLL))
           ConsoleWrite ( "Ctrl is released.  SetStyle result = " & $Change & @CRLF )
        EndIf
    EndIf
    
    $nMsgMainGUI = GUIGetMsg(1)
        
        Select
            Case $nMsgMainGUI[0] = $GUI_EVENT_CLOSE and $nMsgMainGUI[1] = $Form1
                Exit
                
        EndSelect
        
WEnd
Edited by idbirch

Share this post


Link to post
Share on other sites
Zedna

From helpfile:

GUICtrlSetStyle()

Remarks

Some styles cannot be changed dynamically, check MSDN documentation. $CBS_UPPERCASE combo style is one example.

It seems you can't change LBS_MULTIPLESEL dynamically.

This works OK:

$List1 = GUICtrlCreateList("", 16, 40, 241, 214, BitOr($GUI_SS_DEFAULT_LIST, $LBS_MULTIPLESEL))

This doesn't work:

$List1 = GUICtrlCreateList("", 16, 40, 241, 214)
GUICtrlSetStyle ( $List1, BitOr($GUI_SS_DEFAULT_LIST, $LBS_MULTIPLESEL))
Edited by Zedna

Share this post


Link to post
Share on other sites
idbirch

Hm, yes I did note that caveat in the help file but found nothing on MSDN to suggest that it couldn't be changed on the fly, damn. I'm not in the office with my code now but would destroying the listbox (backing up its contents first of course) and then recreating it with that style work? Or is that a stupid idea? Does anyone have any other suggestions as to how this could be achieved?

Share this post


Link to post
Share on other sites
Authenticity

If you're satisfied with shift also, you can use $LBS_EXTENDEDSEL.

Share this post


Link to post
Share on other sites
idbirch

Thanks Authenticity, another case of the 'GUI Control Styles' section of the help file not being updated with the newer styles available with the _GUICtrl* family. Anyway, yes, this seems like the best alternative for now. And thanks again Zedna, I'm sure I'll find another use for that IsPressed() trick.

Share this post


Link to post
Share on other sites
idbirch

Hang on a sec! I've just tried out $LBS_EXTENDEDSEL and holding shift highlights everything in between the 2 items you click and CTRL lets you select multiple, non-adjacent items! Brilliant!

Share this post


Link to post
Share on other sites
Authenticity

Are you using $LBS_MULTIPLESEL also? I didn't use it because it's not required in this case.

Share this post


Link to post
Share on other sites
idbirch

No, just $LBS_EXTENDEDSEL which is doing exactly what I originally asked for - thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×