Sign in to follow this  
Followers 0
rajeshwaran

In ListView, how to select only the subitem, instead of selecting the whole row

6 posts in this topic

I have tried, many style combinations, but, I could not able to select the individual subitems in ListView.

However, I can able to select the first column, but I cant able to select other columns.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Post a short demo script of the last thing you tried and maybe we can tweak it to work as you want.

:)

P.S. Don't double post.

;)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

get selected item from column 1

_GUICtrlListView_GetItemText(GUICtrlGetHandle($ListView1),_GUICtrlListView_GetSelectedIndices($ListView1),0)

column1= 0 (<=index Number must be)

column2 =1 (<=index Number must be)

Index=n1-1

where n1 -eq to column number.

So lets say if you want to get selected item from column 2 :

_GUICtrlListView_GetItemText(GUICtrlGetHandle($ListView1),_GUICtrlListView_GetSelectedIndices($ListView1),1)

and etc.

Also i agree with PsaltyDS .

Without your script it`s enough hard to know where is your mistakes.

Please note that in future.

EDIT: correction

Edited by Fire

[size="5"] [/size]

Share this post


Link to post
Share on other sites

Hi Fire,

I can't able to understand, I want to know the column index and subitem index, please find below my sample script.

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>


;--- Global Variables --

;-- Window properties --
Global  $ID_Window
Global  $Window_Title = "Generator"
Global  $Window_Width = 700
Global  $Window_Height = 500

;-- ListView 1 properties --
Global  $ID_InputDevice_Gen_Config_ListView
Global  $Handle_InputDevice_Gen_Config_ListView

;--- Global Variables --
Global  $Array_InDev_Genrl_Config_XYWH [4] = _
                    [                   _ 
                        10,             _ ;-- Left --
                        10,             _ ;-- Top --
                        500,            _ ;-- Width --
                        300             _ ;-- Height --
                    ]

Global  $Array_InDev_Genrl_Config_Columns [6] = _
                    [                           _ 
                        "In. Device Name",      _
                        "Input Fs",             _
                        "Master Clck",          _
                        "Decoder Type",         _
                        "Dual Mono Mode",       _
                        "Down Mix Mode"         _
                    ]

Global  $Array_InDev_Genrl_Config_Items [18][6] =                                           _
                    [                                                                       _
                        [   "std0_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "std1_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "std2_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "std3_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "capa_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "capb_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "capc_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "capa_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "capb_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "capc_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "hdmi_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "hdmi_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "esf_01",       "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "esf_23",       "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "pcmf_01",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "pcmf_23",      "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "pcm_mem_01",   "48k",  "amclki0",  "ac3",  "and",  "mode0" ],  _
                        [   "pcm_mem_23",   "48k",  "amclki0",  "ac3",  "and",  "mode0" ]   _
                    ]

Global  $Array_InDev_InputFs_Combo_List [4] =       _
                    [                               _
                        "48k",                      _
                        "44.1k",                    _
                        "32k",                      _
                        "null"                      _
                    ]
                    
Global  $Array_InDev_MstrClck_Combo_List [7] =      _
                    [                               _
                        "amclki0",                  _
                        "stc0",                     _
                        "stc1",                     _
                        "stc2",                     _
                        "hdmi",                     _
                        "sif25",                    _
                        "null"                      _
                    ]                   

Global  $Array_InDev_DecType_Combo_List [9] =       _
                    [                               _
                        "ac3",                      _
                        "aac",                      _
                        "bc",                       _
                        "mp3",                      _
                        "lpcm",                     _
                        "ddp",                      _
                        "wma",                      _
                        "heaac",                    _
                        "null"                      _
                    ]                   

Global  $Array_InDev_DualMode_Combo_List [5] =      _
                    [                               _
                        "and",                      _
                        "dmain",                    _
                        "dsub",                     _
                        "mix",                      _
                        "null"                      _
                    ]                   

Global  $Array_InDev_DwnMix_Combo_List [5] =        _
                    [                               _
                        "mode0",                    _
                        "mode1",                    _
                        "multi",                    _
                        "auto",                     _
                        "null"                      _
                    ]                   

;-- Start Main Function --
Main()

Func Main()
    
    ;-- Create a window --
    $ID_Window = GUICreate($Window_Title, $Window_Width, $Window_Height, -1, -1) 
    
    ;-- Create ListView for input device general configuration --
    Create_ListView(    $ID_Window,                                 _
                        $ID_InputDevice_Gen_Config_ListView,        _
                        $Handle_InputDevice_Gen_Config_ListView,    _
                        $Array_InDev_Genrl_Config_XYWH,             _
                        $Array_InDev_Genrl_Config_Columns,          _
                        $Array_InDev_Genrl_Config_Items)

    GUISetState(@SW_SHOW)

    ;-- Register message for ListView notifications --
    GUIRegisterMsg($WM_NOTIFY, "ListView_EventHandler")

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

;-- Function to create a listview and assign items specified in the input array --
Func Create_ListView( ByRef $H_Window, ByRef $ID_ListView, ByRef $Handle_ListView, ByRef $Array_XYWH, ByRef $Array_Columns, ByRef $Array_Items)
    
    Local   $Array_Element
    Local   $ListView_Width = 0
    Local   $Loop = 0

    ;-- Create ListView --
    $ID_ListView = GUICtrlCreateListView(   "",                 _
                                            $Array_XYWH[0],     _
                                            $Array_XYWH[1],     _
                                            $Array_XYWH[2],     _
                                            $Array_XYWH[3],     _
                                            $LVS_SINGLESEL,     _
                                            $LVS_EX_GRIDLINES ) 
                                            
    $Handle_ListView = GUICtrlGetHandle($ID_ListView)
    
    ;-- Set event handler for list view --
    GUICtrlSetOnEvent($ID_ListView, "ListView_EventHandler")
    
    ;-- Add columns for ListView --
    For $Array_Element In $Array_Columns
        _GUICtrlListView_AddColumn($Handle_ListView, $Array_Element)
    Next
    
    ;-- Add items for ListView --
    _GUICtrlListView_AddArray($Handle_ListView, $Array_Items)

    ;-- Determine the column width --
    For $Loop = 0 To UBound($Array_Columns) Step 1
        Determine_ColumnWidth($Handle_ListView, $Loop)
    Next
    
    ;-- Center justify all columns --
    For $Loop = 0 To UBound($Array_Columns) Step 1
        _GUICtrlListView_JustifyColumn($Handle_ListView, $Loop + 1, 2)
    Next

EndFunc


Func Determine_ColumnWidth($Handle_ListView, $Column_Index = 0)
    
    Local $TotalNumberOfColumns = 0
    Local $AutoWidth_Of_DataItems = 0
    Local $AutoWidth_Of_Columns = 0
    
    ;-- Get column count
    Local $TotalNumberOfColumns = _GUICtrlListView_GetColumnCount($Handle_ListView)
    
    ;-- If column index passed by user is greater than total number of columns then do needful--
    If $Column_Index > $TotalNumberOfColumns Then $Column_Index = $TotalNumberOfColumns

    ;-- Set auto width for data items --
    _GUICtrlListView_SetColumnWidth($Handle_ListView, $Column_Index, $LVSCW_AUTOSIZE)

    ;-- Get auto width assigned by AutoIt (for later comparision) --
    $AutoWidth_Of_DataItems = _GUICtrlListView_GetColumn($Handle_ListView, $Column_Index)
    
    ;-- Set auto width for Columns Headers --
    _GUICtrlListView_SetColumnWidth($Handle_ListView, $Column_Index, $LVSCW_AUTOSIZE_USEHEADER)
    
    ;-- Get auto width assigned by AutoIt (for later comparision) --
    $AutoWidth_Of_Columns = _GUICtrlListView_GetColumn($Handle_ListView, $Column_Index)

    ;-- Compare and apply the suitable auto width to that column --
    If $AutoWidth_Of_DataItems[4] < $AutoWidth_Of_Columns[4] Then _GUICtrlListView_SetColumnWidth($Handle_ListView, $Column_Index, $LVSCW_AUTOSIZE_USEHEADER)
    If $AutoWidth_Of_DataItems[4] > $AutoWidth_Of_Columns[4] Then _GUICtrlListView_SetColumnWidth($Handle_ListView, $Column_Index, $LVSCW_AUTOSIZE)
        
EndFunc


Func ListView_EventHandler($hWnd, $Msg, $wParam, $lParam)
    
    Local   $tNotificationMessage 
    Local   $Handle_Control 
    Local   $Event_Occured
    
    ;-- From $lParam form the system structure $tagNMHDR --
    $tNotification_Message = DllStructCreate($tagNMHDR, $lParam)
    
    ;-- From the above structure formed, retrive the handle of the control and event occured --
    $Handle_Control = DllStructGetData($tNotification_Message, "hWndFrom")
    $Event_Occured = DllStructGetData($tNotification_Message, "Code")
    
    ;-- Classification of controls --
    Switch $Handle_Control
        
        ;-- Check notification for ListView control (General config for input device) --
        Case $Handle_InputDevice_Gen_Config_ListView
        
            ;-- Classification of events --
            Switch $Event_Occured
                
                ;-- Check for mouse single click event --
                Case $NM_CLICK
                
                    Local $tLV_NotificationMessage
                    
                    ;-- From $lParam form the system structure $tagNMLISTVIEW --
                    $tLV_NotificationMessage = DllStructCreate($tagNMLISTVIEW, $lParam)
                    
                    ;-- From the above structure formed, retrive the notification message --
                    ;-- and confirm the event occured for "SubItem" --
                    If DllStructGetData($tLV_NotificationMessage, "SubItem") <> 0 Then 
                        ListView_Handle_SubItem_Event($Handle_InputDevice_Gen_Config_ListView)
                    EndIf
                        
            EndSwitch ;-- End switch for Classification of events --
    
    EndSwitch ;-- End switch for Classification of controls --
    
    Return $GUI_RUNDEFMSG
EndFunc


Func ListView_Handle_SubItem_Event(ByRef $Handle_ListView)
    Local   $Array_HitInfo
    Local   $Column_Selected
    
    $Column_Selected = _GUICtrlListView_GetSelectedColumn($ID_InputDevice_Gen_Config_ListView)
    $Array_HitInfo = _GUICtrlListView_SubItemHitTest($Handle_ListView)
    
    ;-- If True, position is in control's client window but not on an item --
    If $Array_HitInfo[1] = -1 Then Return False
    
    ;-- For debug --    
    ConsoleWrite("Column Selected = " & $Column_Selected & " SubItem selected = " & $Array_HitInfo[0] & @LF)    
        
EndFunc

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I'm not sure if this would help you but you can make a workaround with this i think :

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Global $fDblClk = False
Global $aLV_Click_Info, $hTmp_Edit = 0
$dll = DllOpen("user32.dll")

$GUI = GUICreate("Bottin téléphonique personnel", 510, 450)
$NVT = _GUICtrlListView_Create($GUI, "", 5, 5, 500, 347)
_GUICtrlListView_InsertColumn($NVT, 0, "Selecting", 140, 2)
_GUICtrlListView_InsertColumn($NVT, 1, "tiem + edit", 130, 2)

_GUICtrlListView_AddItem($NVT, "Row 1: Col 1", 0)
_GUICtrlListView_AddSubItem($NVT,0, "Row 1: Col 1", 1,1)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    ; If a temporary edit exists
    If $hTmp_Edit <> 0 Then
    ; If ENTER pressed
        If _IsPressed("0D", $dll) Then
    ; Set label to edit content
            $sText = GUICtrlRead($hTmp_Edit)
            _GUICtrlListView_SetItemText($NVT, $aLV_Click_Info[0], $sText, $aLV_Click_Info[1]) 
    ; Delete temporary edit
            GUICtrlDelete($hTmp_Edit)
            $hTmp_Edit = 0
        EndIf
    EndIf

    ; If an item was double clicked
    If $fDblClk Then
        $fDblClk = False
    ; Delete any existing temporary edits
        GUICtrlDelete($hTmp_Edit)
    ;$hTmp_Edit = 0
    ; Read current text of clicked label
        $sTmp_Text = _GUICtrlListView_GetItemText($NVT, $aLV_Click_Info[0], $aLV_Click_Info[1])
    ; Get label position
    Switch $aLV_Click_Info[1]
        Case 0 ; On Item
            $aLV_Rect_Info = _GUICtrlListView_GetItemRect($NVT, $aLV_Click_Info[0], 2)
        Case Else ; On SubItem
            $aLV_Rect_Info = _GUICtrlListView_GetSubItemRect($NVT, $aLV_Click_Info[0], $aLV_Click_Info[1])
    EndSwitch
    ; Create temporary edit
            $hTmp_Edit = GUICtrlCreateEdit($sTmp_Text, $aLV_Rect_Info[0] + 5, $aLV_Rect_Info[1] + 5, $aLV_Rect_Info[2] - $aLV_Rect_Info[0], $aLV_Rect_Info[3] - $aLV_Rect_Info[1], 0)
            GUICtrlSetState($hTmp_Edit, $GUI_FOCUS)
    EndIf
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
    Case $NVT
    Switch $iCode
                Case $NM_DBLCLK ; Sent by a list-view control when the user double clicks an item with the left mouse button
                    $aLV_Click_Info = _GUICtrlListView_SubItemHitTest($NVT)
                    If $aLV_Click_Info[0] <> -1 Then $fDblClk = True
                    
    EndSwitch
        EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Edited by Jayson

Share this post


Link to post
Share on other sites

Your demo is a little over-wrought. Here's a simpler one:

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>

Global $hGUI = GUICreate("Test", 400, 400)
Global $idLV = GUICtrlCreateListView("Col_0|Col_1|Col_2|Col_3", 20, 20, 360, 360)
Global $hLV = GUICtrlGetHandle($idLV)
For $n = 0 To 3
    GUICtrlCreateListViewItem($n & "_0|" & $n & "_1|" & $n & "_2|" & $n & "_3", $idLV)
Next
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hLV ; Came from the ListView
            Switch $iCode
                Case $NM_CLICK ; Is a CLICK message
                    Local $tLVInfo = DllStructCreate($tagNMITEMACTIVATE, $lparam)
                    ConsoleWrite("ListView Click:  hWndFrom = " & $hWndFrom & "; Index = " & _
                        DllStructGetData($tLVInfo, "Index") & "; SubItem = " & _
                        DllStructGetData($tLVInfo, "SubItem") & @LF)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

When you catch a WM_NOTIFY message, you first see if it came from the control you are interested in, then see if the code matches the action you want (i.e. NM_CLICK). In the case of a ListView control, you then have to get the full NMITEMACTIVATE structure, which contains the index and subitem.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0