Jump to content

Read Value from checked ListView Item


Misuranai
 Share

Go to solution Solved by Nine,

Recommended Posts

Hi,

I'm not sure if I am just unable to use the search function but I have a small question concerning listview with checkboxes. I want to read the value from the checked item in the listview (for e.g. in a variable and then show the value in a msgbox).

I have an example GUI:

 

#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiListView.au3>


$Test = GUICreate("Test",554,274,-1,-1,-1,-1)
$list =  GUICtrlCreateListView("Test", 0,0,542,266,$LVS_SHOWSELALWAYS, BitOr($LVS_EX_FULLROWSELECT,$LVS_EX_CHECKBOXES,$WS_EX_CLIENTEDGE))
$item =  GUICtrlCreateListViewItem("Test Item", $list)
GUISetState(@SW_SHOW)


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

            
    EndSwitch
Wend

When I check the item ($item) the value "Test Item" should be saved in a variable like $value1. Do you have any ideas how to resolve it?

Thanks in Advanced.

Best regards

Link to comment
Share on other sites

There is a few ways to perform such a thing.  Here one of them :

#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiListView.au3>

Global $Test = GUICreate("Test", 554, 274, -1, -1, -1, -1)
Global $list = GUICtrlCreateListView("Test", 0, 0, 542, 266, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE))
Global $item1 = GUICtrlCreateListViewItem("Test Item 1", $list)
Global $item2 = GUICtrlCreateListViewItem("Test Item 2", $list)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, WM_NOTIFY)

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
  EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  Local $tStruct = DllStructCreate($tagNMHDR, $lParam)
  If GUICtrlGetHandle($list) = $tStruct.hwndFrom And $tStruct.Code = $LVN_ITEMCHANGED Then
    $tStruct = DllStructCreate($tagNMLISTVIEW, $lParam)
    ConsoleWrite("Item selected : " & _GUICtrlListView_GetItemText($list, $tStruct.item) & @CRLF)
    ConsoleWrite("Is checked : " & _GUICtrlListView_GetItemChecked($list, $tStruct.item) & @CRLF)
  EndIf
  Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Link to comment
Share on other sites

16 hours ago, Nine said:

There is a few ways to perform such a thing.  Here one of them :

#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiListView.au3>

Global $Test = GUICreate("Test", 554, 274, -1, -1, -1, -1)
Global $list = GUICtrlCreateListView("Test", 0, 0, 542, 266, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE))
Global $item1 = GUICtrlCreateListViewItem("Test Item 1", $list)
Global $item2 = GUICtrlCreateListViewItem("Test Item 2", $list)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, WM_NOTIFY)

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
  EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  Local $tStruct = DllStructCreate($tagNMHDR, $lParam)
  If GUICtrlGetHandle($list) = $tStruct.hwndFrom And $tStruct.Code = $LVN_ITEMCHANGED Then
    $tStruct = DllStructCreate($tagNMLISTVIEW, $lParam)
    ConsoleWrite("Item selected : " & _GUICtrlListView_GetItemText($list, $tStruct.item) & @CRLF)
    ConsoleWrite("Is checked : " & _GUICtrlListView_GetItemChecked($list, $tStruct.item) & @CRLF)
  EndIf
  Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Thanks for the example. Really appreciated. I have a script which is working directly with the Active Directory and I am listing the groups in a listview with checkboxes in an array. Isn't there any options to do like that:

For $i =  0 To UBound($groups) -1
        If _GUICtrlListView_GetItemChecked($list, $i) = True Then 
            ConsoleWrite(_GUICtrlListView_GetItemText($list, $i))
        Else 
            ConsoleWrite("False")
        EndIf

That's just an imagination from my side. It doesn't work for me so but It would be nice to realize this code.

 

Best regards

Link to comment
Share on other sites

Oookkk, target is moving...Hold on :

For $i =  0 To _GUICtrlListView_GetItemCount($list) - 1
  If _GUICtrlListView_GetItemChecked($list, $i) Then 
    ConsoleWrite(_GUICtrlListView_GetItemText($list, $i) & @CRLF)
  Else 
    ConsoleWrite("False" & @CRLF)
  EndIf
Next

 

Link to comment
Share on other sites

1 hour ago, Nine said:

Oookkk, target is moving...Hold on :

For $i =  0 To _GUICtrlListView_GetItemCount($list) - 1
  If _GUICtrlListView_GetItemChecked($list, $i) Then 
    ConsoleWrite(_GUICtrlListView_GetItemText($list, $i) & @CRLF)
  Else 
    ConsoleWrite("False" & @CRLF)
  EndIf
Next

 

Thank you so much! That is what I was looking for. I have just one more question. I will generate a list of the groups into the listview which are unchecked at first. I want to update the "ConsoleWrite" everytime when I check an item from the listview. For example: I check Item1 and then he should give me the ItemText in the output. Afterwards I check another Item for e.g. Item2. Then he should give me Item1 and Item2 in the ouput. How can I realize the code? My goal is to put the groups which is saved in "GetItemText" into an array so that I can add users to the outputted groups in a For loop by using a method.

Link to comment
Share on other sites

  • Solution

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