Jump to content
Sign in to follow this  
mrmacadamia

Getting Selected Item from Listview or Treeview

Recommended Posts

mrmacadamia

Hi guys,

been looking around for days looking for solution but as always, doesn't found what I'm really looking.

I try to get list from listview I created(actually, modified from jennico's script) and try to

save it in .ini file.

Anyway, I'm trying to build a script that I can select folders(not files) and perform task on all folders that being listed. I know I can use other method but I want to try and learn using listview instead.

#include <date.au3>
#include <string.au3>
#include <IE.au3>
#include <Base64.au3>
#include <WinApi.au3>
#include <Inet.au3>
#include "CompInfo.au3"
#include <File.au3>
#include <Process.au3>
#include <Crypt.au3>
#include <Misc.au3>
#include "GUIEnhance.au3"
#include <GuiComboBox.au3>
#include<GuiStatusBar.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListviewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiImageList.au3>
#include <GuiTreeView.au3>
#include <GuiListView.au3>

Global Const $pv = "PendingFileRenameOperations", $pe = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager"
Global $iStyleJust = BitOR($WS_MINIMIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS)
Global $title = "Setting", $item[1], $items = 0, $popup = 0, $start = 0, $listview
Global $GUI

    $avi2 = GUICtrlCreateAvi("shell32.dll", 164, 160, 416, 300, 60)
    GUICtrlSetResizing(-1, 834)
    GUICtrlSetState(-1, 32)

    $label = GUICtrlCreateLabel("", 170, 422, 450, 54)
    GUICtrlSetResizing(-1, 582)


_ChooseYourFolder()

Func _ChooseYourFolder()

    $GUI = GUICreate($title & "  c  2010 by Guugul", 492, 335, Default, Default, $WS_MAXIMIZEBOX + $WS_MINIMIZEBOX + $WS_SIZEBOX)

    GUICtrlSetFont(GUICtrlCreateGroup("Select your folder", 10, 15, 470, 255), 8.5, 800)
    GUICtrlSetResizing(-1, 550)
    $listview = GUICtrlCreateListView("x|x", 15, 30, 405, 220, $LVS_NOCOLUMNHEADER + $LVS_SINGLESEL + $LVS_SHOWSELALWAYS, $LVS_EX_FULLROWSELECT + $LVS_EX_GRIDLINES); + $LVS_EX_CHECKBOXES)

    GUICtrlSetFont(-1, 8.5, 800)
    GUICtrlSetResizing(-1, 550)

    GUIRegisterMsg($WM_SIZE, "_WM_SIZE")

    $btnadd = GUICtrlCreateButton("Add", 425, 160, 50, 26)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnremove = GUICtrlCreateButton("Remove", 425, 190, 50, 26)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnPerform= GUICtrlCreateButton("Perform", 425, 230, 50, 26)
    GUICtrlSetFont(-1, 8.5, 800)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnDoSave = GUICtrlCreateButton("OK", 275, 275, 100)
    $ReturnMenu = GUICtrlCreateButton("Cancel", 380, 275, 100)
    GUISetState()

    While 1
        $gMsg = GUIGetMsg()
        If GUICtrlRead($listview) And GUICtrlGetState($btnremove) = 144 Then GUICtrlSetState($btnremove, 64)
        If GUICtrlRead($listview) = 0 And GUICtrlGetState($btnremove) = 80 Then GUICtrlSetState($btnremove, 128)
        Switch $gMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($GUI)
                ExitLoop

            Case $btnadd
                _Add()

            Case $btnremove
                _Remove()

            Case $btnPerform
                _PerformTask()

            Case $btnDoSave
                Exit

            Case $ReturnMenu
                GUIDelete($GUI)
                ExitLoop
        EndSwitch
    WEnd

EndFunc

Func _Add()
    $x = FileSelectFolder("Select directory to search", "", 0, "", $GUI)
    If $x = "" Or StringInStr($x, ":\") = 0 Then Return
    If StringRight($x, 1) = "\"  Then $x = StringTrimRight($x, 1)
    For $j = 1 To $items
        $input = StringSplit(GUICtrlRead($item[$j - 1]), "|")
        If $input[0] > 1 And $x = $input[2] Then Return
    Next
    $items += 1
    ReDim $item[$items]
    $item[$items - 1] = GUICtrlCreateListViewItem("|" & $x, $listview)
    ConsoleWrite($x & @CRLF);

    For $i = 1 To $items; this is the one i'm trying, instead of creating new value, it overwrite all value.
    IniWrite(@ScriptDir & "\test.ini", "FolderList", "FolderName" & "_" & $i, $x)
    Next


EndFunc


Func _Remove()
    $x = GUICtrlRead($listview)
    If $x > 0 Then GUICtrlDelete($x)
    For $j = 1 To $items
        $input = StringSplit(GUICtrlRead($item[$j - 1]), "|")
        If $input[0] = 3 Then Return
    Next
    GUICtrlSetTip($listview, "")

EndFunc   ;==>_Remove

Func _PerformTask()
        ;This one doesn't be the problem, i can create myself
        ;read the ini files
    ;do something with all listed folders
EndFunc

Func _WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
    $pos = WinGetClientSize($GUI)
    _GUICtrlListView_SetColumnWidth ($listview, 1, $pos[0] - 130)
    ;ControlMove($GUI, "", $listview, 5, 118, $pos[0] - 9, $pos[1] - 203)
EndFunc   ;==>_WM_SIZE

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Dim $tNMHDR = DllStructCreate($tagNMHDR, $ilParam), $iCode = DllStructGetData($tNMHDR, "Code")
    If HWnd(DllStructGetData($tNMHDR, "hWndFrom")) = $listview Then
       ; If $iCode = $NM_DBLCLK Then Return _Open()
        If $iCode = $NM_RCLICK Then
            $popup = 1
            Return 0
        EndIf
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_NOTIFY

p/s: Don't bother the include files. I know it's a LOT. I'm too lazy to remove it.

Edited by mrmacadamia

Share this post


Link to post
Share on other sites
ajit

_GUICtrlListView_GetItemSelected($hWnd, $iIndex)

Ajit

Share this post


Link to post
Share on other sites
mrmacadamia

_GUICtrlListView_GetItemSelected($hWnd, $iIndex)

Ajit

I don't need to wait to be selected to perform the task.

Share this post


Link to post
Share on other sites
ajit

If I am getting you question right, which has nothing to do with your topic title then this may help.

$result = IniReadSection ( @ScriptDir & "\test.ini", "FolderList" )

A 2D array will be returned where $result[0][0] will be the number of items already existing in your ini file.

Then Add $result[0][0] to $i so that a new Key is created in your Ini file.

Thus, IniWrite(@ScriptDir & "\test.ini", "FolderList", "FolderName" & "_" & ($i + $result[0][0]), $x)

Edited by ajit

Share this post


Link to post
Share on other sites
wolf9228

Hi guys,

been looking around for days looking for solution but as always, doesn't found what I'm really looking.

I try to get list from listview I created(actually, modified from jennico's script) and try to

save it in .ini file.

Anyway, I'm trying to build a script that I can select folders(not files) and perform task on all folders that being listed. I know I can use other method but I want to try and learn using listview instead.

#include <date.au3>
#include <string.au3>
#include <IE.au3>
#include <Base64.au3>
#include <WinApi.au3>
#include <Inet.au3>
#include "CompInfo.au3"
#include <File.au3>
#include <Process.au3>
#include <Crypt.au3>
#include <Misc.au3>
#include "GUIEnhance.au3"
#include <GuiComboBox.au3>
#include<GuiStatusBar.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListviewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiImageList.au3>
#include <GuiTreeView.au3>
#include <GuiListView.au3>

Global Const $pv = "PendingFileRenameOperations", $pe = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager"
Global $iStyleJust = BitOR($WS_MINIMIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS)
Global $title = "Setting", $item[1], $items = 0, $popup = 0, $start = 0, $listview
Global $GUI

    $avi2 = GUICtrlCreateAvi("shell32.dll", 164, 160, 416, 300, 60)
    GUICtrlSetResizing(-1, 834)
    GUICtrlSetState(-1, 32)

    $label = GUICtrlCreateLabel("", 170, 422, 450, 54)
    GUICtrlSetResizing(-1, 582)


_ChooseYourFolder()

Func _ChooseYourFolder()

    $GUI = GUICreate($title & "  c  2010 by Guugul", 492, 335, Default, Default, $WS_MAXIMIZEBOX + $WS_MINIMIZEBOX + $WS_SIZEBOX)

    GUICtrlSetFont(GUICtrlCreateGroup("Select your folder", 10, 15, 470, 255), 8.5, 800)
    GUICtrlSetResizing(-1, 550)
    $listview = GUICtrlCreateListView("x|x", 15, 30, 405, 220, $LVS_NOCOLUMNHEADER + $LVS_SINGLESEL + $LVS_SHOWSELALWAYS, $LVS_EX_FULLROWSELECT + $LVS_EX_GRIDLINES); + $LVS_EX_CHECKBOXES)

    GUICtrlSetFont(-1, 8.5, 800)
    GUICtrlSetResizing(-1, 550)

    GUIRegisterMsg($WM_SIZE, "_WM_SIZE")

    $btnadd = GUICtrlCreateButton("Add", 425, 160, 50, 26)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnremove = GUICtrlCreateButton("Remove", 425, 190, 50, 26)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnPerform= GUICtrlCreateButton("Perform", 425, 230, 50, 26)
    GUICtrlSetFont(-1, 8.5, 800)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnDoSave = GUICtrlCreateButton("OK", 275, 275, 100)
    $ReturnMenu = GUICtrlCreateButton("Cancel", 380, 275, 100)
    GUISetState()

    While 1
        $gMsg = GUIGetMsg()
        If GUICtrlRead($listview) And GUICtrlGetState($btnremove) = 144 Then GUICtrlSetState($btnremove, 64)
        If GUICtrlRead($listview) = 0 And GUICtrlGetState($btnremove) = 80 Then GUICtrlSetState($btnremove, 128)
        Switch $gMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($GUI)
                ExitLoop

            Case $btnadd
                _Add()

            Case $btnremove
                _Remove()

            Case $btnPerform
                _PerformTask()

            Case $btnDoSave
                Exit

            Case $ReturnMenu
                GUIDelete($GUI)
                ExitLoop
        EndSwitch
    WEnd

EndFunc

Func _Add()
    $x = FileSelectFolder("Select directory to search", "", 0, "", $GUI)
    If $x = "" Or StringInStr($x, ":\") = 0 Then Return
    If StringRight($x, 1) = "\"  Then $x = StringTrimRight($x, 1)
    For $j = 1 To $items
        $input = StringSplit(GUICtrlRead($item[$j - 1]), "|")
        If $input[0] > 1 And $x = $input[2] Then Return
    Next
    $items += 1
    ReDim $item[$items]
    $item[$items - 1] = GUICtrlCreateListViewItem("|" & $x, $listview)
    ConsoleWrite($x & @CRLF);

    For $i = 1 To $items; this is the one i'm trying, instead of creating new value, it overwrite all value.
    IniWrite(@ScriptDir & "\test.ini", "FolderList", "FolderName" & "_" & $i, $x)
    Next


EndFunc


Func _Remove()
    $x = GUICtrlRead($listview)
    If $x > 0 Then GUICtrlDelete($x)
    For $j = 1 To $items
        $input = StringSplit(GUICtrlRead($item[$j - 1]), "|")
        If $input[0] = 3 Then Return
    Next
    GUICtrlSetTip($listview, "")

EndFunc   ;==>_Remove

Func _PerformTask()
        ;This one doesn't be the problem, i can create myself
        ;read the ini files
    ;do something with all listed folders
EndFunc

Func _WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
    $pos = WinGetClientSize($GUI)
    _GUICtrlListView_SetColumnWidth ($listview, 1, $pos[0] - 130)
    ;ControlMove($GUI, "", $listview, 5, 118, $pos[0] - 9, $pos[1] - 203)
EndFunc   ;==>_WM_SIZE

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Dim $tNMHDR = DllStructCreate($tagNMHDR, $ilParam), $iCode = DllStructGetData($tNMHDR, "Code")
    If HWnd(DllStructGetData($tNMHDR, "hWndFrom")) = $listview Then
       ; If $iCode = $NM_DBLCLK Then Return _Open()
        If $iCode = $NM_RCLICK Then
            $popup = 1
            Return 0
        EndIf
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_NOTIFY

p/s: Don't bother the include files. I know it's a LOT. I'm too lazy to remove it.

#include <date.au3>
#include <string.au3>
#include <IE.au3>
;#include <Base64.au3>
#include <WinApi.au3>
#include <Inet.au3>
;#include "CompInfo.au3"
#include <File.au3>
#include <Process.au3>
#include <Crypt.au3>
#include <Misc.au3>
;#include "GUIEnhance.au3"
#include <GuiComboBox.au3>
#include<GuiStatusBar.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListviewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiImageList.au3>
#include <GuiTreeView.au3>
#include <GuiListView.au3>

Global Const $pv = "PendingFileRenameOperations", $pe = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager"
Global $iStyleJust = BitOR($WS_MINIMIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS)
Global $title = "Setting", $item[1], $items = 0, $popup = 0, $start = 0, $listview
Global $GUI

    $avi2 = GUICtrlCreateAvi("shell32.dll", 164, 160, 416, 300, 60)
    GUICtrlSetResizing(-1, 834)
    GUICtrlSetState(-1, 32)

    $label = GUICtrlCreateLabel("", 170, 422, 450, 54)
    GUICtrlSetResizing(-1, 582)


_ChooseYourFolder()

Func _ChooseYourFolder()

    $GUI = GUICreate($title & "  c  2010 by Guugul", 492, 335, Default, Default, $WS_MAXIMIZEBOX + $WS_MINIMIZEBOX + $WS_SIZEBOX)

    GUICtrlSetFont(GUICtrlCreateGroup("Select your folder", 10, 15, 470, 255), 8.5, 800)
    GUICtrlSetResizing(-1, 550)
    $listview = GUICtrlCreateListView("x|x", 15, 30, 405, 220, $LVS_NOCOLUMNHEADER + $LVS_SINGLESEL + $LVS_SHOWSELALWAYS, $LVS_EX_FULLROWSELECT + $LVS_EX_GRIDLINES); + $LVS_EX_CHECKBOXES)

    GUICtrlSetFont(-1, 8.5, 800)
    GUICtrlSetResizing(-1, 550)

    GUIRegisterMsg($WM_SIZE, "_WM_SIZE")

    $btnadd = GUICtrlCreateButton("Add", 425, 160, 50, 26)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnremove = GUICtrlCreateButton("Remove", 425, 190, 50, 26)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnPerform= GUICtrlCreateButton("Perform", 425, 230, 50, 26)
    GUICtrlSetFont(-1, 8.5, 800)
    GUICtrlSetResizing(-1, 804)
    GUICtrlSetCursor(-1, 0)

    $btnDoSave = GUICtrlCreateButton("OK", 275, 275, 100)
    $ReturnMenu = GUICtrlCreateButton("Cancel", 380, 275, 100)
    GUISetState()
    While 1
        $gMsg = GUIGetMsg()
        If GUICtrlRead($listview) And GUICtrlGetState($btnremove) = 144 Then GUICtrlSetState($btnremove, 64)
        If GUICtrlRead($listview) = 0 And GUICtrlGetState($btnremove) = 80 Then GUICtrlSetState($btnremove, 128)
        Switch $gMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($GUI)
                ExitLoop

            Case $btnadd
                _Add()

            Case $btnremove
                _Remove()

            Case $btnPerform
                _PerformTask()

            Case $btnDoSave
                Exit

            Case $ReturnMenu
                GUIDelete($GUI)
                ExitLoop
        EndSwitch
    WEnd

EndFunc

Func _Add()
    $x = FileSelectFolder("Select directory to search", "", 0, "", $GUI)
    If @error Then Return
    $items += 1
    ReDim $item[$items]
    For $j = 0 To $items - 1
        $input = StringReplace(GUICtrlRead($item[$j]), "|","")
        If StringUpper($x) == StringUpper($input) Then Return
    Next
    $item[$items - 1] = GUICtrlCreateListViewItem("|" & $x, $listview)
    ConsoleWrite($x & @CRLF);
     ; this is the one i'm trying, instead of creating new value, it overwrite all value.
    IniWrite(@ScriptDir & "\test.ini", "FolderList", "FolderName" & "_" & $items, $x)
EndFunc


Func _Remove()
    $x = GUICtrlRead($listview)
    $i = 0
    Dim $Tempitem[1]
    If $x <> 0 Then
    $SectionName = "FolderList"
    $SectionData = ""
    For $j = 0 To $items - 1
    If $x = $item[$j] Then
    GUICtrlDelete($x)
    Else
    $i = $i + 1
    ReDim $Tempitem[$i]
    $Tempitem[$i - 1] = $item[$j]
    $input = StringReplace(GUICtrlRead($item[$j]), "|","")
    $Key = "FolderName" & "_" & $i & " = " ; =
    $Value = $input
    $SectionData = $SectionData & $Key & $Value & @LF ; @LF Line feed
    EndIf
    Next
    $item = $Tempitem
    $items = $items - 1
    IniWriteSection(@ScriptDir & "\test.ini",$SectionName,$SectionData)
    EndIf

;For $j = 0 To $items -1
;$input = StringReplace(GUICtrlRead($item[$j]), "|","")
;If $input = 3 Then Return
;Next
;GUICtrlSetTip($listview, "")

EndFunc   ;==>_Remove

Func _PerformTask()

For $j = 0 To $items - 1
GUICtrlDelete($item[$j])
Next

$SectionName = "FolderList"
$var = IniReadSection(@ScriptDir & "\test.ini", $SectionName)
If @error Then
;Error occurred, probably no INI file

Else
Dim $item[1]
$items = 0
For $i = 1 To $var[0][0]
$Key = $var[$i][0] ; "FolderName" & "_" & $items
$Value = $var[$i][1] ; $x
$x = $Value
$items += 1
ReDim $item[$items]
$item[$items - 1] = GUICtrlCreateListViewItem("|" & $x, $listview)
Next
EndIf
EndFunc

Func _WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
    $pos = WinGetClientSize($GUI)
    _GUICtrlListView_SetColumnWidth ($listview, 1, $pos[0] - 130)
    ;ControlMove($GUI, "", $listview, 5, 118, $pos[0] - 9, $pos[1] - 203)
EndFunc   ;==>_WM_SIZE

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Dim $tNMHDR = DllStructCreate($tagNMHDR, $ilParam), $iCode = DllStructGetData($tNMHDR, "Code")
    If HWnd(DllStructGetData($tNMHDR, "hWndFrom")) = $listview Then
       ; If $iCode = $NM_DBLCLK Then Return _Open()
        If $iCode = $NM_RCLICK Then
            $popup = 1
            Return 0
        EndIf
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_NOTIFY
Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites
mrmacadamia

That is totally genius wolf9228. Never thought that $items can play role in this situation.

Don't have any idea what was the $items is all about, clueless.

Eventhough I didn't ask to create other function, you kindly created and modified the _Remove() and the _PerformTask() function to suit the script. Thanks a lot.

I owe you man.

There's more things I need to learn here.

Thanks. :mellow:

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.