Jump to content

GUI Setup & How To Code it.


thawee
 Share

Recommended Posts

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

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
Link to comment
Share on other sites

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 )
Link to comment
Share on other sites

  • Moderators

thawee,

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

#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: :)

#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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

thawee,

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

#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: :)

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

Link to comment
Share on other sites

thawee,

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

#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?

Link to comment
Share on other sites

  • Moderators

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:

#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: :)

#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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

$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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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