Jump to content

Listview column click issue


taietel
 Share

Recommended Posts

The issue is not with the Listview, but with with myself.

I'm trying to switch to a GUI when the column is clicked, but when the second gui appears, the focus is still on the main gui.

So far I failed with GuiSwitch and GuiSetState.

Any advice is more than welcome!

testLV.au3

Edited by taietel
Link to comment
Share on other sites

  • Moderators

taietel,

I do not understand what you mean by "the focus is still on the main gui". :x

When I click on the column header the secondary GUI appears and takes focus. If I set the focus to the combo or input using GUICtrlSetState it works without problem. So could you please be a little more specific. :P

However, you do have a MAJOR problem with your script. You are calling a function (_GUI_Sort) from within a message handler when the function contains a blocking infinite loop. :lol: This is likely to crash your script in pretty short order - as the Help file clearly states:

"Warning: blocking of running user functions which executes window messages with commands such as "Msgbox()" can lead to unexpected behavior, the return to the system should be as fast as possible !!!"

The way to do this correctly is to set a flag in the handler and then run the function from your main script like this - look for the <<<<<< lines as usual: :(

#include <File.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <GuiListView.au3>
Global $aCSV
Global $hGUI, $hSearch
Global $IDColumn
Global $fColFlag = False  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;some data
Global $aCSV[50], $arItems[UBound($aCSV)]
$aCSV[0] = "Col0|Col1|Col2|Col3|Col4"
For $i = 1 To 49
    $aCSV[$i] = "Item" & $i & " 0|" & "SubItem" & $i & " 1|" & "SubItem" & $i & " 2|" & "SubItem" & $i & " 3|" & "SubItem" & $i & " 4"
Next

$hForm = GUICreate("CVS Listview", 620, 250, -1, -1)
$hListView = GUICtrlCreateListView($aCSV[0], 10, 10, 600, 210)
For $i = 1 To UBound($aCSV) - 1
    $s_temp = $aCSV[$i]
    GUICtrlCreateListViewItem($s_temp, $hListView)
Next
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
    Sleep(10)
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If $fColFlag Then  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $fColFlag = False
        _GUI_Sort()
    EndIf

WEnd

Func _GUI_Sort()
    Local $icnSortaz, $hSortAZ, $icnSortza, $hSortZA, $cboCriteria, $hTVSelect, $btnCancel, $btnClose, $btnOK
    ;make the GUI at the mouse position:
    Local $pos = MouseGetPos()
    Local $iW = 190, $iH = 420
    $hGUI = GUICreate("Filter/Sort", $iW, $iH, $pos[0] - $iW + 15, $pos[1] - 15, BitOR($WS_SYSMENU, $WS_POPUP, $WS_BORDER), $WS_EX_TOPMOST);BitOR($WS_EX_TOPMOST, $WS_EX_LAYERED))
    GUISetBkColor(0xFFFFFF)
    ;AZ sort:
    $icnSortaz = GUICtrlCreateIcon(_Icon_AZ(), -1, 8, 8, 16, 16, BitOR($SS_NOTIFY, $WS_GROUP))
    GUICtrlSetCursor(-1, 0)
    $hSortAZ = GUICtrlCreateLabel("&Sort A to Z", 32, 8, 61, 17)
    GUICtrlSetCursor(-1, 0)
    ;ZA sort:
    $icnSortza = GUICtrlCreateIcon(_Icon_ZA(), -1, 8, 30, 16, 16, BitOR($SS_NOTIFY, $WS_GROUP))
    GUICtrlSetCursor(-1, 0)
    $hSortZA = GUICtrlCreateLabel("S&ort Z to A", 32, 30, 61, 17)
    GUICtrlSetCursor(-1, 0)
    ;Filtering:
    GUICtrlCreateGroup("Filter", 4, 60, $iW - 8, $iH - 100)
    GUICtrlCreateLabel("Criteria", $iW - 45, 74, 40, 17)
    GUICtrlSetColor(-1, 0x444444)
    $cboCriteria = GUICtrlCreateCombo("", 10, 92, $iW - 20, 23)
    Local $sIfText = "Equals...|Does Not Equal...|Begins With...|Ends With...|Contains...|Does Not Contain..." ;if text, then ...
    Local $sCriteriaSep = "----------------------------------------"
    Local $sIfNumber = "Greater Than...|Greater Than Or Equal To...|Less Than...|Less Than Or Equal To...|Between..." ;if number, then ...
    GUICtrlSetData(-1, $sIfText & "|" & $sCriteriaSep & "|" & $sIfNumber)
    $hSearch = GUICtrlCreateInput("Search", 10, 120, $iW - 20, 21)
    GUICtrlSetColor(-1, 0x444444)
    $hTVSelect = GUICtrlCreateTreeView(10, 146, $iW - 20, 223, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP), $WS_EX_CLIENTEDGE)
    $arItems[0] = GUICtrlCreateTreeViewItem("(Select All)", $hTVSelect)
    GUICtrlSetState(-1, $GUI_CHECKED)
    ;fill the treeview with data:
    For $i = 1 To UBound($aCSV) - 1
        $arItems[$i] = GUICtrlCreateTreeViewItem("", $hTVSelect)
        Local $item = _GUICtrlListView_GetItemTextArray($hListView, $i - 1)
        GUICtrlSetData(-1, $item[$IDColumn + 1])
        GUICtrlSetState(-1, $GUI_CHECKED)
    Next
    $btnOK = GUICtrlCreateButton("OK", 10, 386, 75, 25, $WS_GROUP)
    $btnCancel = GUICtrlCreateButton("Cancel", 106, 386, 75, 25, $WS_GROUP)
    $btnClose = GUICtrlCreateIcon("shell32.dll", -132, 168, 8, 16, 16, BitOR($SS_NOTIFY, $WS_GROUP))
    GUICtrlSetCursor(-1, 0)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    GUICtrlCreatePic("", 1, 1, $iW - 2, $iH - 2, BitOR($GUI_SS_DEFAULT_PIC, $SS_CENTERIMAGE))
    GUISetState(@SW_SHOW)
    GUISetState($hForm, @SW_DISABLE)

    GUICtrlSetState($cboCriteria, $GUI_FOCUS)

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    While 1
        Sleep(10)
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $icnSortaz, $hSortAZ
                ConsoleWrite("sorted A-Z..." & @CRLF) ;test
            Case $icnSortza, $hSortZA
                ConsoleWrite("sorted Z-A..." & @CRLF) ;test
            Case $btnCancel, $btnClose
                _Close($hGUI)
                Return ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< You have to get back somehow!
            Case $arItems[0]
                ;select all/none...
                Switch GUICtrlRead($arItems[0])
                    Case 257 ;check all
                        For $i = 1 To _GUICtrlTreeView_GetCount($hTVSelect) - 1
                            _GUICtrlTreeView_SetChecked($hTVSelect, $arItems[$i])
                        Next
                        ConsoleWrite("all checked!..." & @CRLF) ;test
                    Case 260 ;uncheck all
                        For $i = 1 To _GUICtrlTreeView_GetCount($hTVSelect) - 1
                            _GUICtrlTreeView_SetChecked($hTVSelect, $arItems[$i], False)
                        Next
                        ConsoleWrite("all unchecked!..." & @CRLF) ;test
                EndSwitch
            Case $arItems[1] To $arItems[UBound($aCSV) - 1]
                ;uncheck/check the SelectAll
                For $i = 1 To _GUICtrlTreeView_GetCount($hTVSelect) - 1;all but first
                    If _GUICtrlTreeView_GetChecked($hTVSelect, $arItems[$i]) = False Then _GUICtrlTreeView_SetChecked($hTVSelect, $arItems[0], False)
                Next
            Case $btnOK
                Local $bifat
                ;checked values...
                For $i = 1 To _GUICtrlTreeView_GetCount($hTVSelect) - 1
                    If _GUICtrlTreeView_GetChecked($hTVSelect, $arItems[$i]) Then $bifat &= $i - 1 & "|" ;listview starts at 0
                Next
                $bifat = StringTrimRight($bifat, 1)
                ConsoleWrite($bifat & @CRLF) ;test
                $bifat = ""
            Case $cboCriteria
                Local $sCriteria = GUICtrlRead($cboCriteria)
                If $sCriteria <> $sCriteriaSep Then ConsoleWrite($sCriteria & @CRLF) ;test
        EndSwitch
    WEnd
EndFunc   ;==>_GUI_Sort

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView, $hListView
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    ;get the number of the column
                    $IDColumn = DllStructGetData($tInfo, "SubItem")
                    ;and show the gui
                    $fColFlag = True ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            EndSwitch
    EndSwitch
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndSearch
    If Not IsHWnd($hSearch) Then $hWndSearch = GUICtrlGetHandle($hSearch)
    $hWndFrom = $ilParam
    $iIDFrom = _WinAPI_LoWord($iwParam)
    $iCode = _WinAPI_HiWord($iwParam)
    Switch $hWndFrom
        Case $hSearch, $hWndSearch
            Switch $iCode
                Case $EN_CHANGE
                    ConsoleWrite("changed" & @CRLF)
                Case $EN_KILLFOCUS
                    ;if nothing was entered, set to default text
                    If GUICtrlRead($hSearch) = "" Then GUICtrlSetData($hSearch, "Search")
                    ConsoleWrite("lost focus" & @CRLF) ;test
                Case $EN_SETFOCUS
                    ;if focused, delete the default text
                    If GUICtrlRead($hSearch) = "Search" Then GUICtrlSetData($hSearch, "")
                    ConsoleWrite("got focus" & @CRLF) ;test
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _LV_Sort($iMode = 1)
    Switch $iMode
        Case 1
            ;sort A-Z
        Case 2
            ;sort Z-A
        Case Else
            ;
    EndSwitch
EndFunc   ;==>_LV_Sort

Func _Close($hWnd)
    GUISetState($hForm, @SW_ENABLE)
    GUIDelete($hWnd)
    Return
EndFunc   ;==>_Close

I left out the icons - they were screrwing up the code tags. :nuke:

Please ask if you do not understand why it should be coded as above - or read the GUIRegisterMsg tutorial in the Wiki. :shifty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba!

I do not understand what you mean by "the focus is still on the main gui"

I'm sorry, but that was the worst explanation I thought at the moment... What I had in mind was the following: when I click a column header and the second gui shows, the column header can still be clicked. I want to avoid that.

Regarding the blocking, in the _Close() function there is a Return statement and I thought is enough...

Link to comment
Share on other sites

  • Moderators

taietel,

Just disable the LV header when you creat the additional GUI: :x

If $fColFlag Then  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ControlDisable($hForm, "", HWnd(_GUICtrlListView_GetHeader($hListView)))
    $fColFlag = False
    _GUI_Sort()
    ControlEnable($hForm, "", HWnd(_GUICtrlListView_GetHeader($hListView)))
EndIf

As to the handler question, you really do need to get out of the handler as soon as possible. Remember Windows uses all these messages to keep everything in sync - if you end up blocking a message you can really screw up the system quite quickly. You have a entire GUIGetMsg loop in the function you were calling - if that is not "blocking" then I do not know what is! :shifty:

The best way is to use a flag as I showed above - then the message handler is freed quickly and you can deal with the event in the main loop in slowere time where you interfere with nothing else. :P

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

taietel,

Always delighted to be of assistance. :x

And as a fully paid up member of the "Keyboard Protection League" I have a duty to prevent them from being injured by coders' foreheads! :P

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...