Jump to content

check which item is selected in a ListView control


Zomp
 Share

Recommended Posts

Which is the simplest way to check which item is selected in a ListView control just after it has been selected?

I would not want to use a loop to scan every item in the ListView, but I have also recognized that I cannot use the identifier of the ListView with GUIGetmsg().

I have read in some past threads on this forum some examples which use the GUIRegisterMsg($WM_NOTIFY,...) function. Can I ask if there is something simpler?

At present I'm using the following lines just after a select ... endselect code about GUIGetmsg():

$ItemLine=stringsplit(Guictrlread(GUICtrlRead($ViewListControlInGUI),0),"|")
$ItemName=$ItemLine[1]
Edited by Zomp
Link to comment
Share on other sites

#NoTrayIcon 
#include <GUIConstants.au3>
#include <GUIListView.au3>

$Form1 = GUICreate("Test", 300, 200)
$ListView1 = GUICtrlCreateListView("A|B|C", 5, 5, 290, 175, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_NOLABELWRAP,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT))
GUICtrlCreateListViewItem('r1c1|r1c2|r1c3', $ListView1)
GUICtrlCreateListViewItem('r2c1|r2c2|r2c3', $ListView1)
GUICtrlCreateListViewItem('r3c1|r3c2|r3c3', $ListView1)
$status1 = GUICtrlCreateLabel("", 0, 183, 300, 17, $WS_GROUP, $WS_EX_CLIENTEDGE)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

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

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
 #forceref $hWndGUI, $MsgID, $wParam
 Local $NMHDR, $NMLVDISPINFO, $NMLISTVIEW, $event
 
 $NMHDR = DllStructCreate($tagNMHDR, $lParam)
 $event = DllStructGetData($NMHDR, 'Code')
 
 If $hWndGUI = $Form1 And $wParam = $ListView1 Then
    Select
        Case $LVN_ITEMCHANGED
            $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
            If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then OnStateChange()
    EndSelect
 EndIf
 Return $GUI_RUNDEFMSG
EndFunc

Func OnStateChange()
 $cur_sel = _GUICtrlListView_GetNextItem($ListView1) ; current selected
 $count_sel = _GUICtrlListView_GetSelectedCount($ListView1)
 $count = _GUICtrlListView_GetItemCount($ListView1)
 
 GUICtrlSetData($status1, 'Current selected: ' & $cur_sel & ' Total selected: ' & $count_sel & ' Total items: ' & $count)
EndFunc

Edited by Zedna
Link to comment
Share on other sites

  • 2 years later...

#NoTrayIcon 
#include <GUIConstants.au3>
#include <GUIListView.au3>

$Form1 = GUICreate("Test", 300, 200)
$ListView1 = GUICtrlCreateListView("A|B|C", 5, 5, 290, 175, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_NOLABELWRAP,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT))
GUICtrlCreateListViewItem('r1c1|r1c2|r1c3', $ListView1)
GUICtrlCreateListViewItem('r2c1|r2c2|r2c3', $ListView1)
GUICtrlCreateListViewItem('r3c1|r3c2|r3c3', $ListView1)
$status1 = GUICtrlCreateLabel("", 0, 183, 300, 17, $WS_GROUP, $WS_EX_CLIENTEDGE)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

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

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
 #forceref $hWndGUI, $MsgID, $wParam
 Local $NMHDR, $NMLVDISPINFO, $NMLISTVIEW, $event
 
 $NMHDR = DllStructCreate($tagNMHDR, $lParam)
 $event = DllStructGetData($NMHDR, 'Code')
 
 If $hWndGUI = $Form1 And $wParam = $ListView1 Then
    Select
        Case $LVN_ITEMCHANGED
            $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
            If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then OnStateChange()
    EndSelect
 EndIf
 Return $GUI_RUNDEFMSG
EndFunc

Func OnStateChange()
 $cur_sel = _GUICtrlListView_GetNextItem($ListView1) ; current selected
 $count_sel = _GUICtrlListView_GetSelectedCount($ListView1)
 $count = _GUICtrlListView_GetItemCount($ListView1)
 
 GUICtrlSetData($status1, 'Current selected: ' & $cur_sel & ' Total selected: ' & $count_sel & ' Total items: ' & $count)
EndFunc

Wich Auto-it version are using to use this code ?

i got Always this error message :

\DEV\LDAP\GUI_DEVEX.au3(9,141) : WARNING: $WS_HSCROLL: possibly used before declaration.

$ListView1 = GUICtrlCreateListView("A|B|C", 5, 5, 290, 175, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_NOLABELWRAP,$WS_HSCROLL,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

\DEV\LDAP\GUI_DEVEX.au3(9,153) : WARNING: $WS_VSCROLL: possibly used before declaration.

$ListView1 = GUICtrlCreateListView("A|B|C", 5, 5, 290, 175, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_NOLABELWRAP,$WS_HSCROLL,$WS_VSCROLL,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

\LDAP\GUI_DEVEX.au3(9,164) : WARNING: $WS_BORDER: possibly used before declaration.

$ListView1 = GUICtrlCreateListView("A|B|C", 5, 5, 290, 175, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_NOLABELWRAP,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

\DEV\LDAP\GUI_DEVEX.au3(9,190) : WARNING: $WS_EX_CLIENTEDGE: possibly used before declaration.

$ListView1 = GUICtrlCreateListView("A|B|C", 5, 5, 290, 175, BitOR($LVS_REPORT,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_NOLABELWRAP,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

\DEV\LDAP\GUI_DEVEX.au3(13,61) : WARNING: $WS_GROUP: possibly used before declaration.

$status1 = GUICtrlCreateLabel("", 0, 183, 300, 17, $WS_GROUP,

Edited by Reekod
Link to comment
Share on other sites

A simple search in the Help would have led you to use #include <WindowsConstants.au3> as the 3rd include.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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...