Jump to content

Get Selected Row Number in ListView


 Share

Recommended Posts

I am looking for a way to get the Row Number when a cell is selected.

The problem with the below code is, if 2 or more than 2 Cells contain the same number, all the later cells tend to open the same file reading from the 1st cell instead of the selected row. I think after the line 'Local $ListView_Results = ....', I will have to put a check 'If $ListView_Results = $R1C1_result AND selected Row = 'Row number' then Open the specific file.

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <GuiConstants.au3>;Inclusion file for the GUI interface controls
#include <WinAPI.au3>
#include <FontConstants.au3>

Global Const $WM_LV_CLICK = $WM_USER + 1

Global $Child_GUI, $R1C1_result, $R1C2_result, $R2C1_result, $R2C2_result, $GUI, $hListview, $StatusBar1, $aHit[2] = [-1, -1] ; $aHit contains row & col of marked cell
Global $iButton1 = 9999
Global $iButton2 = 9999
Global $Get_Child_GUI2 = 9999
Global $files[3][3]

$files[1][1] = @DesktopDir & "\Test1\test1.txt"
$files[1][2] = @DesktopDir & "\Test2\test2.txt"
$files[2][1] = @DesktopDir & "\Test3\test3.txt"
$files[2][2] = @DesktopDir & "\Test4\test4.txt"


$Parent_GUI = GUICreate("Parent GUI", 640, 665, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU))
Local $Get_Default_Label_ForAll_Controls = GUICtrlCreateLabel("", 8, 16, 620, 400, $WS_BORDER)
GUICtrlSetState(-1, $GUI_DISABLE)


    Local $R1C1 = FileRead($files[1][1])
    Local $R1C1_result = $R1C1

    Local $R1C2 = FileRead($files[1][2])
    Local $R1C2_result = $R1C2

    Local $R2C1 = FileRead($files[2][1])
    Local $R2C1_result = $R2C1

    Local $R2C2 = FileRead($files[2][2])
    Local $R2C2_result = $R2C2

    $Child_GUI = GUICtrlCreateListView("| Column 1| Column 2|", 80, 56, 498, 350, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ; Compliance Incidents Header Label
    $hListview = GUICtrlGetHandle($Child_GUI)

    ; Add items
    _GUICtrlListView_AddItem($Child_GUI, "Row 1", 0)
    _GUICtrlListView_AddSubItem($Child_GUI, 0, 12, 1) ; The data comes from $R1C1_result
    _GUICtrlListView_AddSubItem($Child_GUI, 0, 48, 2) ; The data comes from $R1C2_result

    _GUICtrlListView_AddItem($Child_GUI, "Row 2", 1)
    _GUICtrlListView_AddSubItem($Child_GUI, 1, 12, 1) ; The data comes from $R2C1_result
    _GUICtrlListView_AddSubItem($Child_GUI, 1, 120, 2) ; The data comes from $R2C2_result

Local $Get_Child_GUI2 = GUICtrlCreateButton("Get GUI 2", 100, 608, 75, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetState($Child_GUI, $GUI_HIDE)

    Local $iButton1 = GUICtrlCreateButton("Open Files", 376, 608, 75, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetState(-1, $GUI_HIDE)

Local $iButton2 = GUICtrlCreateButton("Hide Child GUI", 472, 608, 75, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetState(-1, $GUI_HIDE)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Get_Child_GUI2 ; This will get the ListView visible with single cell click enable
            Example()

        Case $iButton1 ; This will allow the dir to open when single cells are clicked
                Local $ListView_Results = _GUICtrlListView_GetItemText($hListview, $aHit[0], $aHit[1])
                If $ListView_Results = $R1C1_result Then
                    ShellExecute(@DesktopDir & "\Test1\test1.txt") ; For sampling please '12' inside test1.txt
                EndIf

                If $ListView_Results = $R1C2_result Then
                ShellExecute(@DesktopDir & "\Test2\test2.txt")
                EndIf

                If $ListView_Results = $R2C1_result Then
                ShellExecute(@DesktopDir & "\Test3\test3.txt") ; For sampling please '12' inside test3.txt
                EndIf

                If $ListView_Results = $R2C2_result Then
                ShellExecute(@DesktopDir & "\Test4\test4.txt")
                EndIf

        Case $iButton2 ; This will hide the Child GUI
            GUICtrlSetState($Child_GUI, $GUI_HIDE)

    EndSwitch
WEnd



Func Example()

; Do required actions here...

GUICtrlSetState($Child_GUI, $GUI_SHOW)

GUICtrlSetState($iButton1, $GUI_SHOW)

GUICtrlSetState($iButton2, $GUI_SHOW)

EndFunc   ;==>Example


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListview
            Switch $iCode
                Case $LVN_ITEMCHANGED
                    Local $tNMLISTVIEW, $iItem, $sInfo
                    $tNMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
                    $iItem = DllStructGetData($tNMLISTVIEW, "Item")
                    _GUICtrlListView_SetItemSelected($hListview, $iItem, False)
                    _GUICtrlListView_SetItemState($hListview, $iItem, 0, $LVIS_FOCUSED)
                    Local $sInfo = GUIGetCursorInfo($Parent_GUI)
                    If $sInfo[2] Then
                        $sInfo = _GUICtrlListView_SubItemHitTest($hListview, $sInfo[0] - 80, $sInfo[1] - 56) ; Upper left = ( 200, 310 )
                        If $sInfo[0] > -1 And $sInfo[1] > -1 And $sInfo[0] = $iItem Then
                            If $aHit[0] > -1 Then _GUICtrlListView_RedrawItems($hListview, $aHit[0], $aHit[0])
                            If $aHit[0] <> $sInfo[0] Or $aHit[1] <> $sInfo[1] Then
                                $aHit[0] = $sInfo[0] ; Row
                                $aHit[1] = $sInfo[1] ; Col
                            Else
                                $aHit[0] = -1 ; Row
                                $aHit[1] = -1 ; Col
                            EndIf
                            _GUICtrlListView_RedrawItems($hListview, $iItem, $iItem)
                        EndIf
                    EndIf

                Case $NM_CUSTOMDRAW
                    Local $tNMLVCUSTOMDRAW = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $dwDrawStage = DllStructGetData($tNMLVCUSTOMDRAW, "dwDrawStage")
                    Switch $dwDrawStage ; Holds a value that specifies the drawing stage
                        Case $CDDS_PREPAINT ; Before the paint cycle begins
                            Return $CDRF_NOTIFYITEMDRAW ; Notify the parent window of any ITEM-related drawing operations
                        Case $CDDS_ITEMPREPAINT ; Before painting an item
                            Return $CDRF_NOTIFYSUBITEMDRAW ; Notify the parent window of any SUBITEM-related drawing operations
                        Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM) ; Before painting a subitem
                            Local $dwItemSpec = DllStructGetData($tNMLVCUSTOMDRAW, "dwItemSpec") ; Item index
                            Local $iSubItem = DllStructGetData($tNMLVCUSTOMDRAW, "iSubItem") ; Subitem index
                            Local $uItemState = DllStructGetData($tNMLVCUSTOMDRAW, "uItemState") ; Item state
                            If $dwItemSpec = $aHit[0] Then ; Marked row
                                Switch $iSubItem
                                    Case $aHit[1] ; Marked column
                                        DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", 0xFFFFFF) ; Forecolor white
                                        DllStructSetData($tNMLVCUSTOMDRAW, "clrTextBk", 0xCC6600) ; Backcolor dark blue, BGR

                                    Case Else ; Other columns
                                        DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", 0x000000) ; Forecolor black
                                        DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF) ; Backcolor white
                                EndSwitch
                            Else ; Other rows
                                DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", 0x000000)
                                DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF)
                            EndIf
                            Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
#EndRegion - Declare Non-Compliant Incidents Variables

I looked at the below page but it always gives the selected cell as '-27'.

 

Can someone please help.

Edited by DigDeep
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...