Jump to content

Row index of a controlID in ListView after sorting?


Logman
 Share

Recommended Posts

Hi, I have simple question. How to remain Index (Row) of a ControlID after sorting in ListView?

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Dim $nCurCol = -1
Dim $nSortDir = 1
Dim $bSet = 0
Dim $nCol = -1
Dim $MyArr[10]
Dim $CtlrID[10]

_dumpArr_for_test_only()

; GUI
GUICreate("Retrieve Row after sorting",220, 270)
$hListView = GUICtrlCreateListView ("colA|colB|colC|colD",10, 10, 200, 200)
$Btn_Ret  = GUICtrlCreateButton("Retrieve Row from $CtlrID[5]", 25, 230, 170, 25, 0)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
; create rows
For $i = 0 To UBound($MyArr) - 1
    $CtlrID[$i] = GUICtrlCreateListViewItem($MyArr[$i], $hListView) ;
Next
GUICtrlSetBkColor ($CtlrID[5], 0xff0000)
GUICtrlRegisterListViewSort($hListView, "LVSort")
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Btn_Ret
            msgbox (0,"Row No.", "RowIndex (Index) of $CtlrID[5] is: " & "??")  ;?? <= I need get Row (Index) from ControlID
        Case $hListView
            $bSet = 0
            $nCurCol = $nCol
            DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($hListView), "int", 0, "int", 1)
    EndSwitch
WEnd

; for example only
Func _dumpArr_for_test_only()
Local $bTmp = true, $sTmp
    For $i = 0 To UBound($MyArr) - 1
        Switch $i
            Case 2, 5, 7
                $sTmp = "Err"
            Case Else
                $sTmp = "OK"
        EndSwitch      
        $bTmp = Not $bTmp
        $MyArr[$i] = $i & "|" & UBound($MyArr)-$i-1  & "|" & $bTmp & "|" & $sTmp
    Next   
EndFunc

; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
    Local $tInfo = DllStructCreate($tagLVFINDINFO)
    DllStructSetData($tInfo, "Flags", $LVFI_PARAM)
     ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * - 1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn
     DllStructSetData($tInfo, "Param", $nItem1)
    $val1 = _GUICtrlListView_FindItem($hWnd, -1, $tInfo)
    DllStructSetData($tInfo, "Param", $nItem2)
    $val2 = _GUICtrlListView_FindItem($hWnd, -1, $tInfo)
    $val1 = _GUICtrlListView_GetItemText($hWnd, $val1, $nColumn)
    $val2 = _GUICtrlListView_GetItemText($hWnd, $val2, $nColumn)
    $nResult = 0 ; No change of item1 and item2 positions
    If $val1 < $val2 Then
        $nResult = -1 ; Put item2 before item1
    ElseIf $val1 > $val2 Then
        $nResult = 1 ; Put item2 behind item1
    EndIf
    $nResult = $nResult * $nSortDir
    Return $nResult
EndFunc   ;==>LVSort
Link to comment
Share on other sites

Hi, I have simple question. How to remain Index (Row) of a ControlID after sorting in ListView?

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Dim $nCurCol = -1
Dim $nSortDir = 1
Dim $bSet = 0
Dim $nCol = -1
Dim $MyArr[10]
Dim $CtlrID[10]

_dumpArr_for_test_only()

; GUI
GUICreate("Retrieve Row after sorting",220, 270)
$hListView = GUICtrlCreateListView ("colA|colB|colC|colD",10, 10, 200, 200)
$Btn_Ret  = GUICtrlCreateButton("Retrieve Row from $CtlrID[5]", 25, 230, 170, 25, 0)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
; create rows
For $i = 0 To UBound($MyArr) - 1
    $CtlrID[$i] = GUICtrlCreateListViewItem($MyArr[$i], $hListView) ;
Next
GUICtrlSetBkColor ($CtlrID[5], 0xff0000)
GUICtrlRegisterListViewSort($hListView, "LVSort")
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Btn_Ret
            msgbox (0,"Row No.", "RowIndex (Index) of $CtlrID[5] is: " & "??")  ;?? <= I need get Row (Index) from ControlID
        Case $hListView
            $bSet = 0
            $nCurCol = $nCol
            DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($hListView), "int", 0, "int", 1)
    EndSwitch
WEnd

; for example only
Func _dumpArr_for_test_only()
Local $bTmp = true, $sTmp
    For $i = 0 To UBound($MyArr) - 1
        Switch $i
            Case 2, 5, 7
                $sTmp = "Err"
            Case Else
                $sTmp = "OK"
        EndSwitch      
        $bTmp = Not $bTmp
        $MyArr[$i] = $i & "|" & UBound($MyArr)-$i-1  & "|" & $bTmp & "|" & $sTmp
    Next   
EndFunc

; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
    Local $tInfo = DllStructCreate($tagLVFINDINFO)
    DllStructSetData($tInfo, "Flags", $LVFI_PARAM)
     ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * - 1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn
     DllStructSetData($tInfo, "Param", $nItem1)
    $val1 = _GUICtrlListView_FindItem($hWnd, -1, $tInfo)
    DllStructSetData($tInfo, "Param", $nItem2)
    $val2 = _GUICtrlListView_FindItem($hWnd, -1, $tInfo)
    $val1 = _GUICtrlListView_GetItemText($hWnd, $val1, $nColumn)
    $val2 = _GUICtrlListView_GetItemText($hWnd, $val2, $nColumn)
    $nResult = 0 ; No change of item1 and item2 positions
    If $val1 < $val2 Then
        $nResult = -1 ; Put item2 before item1
    ElseIf $val1 > $val2 Then
        $nResult = 1 ; Put item2 behind item1
    EndIf
    $nResult = $nResult * $nSortDir
    Return $nResult
EndFunc   ;==>LVSort

Case $Btn_Ret
            For $i = 0 To _GUICtrlListView_GetItemCount($hListView) - 1
                $index = _GUICtrlListView_GetItemText($hListView, $i, 0)
                If $index = 5 Then
                    MsgBox(0, "Row No.", "RowIndex (Index) of $CtlrID[5] is: " & $i)
                    ExitLoop
                EndIf
            Next
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...