Jump to content



Photo

GUI Setup & How To Code it.


  • Please log in to reply
13 replies to this topic

#1 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 23 April 2010 - 12:56 PM

I am wondering if there is a simple way to do this.

I would like to have a GUI that has several general options, but also maybe when i click and advanced tab i can put checks into check boxes to activate the additional features.

I am not sure how I would read in if the check box is checked and or what is the most efficient way to process my code.

For Example
AutoIt         
While 1 Select         Case $nMsg = $Radio1            ; ======================= Section 1 ==================================                      radios($off)                        ;Greys out the radio buttons.             Do              `               ; ---- MAIN LOOP -----                 $nMsg = GUIGetMsg()                 If $nMsg = $Button1 Then                     GUICtrlSetState($Button1, $GUI_DISABLE)                     program1($norbs)                                  EndIf             Until $nMsg = $Button2  Or $nMsg = $GUI_EVENT_CLOSE Or $status = "Done"             Closeout($norbs)         Case $nMsg = $Radio2                ;===================== Section 2 =========================                  radios($off)                        ; Greys out the radio buttons.             Do                              ; ---- MAIN LOOP -----                 $nMsg = GUIGetMsg()                 If $nMsg = $Button1 Then                     GUICtrlSetState($Button1, $GUI_DISABLE)                     program2($norbs)                                    EndIf             Until $nMsg = $Button2 Or $nMsg = $GUI_EVENT_CLOSE Or $status = "Done"             Closeout($norbs)     EndSelect     If $nMsg = $GUI_EVENT_CLOSE Then         ExitLoop     EndIf WEnd


What this section of code does is waits for you to select a radio button. Once you do that it will wait for you to click button 1 before it does anything.

The problem I am having is if i want different versions or more advanced options I don't know how to write it into my code without having so much duplicate code its ridiculous. Section 1 and Section 2 are nearly identical!! in function but because i don't know how to mesh the code in section 1 and section 2 it stays separate.

What i would like is to have one case statement with precise controls that funneled down the GuiGetmsg inputs to the correct actions.

I keep thinking there is a simple way to watch for key or mouse inputs and recording them into the correct variables without having tons of duplicate code.

So for example in my previous code does anyone have an idea how i could mesh in the ability to read check boxes for advanced settings on the programs1() and 2 functions?

or for that matter how does the check box feature work in the gui? is it just a simple $nMsg = GUIGetMsg() ? but not sure how to assign it to a variable?

Thanks everyone

I am still learning how to code gui inputs and such. Is there a place i can learn the best practices on coding behind the scenes of a gui?

Hope i provided enough information.

Edited by thawee, 23 April 2010 - 01:02 PM.






#2 FinalVersion

FinalVersion

    0 ^ 1

  • Active Members
  • PipPipPipPipPipPip
  • 599 posts

Posted 23 April 2010 - 01:04 PM

Not sure I understand that correctly, but to get the current state of Radio / Checkbox control, use GUICtrlRead(). Check the HelpFile for states.

#3 PhilHibbs

PhilHibbs

    Adventurer

  • Active Members
  • PipPip
  • 141 posts

Posted 23 April 2010 - 01:17 PM

I would like to have a GUI that has several general options, but also maybe when i click and advanced tab i can put checks into check boxes to activate the additional features.

I usually do this not with tabs but with an expand button that makes the window bigger and reveals the extra controls. I also have an un-expand button that is the same position and shape as the expand button, and each of the two buttons will hide itself and un-hide the other when clicked.
            Case $bExpand                 _ClientResize( $hGUI, $WinWidth*2, $WinHeight )                 GUICtrlSetState( $bExpand, $GUI_HIDE )                 GUICtrlSetState( $bImpand, $GUI_SHOW )             Case $bImpand                 _ClientResize( $hGUI, $WinWidth, $WinHeight )                 GUICtrlSetState( $bImpand, $GUI_HIDE )                 GUICtrlSetState( $bExpand, $GUI_SHOW )


#4 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,305 posts

Posted 23 April 2010 - 02:59 PM

thawee,

If you want to use tabs, you can do it like this: :idea:
AutoIt         
#include <GUIConstantsEx.au3> $hGUI = GUICreate("Test", 200, 200) $hTab = GUICtrlCreateTab(10, 10, 180, 180) $hTab0 = GUICtrlCreateTabItem("Tab 0") $hButton = GUICtrlCreateButton("Go", 20, 50, 80, 30) $hTab1 = GUICtrlCreateTabItem("Tab 1") $hCheckBox_1 = GUICtrlCreateCheckBox(" Option 1", 20, 50, 100, 20) $hCheckBox_2 = GUICtrlCreateCheckBox(" Option 2", 20, 90, 100, 20) GUICtrlCreateTabItem("")    ; end tabitem definition GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             If GUICtrlRead($hCheckBox_1) = 1 And GUICtrlRead($hCheckBox_2) = 1 Then                 MsgBox(0, "Oops!", "Please only check one box!")             ElseIf GUICtrlRead($hCheckBox_1) = 1 Then                 MsgBox(0, "Run", "Running Option 1")             ElseIf GUICtrlRead($hCheckBox_2) = 1 Then                 MsgBox(0, "Run", "Running Option 2")             Else                 MsgBox(0, "Oops!", "Please check a box!")             EndIf     EndSwitch WEnd

If you like PhilHibbs' idea of expanding the GUI, you can also do it with just the one button like this: :)
AutoIt         
#include <GUIConstantsEx.au3> ; Keep positions when resizing Opt("GUIResizeMode", $GUI_DOCKALL) $hGUI = GUICreate("Test", 200, 200) $hButton = GUICtrlCreateButton("Expand", 10, 10, 80, 30) GUICtrlCreateButton("Dummy", 10, 400, 80, 30) GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             Switch GUICtrlRead($hButton)                 Case "Expand"                     GUICtrlSetData($hButton, "Contract")                     WinMove($hGUI, "", Default, Default, 200, 500)                 Case Else                     GUICtrlSetData($hButton, "Expand")                     WinMove($hGUI, "", Default, Default, 200, 200)             EndSwitch     EndSwitch WEnd

If you do go the expansion route - whichever way you decide to do it - do not forget the Opt("GUIResizeMode", $GUI_DOCKALL) line or you will find your controls moving all over the place when the resizing occurs. :(

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#5 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 23 April 2010 - 09:21 PM

thawee,

If you want to use tabs, you can do it like this: :idea:

AutoIt         
#include <GUIConstantsEx.au3> $hGUI = GUICreate("Test", 200, 200) $hTab = GUICtrlCreateTab(10, 10, 180, 180) $hTab0 = GUICtrlCreateTabItem("Tab 0") $hButton = GUICtrlCreateButton("Go", 20, 50, 80, 30) $hTab1 = GUICtrlCreateTabItem("Tab 1") $hCheckBox_1 = GUICtrlCreateCheckBox(" Option 1", 20, 50, 100, 20) $hCheckBox_2 = GUICtrlCreateCheckBox(" Option 2", 20, 90, 100, 20) GUICtrlCreateTabItem("")    ; end tabitem definition GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             If GUICtrlRead($hCheckBox_1) = 1 And GUICtrlRead($hCheckBox_2) = 1 Then                 MsgBox(0, "Oops!", "Please only check one box!")             ElseIf GUICtrlRead($hCheckBox_1) = 1 Then                 MsgBox(0, "Run", "Running Option 1")             ElseIf GUICtrlRead($hCheckBox_2) = 1 Then                 MsgBox(0, "Run", "Running Option 2")             Else                 MsgBox(0, "Oops!", "Please check a box!")             EndIf     EndSwitch WEnd

If you like PhilHibbs' idea of expanding the GUI, you can also do it with just the one button like this: :)
AutoIt         
#include <GUIConstantsEx.au3> ; Keep positions when resizing Opt("GUIResizeMode", $GUI_DOCKALL) $hGUI = GUICreate("Test", 200, 200) $hButton = GUICtrlCreateButton("Expand", 10, 10, 80, 30) GUICtrlCreateButton("Dummy", 10, 400, 80, 30) GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             Switch GUICtrlRead($hButton)                 Case "Expand"                     GUICtrlSetData($hButton, "Contract")                     WinMove($hGUI, "", Default, Default, 200, 500)                 Case Else                     GUICtrlSetData($hButton, "Expand")                     WinMove($hGUI, "", Default, Default, 200, 200)             EndSwitch     EndSwitch WEnd

If you do go the expansion route - whichever way you decide to do it - do not forget the Opt("GUIResizeMode", $GUI_DOCKALL) line or you will find your controls moving all over the place when the resizing occurs. :(

M23


Thanks so much everyone.

I will look over the information that was posted.

#6 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 23 April 2010 - 09:28 PM

thawee,

If you want to use tabs, you can do it like this: :idea:

AutoIt         
#include <GUIConstantsEx.au3> $hGUI = GUICreate("Test", 200, 200) $hTab = GUICtrlCreateTab(10, 10, 180, 180) $hTab0 = GUICtrlCreateTabItem("Tab 0") $hButton = GUICtrlCreateButton("Go", 20, 50, 80, 30) $hTab1 = GUICtrlCreateTabItem("Tab 1") $hCheckBox_1 = GUICtrlCreateCheckBox(" Option 1", 20, 50, 100, 20) $hCheckBox_2 = GUICtrlCreateCheckBox(" Option 2", 20, 90, 100, 20) GUICtrlCreateTabItem("")    ; end tabitem definition GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             If GUICtrlRead($hCheckBox_1) = 1 And GUICtrlRead($hCheckBox_2) = 1 Then                 MsgBox(0, "Oops!", "Please only check one box!")             ElseIf GUICtrlRead($hCheckBox_1) = 1 Then                 MsgBox(0, "Run", "Running Option 1")             ElseIf GUICtrlRead($hCheckBox_2) = 1 Then                 MsgBox(0, "Run", "Running Option 2")             Else                 MsgBox(0, "Oops!", "Please check a box!")             EndIf     EndSwitch WEnd


If you do go the expansion route - whichever way you decide to do it - do not forget the Opt("GUIResizeMode", $GUI_DOCKALL) line or you will find your controls moving all over the place when the resizing occurs. :)

M23

Now what if i want to have more than one option checked?

How do i write clean code without writing tones of duplicate code?

I would like to have a tab or window where i can check tons of extra settings, but i don't want to have to create a control statement for each one.

say if i was automating a check disk program, and i wanted to have the option to run a basic check disk or force a check disk at reboot, or look for bad sectors.

does something like that have to be in a select case or can you have one main loop and drill down the loop to sub loops based on the options chosen?

#7 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,305 posts

Posted 24 April 2010 - 07:32 AM

thawee,

First, when you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. :idea:

Now to the questions:

Now what if i want to have more than one option checked?

One way is to do it like this:
AutoIt         
#include <GUIConstantsEx.au3> $hGUI = GUICreate("Test", 200, 200) $hTab = GUICtrlCreateTab(10, 10, 180, 180) $hTab0 = GUICtrlCreateTabItem("Tab 0") $hButton = GUICtrlCreateButton("Go", 20, 50, 80, 30) $hTab1 = GUICtrlCreateTabItem("Tab 1") $hCheckBox_1 = GUICtrlCreateCheckBox(" Option 1", 20,  50, 100, 20) $hCheckBox_2 = GUICtrlCreateCheckBox(" Option 2", 20,  80, 100, 20) $hCheckBox_3 = GUICtrlCreateCheckBox(" Option 3", 20, 110, 100, 20) $hCheckBox_4 = GUICtrlCreateCheckBox(" Option 4", 20, 140, 100, 20) GUICtrlCreateTabItem("")    ; end tabitem definition GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             $sParameters = ""             If GUICtrlRead($hCheckBox_1) = 1 Then $sParameters &= "Option 1 "             If GUICtrlRead($hCheckBox_2) = 1 Then $sParameters &= "Option 2 "             If GUICtrlRead($hCheckBox_3) = 1 Then $sParameters &= "Option 3 "             If GUICtrlRead($hCheckBox_4) = 1 Then $sParameters &= "Option 4"             If $sParameters = "" Then                 MsgBox(0, "Oops!", "Please check a box!")             Else                 MsgBox(0, "Run", "Running " & $sParameters)             EndIf     EndSwitch WEnd
In the example you chose (ChkDsk) you just add the relevant switches to the list when the checkboxes are ticked.

I would like to have a tab or window where i can check tons of extra settings, but i don't want to have to create a control statement for each one.

I do not really follow you here. You have to write a statement for each checkbox or it will not exist! :) Similarly you have to write at least one statement to read it - or there is little point in having created it.

What you might do is to have an array which holds the values associated with each checkbox and then use loops like this: :)
AutoIt         
#include <GUIConstantsEx.au3> Global $aCheckValues[4][2] = [[9999, "Option 1"], [9999, "Option 2"], [9999, "Option 3"], [9999, "Option 4"]] $hGUI = GUICreate("Test", 200, 200) $hTab = GUICtrlCreateTab(10, 10, 180, 180) $hTab0 = GUICtrlCreateTabItem("Tab 0") $hButton = GUICtrlCreateButton("Go", 20, 50, 80, 30) $hTab1 = GUICtrlCreateTabItem("Tab 1") For $i = 0 To 3     $aCheckValues[$i][0] = GUICtrlCreateCheckBox($aCheckValues[$i][1], 20,  50 + (30 * $i), 100, 20) Next GUICtrlCreateTabItem("")    ; end tabitem definition GUISetState() While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit         Case $hButton             $sParameters = ""             For $i = 0 To 3                 If GUICtrlRead($aCheckValues[$i][0]) = 1 Then $sParameters &= $aCheckValues[$i][1] & " "             Next             If $sParameters = "" Then                 MsgBox(0, "Oops!", "Please check a box!")             Else                 MsgBox(0, "Run", "Running " & $sParameters)             EndIf     EndSwitch WEnd
Setting up the array takes a bit of effort but it does give you what you might consider as "cleaner" code - which seems to be one of your main requirements. :) Of course, you could also fill the array with function names and use them to run other sections of code if required - the options are limited only by your imagination. :)

By the way - looking at the number of posts you have - if you are not too used to arrays, I recommend the tutorial in the Wiki.

Does that help at all? :(

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#8 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 24 April 2010 - 01:05 PM

Thanks a bunch much appreciated it will take me a min to figure out your code :0

I am still very new to autoit and i am a basic level script writer.

I will think about my posts and see if i can make something more clear :idea:

#9 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 24 April 2010 - 01:26 PM

            $sParameters = ""             For $i = 0 To 3                 If GUICtrlRead($aCheckValues[$i][0]) = 1 Then $sParameters &= $aCheckValues[$i][1] & " "             Next

Looking at the code above is $aCheckValues[$i][0] a multidimensional array? I think i understand whats going on based on your previous post that wasn't written this way.

You are reading in each array element, in the multi dimensional array 0

$aCheckValues[1][0]
$aCheckValues[2][0]
$aCheckValues[3][0]

for the number of check boxes created.

then your assigning the new variable to

$aCheckValues[1][1]
$aCheckValues[2][1]
$aCheckValues[3][1]

of your second array of $aCheckValues

while adding spaces to clarify the output.

Does this sound correct?

thanks for that example by the way it was stellar.

Edited by thawee, 24 April 2010 - 01:28 PM.


#10 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,305 posts

Posted 24 April 2010 - 02:02 PM

thawee,

is $aCheckValues[$i][0] a multidimensional array?

Sure is! :)

The first dimension [#][0] is filled with the ControlIDs of the checkboxes as we create them so we can read them later on. The second dimension [#][1] if preset with values when the array is declared.

So in the loop you have highlighted, we use the [#][0] elements to tell GUIControlRead what to read, and the [#][1] elements to fill the $sParameters variable.

Did you read the tutorial in the Wiki? Understanding arrays is a pretty important part of any form of coding - they can make your life so easy at times. :idea:

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#11 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 24 April 2010 - 02:48 PM

yeah I finished reading the wiki, and it was a very good overview.

I am still finding myself slightly confused on what data is stored where in a multi dimensional array.

 Local  $arr[3][3] = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]  Local $i, $j  For $i = 0 to UBound( $arr, 1) - 1    For $j = 0 to UBound($arr, 2) - 1       ConsoleWrite("$arr[" & $i & "][" & $j & "]:=" & $arr[$i][$j] & @LF)    Next    ConsoleWrite(@LF)  Next


this little snippet seems to really go through it; however, the output is still kinda of confusing.

$arr[0][0]:=1
$arr[0][1]:=2
$arr[0][2]:=3

$arr[1][0]:=2
$arr[1][1]:=3
$arr[1][2]:=4

$arr[2][0]:=3
$arr[2][1]:=4
$arr[2][2]:=5

It seems like from what was explained it you can access any element in the array you specified.

This example seems a little less controlled that your example of the data from the read statements of the check boxes. Maybe that explains my confusion on knowing where the data will be, so i know where to retrieve the data when needed.

hehe so much to take in all at once.

Still just trying to get the granulated understanding :idea:

Thanks a bunch, your help as been greatly appreciated. :)

Edited by thawee, 24 April 2010 - 02:57 PM.


#12 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 24 April 2010 - 03:10 PM

I have been meaning to ask is there a step through for autoit.

I remember using a scripting language that went line by line and highlighted each one as it was being processed. It also waited for you to confirm for it to go forward with the next line, so you could see how the code was acting.

#13 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,305 posts

Posted 24 April 2010 - 03:32 PM

thawee,

hehe so much to take in all at once

Too true! :(

Think of a 2D array as a grid:
1st Dim  [0] [1] [2] < 2nd dimension v [0]       1   2   3 [1]       2   3   4 [2]       3   4   5
Does that help? :idea:

is there a step through for autoit

Search for "+graphical +debugger". There have been a number of examples, but most of them do not work well with Vista up because of the security restriction on accessing the memory space of another process. :)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#14 thawee

thawee

    Wayfarer

  • Active Members
  • Pip
  • 55 posts

Posted 25 April 2010 - 03:24 AM

yes it does!

Thanks so much :idea:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users