Sign in to follow this  
Followers 0
leomoon

Dynamic GUI Problem

4 posts in this topic

Hello,

I'm writing a small dynamic GUI which will list the files inside a folder called 'apps' at the script location.

For every file in that folder, the GUI will make a check box with the name of that file. Now the problem is that I want to generate a list of checked boxes to do some tasks but as soon as I press the button, it will give me numbers!

Any help is greatly appreciated. ;)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>

$folderToList = 'apps/'
$vSpacer = 24
$hSpacer = 16
$tasks = ''
$noFilesFound = ''

$appList = _FileListToArray($folderToList, "*", 1)
If @error = 1 Then
    Exit
EndIf

If $appList = '' Then
    $noFilesFound = 'No files found inside "apps\" folder.'
EndIf

;~ _ArrayDisplay($appList)
$autoAppLister = GUICreate("Auto App Lister", 370, 370, 192, 124)
$appListGrp = GUICtrlCreateGroup("App List", 8, 8, 353, 313)
If $noFilesFound <> '' Then
    $noFilesLbl = GUICtrlCreateLabel($noFilesFound, 16, 152, 318, 28, $SS_CENTER)
    GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
Else
    If $appList <> '' Then
        For $i = 1 To $appList[0]
            $appList[$i] = GUICtrlCreateCheckbox($appList[$i], $hSpacer, $vSpacer, 161, 17)
            $vSpacer += 16
            If $vSpacer > 300 Then
                $vSpacer = 24
                $hSpacer = 192
            EndIf
        Next
    EndIf
EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)
If $appList <> '' Then
    $installBtn = GUICtrlCreateButton("Show List Of Checked Boxes", 8, 328, 353, 33, $WS_GROUP)
Else
    $installBtn = GUICtrlCreateButton("Show List Of Checked Boxes", 8, 328, 353, 33, $WS_GROUP)
    GUICtrlSetState(-1, $GUI_DISABLE)
EndIf
GUISetState(@SW_SHOW)

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

Func _listCheck()
    For $i = 1 To $appList[0]
        If GUICtrlRead($appList[$i]) = 1 Then
            $tasks &= $appList[$i] & @LF
        EndIf
    Next
    $okPressed = MsgBox(0,'',$tasks)
    If $okPressed = 1 Then
        $tasks = ''
    EndIf
EndFunc   ;==>_listCheck

Share this post


Link to post
Share on other sites



Hi, the numbers that are returned are Control ID's of the selected checkboxes, use GUICtrlRead($appList[$i], 1) to get the text of the checkbox.eg:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>

$folderToList = @ScriptDir & '\apps\'
$vSpacer = 24
$hSpacer = 16
$tasks = ''
$noFilesFound = ''

$appList = _FileListToArray($folderToList, "*", 1)
If @error = 1 Then
    Exit
EndIf

If $appList = '' Then
    $noFilesFound = 'No files found inside "apps\" folder.'
EndIf

;~ _ArrayDisplay($appList)
$autoAppLister = GUICreate("Auto App Lister", 370, 370, 192, 124)
$appListGrp = GUICtrlCreateGroup("App List", 8, 8, 353, 313)
If $noFilesFound <> '' Then
    $noFilesLbl = GUICtrlCreateLabel($noFilesFound, 16, 152, 318, 28, $SS_CENTER)
    GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
Else
    If $appList <> '' Then
        For $i = 1 To $appList[0]
            $appList[$i] = GUICtrlCreateCheckbox($appList[$i], $hSpacer, $vSpacer, 161, 17)
            $vSpacer += 16
            If $vSpacer > 300 Then
                $vSpacer = 24
                $hSpacer = 192
            EndIf
        Next
    EndIf
EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)
If $appList <> '' Then
    $installBtn = GUICtrlCreateButton("Show List Of Checked Boxes", 8, 328, 353, 33, $WS_GROUP)
Else
    $installBtn = GUICtrlCreateButton("Show List Of Checked Boxes", 8, 328, 353, 33, $WS_GROUP)
    GUICtrlSetState(-1, $GUI_DISABLE)
EndIf
GUISetState(@SW_SHOW)

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

Func _listCheck()
    For $i = 1 To $appList[0]
        If GUICtrlRead($appList[$i]) = 1 Then
            $tasks &= $folderToList & GUICtrlRead($appList[$i], 1) & @LF
        EndIf
    Next
    $okPressed = MsgBox(0,'Files Selected', $tasks)
    If $okPressed = 1 Then
        $tasks = ''
    EndIf
EndFunc   ;==>_listCheck

Cheers

Share this post


Link to post
Share on other sites

leomoon,

You were overwriting your list of files when you created the checkboxes. :) So when you tried to read the name of the file, you got the ControlID instead.

This shows how to do it: ;)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>

$folderToList = @ScriptDir
$vSpacer = 24
$hSpacer = 16
$tasks = ''
$noFilesFound = ''
Global $chkList[100]

$appList = _FileListToArray($folderToList, "*", 1)
If @error = 1 Then
    Exit
EndIf

Global $chkList[$appList[0] + 1] ; Declare an array to hold the checkbox ControlIDs

If $appList = '' Then
    $noFilesFound = 'No files found inside "apps\" folder.'
EndIf

;~ _ArrayDisplay($appList)
$autoAppLister = GUICreate("Auto App Lister", 370, 370, 192, 124)
$appListGrp = GUICtrlCreateGroup("App List", 8, 8, 353, 313)
If $noFilesFound <> '' Then
    $noFilesLbl = GUICtrlCreateLabel($noFilesFound, 16, 152, 318, 28, $SS_CENTER)
    GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
Else
    If $appList <> '' Then
        For $i = 1 To $appList[0]
            $chkList[$i] = GUICtrlCreateCheckbox($appList[$i], $hSpacer, $vSpacer, 161, 17) ; Save the ControlIds on the new array 
            $vSpacer += 16
            If $vSpacer > 300 Then
                $vSpacer = 24
                $hSpacer = 192
            EndIf
        Next
    EndIf
EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)

$installBtn = GUICtrlCreateButton("Show List Of Checked Boxes", 8, 328, 353, 33, $WS_GROUP)
If $appList = '' Then GUICtrlSetState(-1, $GUI_DISABLE) ; Is it not more elegant?

GUISetState(@SW_SHOW)

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

Func _listCheck()
    For $i = 1 To $appList[0]
        If GUICtrlRead($chkList[$i]) = 1 Then ; Check the checkboxes
            $tasks &= $appList[$i] & @LF ; Add the text from the file list
        EndIf
    Next
    $okPressed = MsgBox(0, '', $tasks)
    If $okPressed = 1 Then
        $tasks = ''
    EndIf
EndFunc   ;==>_listCheck

Note also the rather more elegant syntax for disabling your button. :P

Please ask if you have any questions. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

AWESOME!

I learn new stuff here EVERY DAY!

The new way of disabling button is definitely MORE elegant and a lot easier to read. ;)

I'm using smashly's script with the elegant way of disabling the button by Melba23 because Melba23's script will give error if there are no files in the directory @ line # 20 where it says: "Global $chkList[$appList[0] + 1] ; Declare an array to hold the checkbox ControlIDs".

Thanks a lot. Here is the final script for anyone who want a dynamic GUI sample:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>

$folderToList = @ScriptDir & '\apps\'
$vSpacer = 24
$hSpacer = 16
$tasks = ''
$noFilesFound = ''

$appList = _FileListToArray($folderToList, "*", 1)
If @error = 1 Then
    Exit
EndIf

If $appList = '' Then
    $noFilesFound = 'No files found inside "apps\" folder.'
EndIf

;~ _ArrayDisplay($appList)
$autoAppLister = GUICreate("Auto App Lister", 370, 370)
$appListGrp = GUICtrlCreateGroup("App List", 8, 8, 353, 313)
If $noFilesFound <> '' Then
    $noFilesLbl = GUICtrlCreateLabel($noFilesFound, 16, 152, 318, 28, $SS_CENTER)
    GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
Else
    If $appList <> '' Then
        For $i = 1 To $appList[0]
            $appList[$i] = GUICtrlCreateCheckbox($appList[$i], $hSpacer, $vSpacer, 161, 17)
            $vSpacer += 16
            If $vSpacer > 300 Then
                $vSpacer = 24
                $hSpacer = 192
            EndIf
        Next
    EndIf
EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)
$installBtn = GUICtrlCreateButton("Show The List Of Selected Check Boxes", 8, 328, 353, 33, $WS_GROUP)
If $appList = '' Then GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)

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

Func _listCheck()
    For $i = 1 To $appList[0]
        If GUICtrlRead($appList[$i]) = 1 Then
            $tasks &= $folderToList & GUICtrlRead($appList[$i], 1) & @LF
        EndIf
    Next
    If $tasks <> '' Then
        $okPressed = MsgBox(0,'Files Selected', $tasks)
    Else
        $okPressed = MsgBox(0,'Oops!','Nothing selected!')
    EndIf
    If $okPressed = 1 Then
        $tasks = ''
    EndIf
EndFunc   ;==>_listCheck
Edited by leomoon

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