Sign in to follow this  
Followers 0
IvanCodin

Reuse GUI Button Names

10 posts in this topic

#1 ·  Posted (edited)

I have a quesion that may be obvious to everyone.

It does not appear button names are significent in scripts. My tests show I can use the same button name in different GUI's without any issue. Only the active GUI and specified button action is ran when the button is pressed. I wrote an example to show what I am talking about. In the example below $Button1_1 is used in both $hGUI1 and $hGUI2. The button action is different based on the active GUI. Is this a good practice or should I take the time to create a unique button number for each GUI?

I want the GUI window "remember" it's position when I switch betenn GUI windows. I have a case statement that does this for me but when I have a large number of GUI's, one of my scripts has 20, it a real pain to add them. Are there better ways to do this?

I am definately not a programmer but want to make sure I am using the correct method when i write my scripts. You opinion is appreciated!!!!

ME

#include <GUIConstantsEX.au3>
Opt('GUIOnEventMode',True)
Global $Width = ('200')
Global $Height = ('200')
HotKeySet("^x", "_Bye")


$hGUI1 = GUICreate("My Tools - FirstGUI", $Width, $Height, 270, 98)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $hGUI1)
$Context_Menu = GUICtrlCreateContextMenu()
$Item_1 = GUICtrlCreateMenuItem("About", $Context_Menu)
GUICtrlSetOnEvent($Item_1, "_About")
$Item_2 = GUICtrlCreateMenuItem("Exit", $Context_Menu)
GUICtrlSetOnEvent($Item_2, "_Exit")
$Button1_1 = GUICtrlCreateButton("Test 1", 50, 50, 89, 23, 0)
GUICtrlSetOnEvent($Button1_1, "Test1")
$Button1_2 = GUICtrlCreateButton("Switch GUI", 50, 90, 89, 23, 0)
GUICtrlSetOnEvent($Button1_2, "SwitchGUI")


$hGUI2 = GUICreate("My Tools - SecondGUI", $Width, $Height, 270, 98)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $hGUI2)
$Context_Menu = GUICtrlCreateContextMenu()
$Item_1 = GUICtrlCreateMenuItem("About", $Context_Menu)
GUICtrlSetOnEvent ($Item_1,"_About")
$Item_2 = GUICtrlCreateMenuItem("Exit", $Context_Menu)
GUICtrlSetOnEvent ($Item_2,"_Exit")
$Button1_1 = GUICtrlCreateButton("Test 2", 50, 50, 89, 23, 0)
GUICtrlSetOnEvent($Button1_1,"Test2")
$Button2_2 = GUICtrlCreateButton("Switch GUI", 50, 90, 89, 23, 0)
GUICtrlSetOnEvent($Button2_2, "SwitchGUI")
GUISetState(@SW_SHOW, $hGUI1)

While 1
    $msg = GUIGetMsg()
    If $msg = $Item_2 Or $msg = -3 Or $msg = -1 Then
        ExitLoop
    EndIf
    Sleep(20) ; Idle around
WEnd

Func SwitchGUI()
    Switch @GUI_CtrlId
        Case $Button1_2 ; Main Form to Second Form
            $Pos = WinGetPos($hGUI1)
            WinMove($hGUI2, "", $Pos[0], $Pos[1])
            GUISetState(@SW_Hide, $hGUI1)
            GUISetState(@SW_SHOW, $hGUI2)
        Case $Button2_2 ; Second Form Return to Main
            $Pos = WinGetPos($hGUI2)
            WinMove($hGUI1, "", $Pos[0], $Pos[1])
            GUISetState(@SW_Hide, $hGUI2)
            GUISetState(@SW_SHOW, $hGUI1)
        Case Else
            GUISetState(@SW_Hide, $hGUi2)
            GUISetState(@SW_SHOW, $hGUI1)
        EndSwitch
EndFunc

Func Test1()
    MsgBox(4096, "Info", "Test 1 GUI", 3)
EndFunc

Func Test2()
    MsgBox(4096, "Info", "Test 2 GUI", 3)
EndFunc

Func Spare()
    MsgBox(4096, "Info", "Not yet implemented", 3)
EndFunc ;==> Spare

Func _Bye()
    $return_value = MsgBox(292,"Close Application", "Do you want to close me?",10)
Switch $return_value
    case 1 ;OK ---> Flags: 0, 1
        ;MsgBox(0, "Return Value", "OK")
    case 2 ;cancel ---> Flags: 1, 3, 5, 6
        ;MsgBox(0, "Return Value", "Cancel")
    case 3 ;abort ---> Flags: 2
        ;MsgBox(0, "Return Value", "Abort")
    case 4 ;retry ---> Flags: 2, 5
        ;MsgBox(0, "Return Value", "Retry")
    case 5 ;Ignore ---> Flags: 2
        ;MsgBox(0, "Return Value", "Ignore")
    case 6 ;Yes ---> Flags: 3, 4
        ;MsgBox(0, "Return Value", "Yes")
        _Exit()
    case 7 ;No ---> Flags: 3, 4
        ;MsgBox(0, "Return Value", "No")
        Return
    case 10 ;Try Again ---> Flags: 6
        ;MsgBox(0, "Return Value", "Try again")
    case 11 ;Continue ---> Flags: 6
        ;MsgBox(0, "Return Value", "Continue")
EndSwitch
    ;MsgBox(1, "Bye", "Exiting program", 4)
    ;_Exit()
EndFunc   ;==>_Bye

Func _About()
    MsgBox(0, "About", "My GUI test app", 4)
EndFunc   ;==>_About

Func _Exit()
    GUIDelete($hGUI1)
    GUIDelete($hGUI2)
Exit
EndFunc   ;==>_Exit
Edited by IvanCodin

Share this post


Link to post
Share on other sites



I have a quesion that may be obvious to everyone.

It does not appear button names are significent in scripts. My tests show I can use the same button name in different GUI's without any issue. Only the active GUI and specified button action is ran when the button is pressed. I wrote an example to show what I am talking about. In the example below $Button1_1 is used in both $hGUI1 and $hGUI2. The button action is different based on the active GUI. Is this a good practice or should I take the time to create a unique button number for each GUI?

I want the GUI window "remember" it's position when I switch betenn GUI windows. I have a case statement that does this for me but when I have a large number of GUI's, one of my scripts has 20, it a real pain to add them. Are there better ways to do this?

I am definately not a programmer but want to make sure I am using the correct method when i write my scripts. You opinion is appreciated!!!!

ME

Each GUI's Control IDs are sequentially numbered from 1 as they are created. The only reason your script works is because you have the exact same number of controls in the same order in both GUIs. So they coincidentally get the same ControlID. If one GUI has more/less controls before $Button1_1 than the other, they would have different control IDs and it would start to go wrong from there.

If you learn to put your GUIs in Event Mode, you can use the same function to handle common tasks and not even have to keep track of the control IDs, because the handling function recieves the ControlID, the control handle, and the window handle as macros. See GuiOnEventMode under Opt() in the help file.

>_<


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I'll readup on GUIOnEventMode.

Thanks for the response!

Share this post


Link to post
Share on other sites

OK I looked at the documentation and it looks like I am using OnEventMode. Am I missing something? The script sets: Opt('GUIOnEventMode',True) So isn't my script using

So may first question was do the button number need to be unique. I believe you described the OnEvent mode and how each button would have it's own index.

Also is there a better way for the script to match wondow position that the SwitchGUI case statement I am using? It doesn't scale very well.

Me

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

OK I looked at the documentation and it looks like I am using OnEventMode. Am I missing something? The script sets: Opt('GUIOnEventMode',True) So isn't my script using

So may first question was do the button number need to be unique. I believe you described the OnEvent mode and how each button would have it's own index.

Also is there a better way for the script to match wondow position that the SwitchGUI case statement I am using? It doesn't scale very well.

Me

Look closer. You are selecting GuiOnEventMode, but then you still try to run a message loop in your While/WEnd. You can't have both event mode and use GuiGetMsg() at the same time.

Switching GUIs is irrelavent to EventMode. All the controls of all the GUIs can generate events. The functions that get called by the events can handle multiple GUIs or controls because the @GUI_CtrlID, @GUI_CtrlHandle, and @GUI_WinHandle macros can tell it which called the function.

:D

P.S. Here's a demo. Two GUIs, switching between them with a button, and all the buttons are handled by a single function, _ButtonHit():

#include <GUIConstantsEX.au3>

Opt('GUIOnEventMode', True)

Global $hGUI1, $hGUI2

$hGUI1 = GUICreate("GUI Test 1", 300, 300, 200, 200)
_CreateControls()

$hGUI2 = GUICreate("GUI Test 2", 300, 300, 400, 400)
_CreateControls()
GUISetState()

While 1
    Sleep(20)
WEnd

Func _CreateControls()
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
    GUICtrlCreateButton("One", 100, 20, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
    GUICtrlCreateButton("Two", 100, 70, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
    GUICtrlCreateButton("Switch GUIs", 100, 140, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
EndFunc   ;==>_CreateControls

Func _ButtonHit()
    Local $sButtonText = ControlGetText(@GUI_WinHandle, "", @GUI_CtrlId)
    Local $sWinTitle = WinGetTitle(@GUI_WinHandle)
    Switch $sButtonText
        Case "One", "Two"
            MsgBox(64, "Button", "You clicked button '" & $sButtonText & "', on GUI '" & $sWinTitle & "'", 5)
        Case "Switch GUIs"
            Switch @GUI_WinHandle
                Case $hGUI1
                    GUISetState(@SW_HIDE, $hGUI1)
                    GUISetState(@SW_SHOW, $hGUI2)
                    WinActivate($hGUI2)

                Case $hGUI2
                    GUISetState(@SW_HIDE, $hGUI2)
                    GUISetState(@SW_SHOW, $hGUI1)
                    WinActivate($hGUI1)
            EndSwitch
    EndSwitch
EndFunc   ;==>_ButtonHit

Func _Quit()
    Exit
EndFunc   ;==>_Quit

Hope that helps.

:D

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

$#@! Thanks!! I have been doing all of my scripts wrong. I'll "fix" them. (edited for code tags)

Any recomendatins on the window position when I move it. I have been doing it this way:

; Main Form to Second Form
            $Pos = WinGetPos($hGUI1)
            WinMove($hGUI2, "", $Pos[0], $Pos[1])
            GUISetState(@SW_Hide, $hGUI1)
            GUISetState(@SW_SHOW, $hGUI2)
  ;Second Form Return to Main
            $Pos = WinGetPos($hGUI2)
            WinMove($hGUI1, "", $Pos[0], $Pos[1])
            GUISetState(@SW_Hide, $hGUI2)
            GUISetState(@SW_SHOW, $hGUI1)
        Case Else
            GUISetState(@SW_Hide, $hGUi2)
            GUISetState(@SW_SHOW, $hGUI1)

So I see tyour example would be modified to this:

#include <GUIConstantsEX.au3>

Opt('GUIOnEventMode', True)

Global $hGUI1, $hGUI2

$hGUI1 = GUICreate("GUI Test 1", 300, 300, 200, 200)
_CreateControls()

$hGUI2 = GUICreate("GUI Test 2", 300, 300, 400, 400)
_CreateControls()
GUISetState()

While 1
    Sleep(20)
WEnd

Func _CreateControls()
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
    GUICtrlCreateButton("One", 100, 20, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
    GUICtrlCreateButton("Two", 100, 70, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
    GUICtrlCreateButton("Switch GUIs", 100, 140, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
EndFunc   ;==>_CreateControls

Func _ButtonHit()
    Local $sButtonText = ControlGetText(@GUI_WinHandle, "", @GUI_CtrlId)
    Local $sWinTitle = WinGetTitle(@GUI_WinHandle)
    Switch $sButtonText
        Case "One", "Two"
            MsgBox(64, "Button", "You clicked button '" & $sButtonText & "', on GUI '" & $sWinTitle & "'", 5)
        Case "Switch GUIs"
            Switch @GUI_WinHandle
            Case $hGUI1
                    $Pos = WinGetPos($hGUI1)
                    WinMove($hGUI2, "", $Pos[0], $Pos[1])
                    GUISetState(@SW_HIDE, $hGUI1)
                    GUISetState(@SW_SHOW, $hGUI2)
                    WinActivate($hGUI2)

                Case $hGUI2
                    $Pos = WinGetPos($hGUI2)
                    WinMove($hGUI1, "", $Pos[0], $Pos[1])
                    GUISetState(@SW_HIDE, $hGUI2)
                    GUISetState(@SW_SHOW, $hGUI1)
                    WinActivate($hGUI1)
            EndSwitch
    EndSwitch
EndFunc   ;==>_ButtonHit

Func _Quit()
    Exit
EndFunc   ;==>_Quit

Am I correct? If so I still have the problem where it takes a lot of code to keep to remember the last windows position.

Thanks again. You have always been very helpful.

Me

Edited by IvanCodin

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

If so I still have the problem where it takes a lot of code to keep to remember the last windows position.

I guess you could knock down the line count a bit with a logic tweak:
Func _ButtonHit()
    Local $Pos
    Local $sButtonText = ControlGetText(@GUI_WinHandle, "", @GUI_CtrlId)
    Local $sWinTitle = WinGetTitle(@GUI_WinHandle)
    Local $hOtherGUI = $hGUI1
    If @GUI_WinHandle = $hGUI1 Then $hOtherGUI = $hGUI2
    Switch $sButtonText
        Case "One", "Two"
            MsgBox(64, "Button", "You clicked button '" & $sButtonText & "', on GUI '" & $sWinTitle & "'", 5)
        Case "Switch GUIs"
            $Pos = WinGetPos(@GUI_WinHandle)
            WinMove($hOtherGUI, "", $Pos[0], $Pos[1])
            GUISetState(@SW_HIDE, @GUI_WinHandle)
            GUISetState(@SW_SHOW, $hOtherGUI)
            WinActivate($hOtherGUI)
    EndSwitch
EndFunc   ;==>_ButtonHit

:D

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thank You!!! That is VERY creative!

I'll definately try this in my script.

Me

Share this post


Link to post
Share on other sites

Look closer. You are selecting GuiOnEventMode, but then you still try to run a message loop in your While/WEnd. You can't have both event mode and use GuiGetMsg() at the same time.

Switching GUIs is irrelavent to EventMode. All the controls of all the GUIs can generate events. The functions that get called by the events can handle multiple GUIs or controls because the @GUI_CtrlID, @GUI_CtrlHandle, and @GUI_WinHandle macros can tell it which called the function.

:D

P.S. Here's a demo. Two GUIs, switching between them with a button, and all the buttons are handled by a single function, _ButtonHit():

#include <GUIConstantsEX.au3>

Opt('GUIOnEventMode', True)

Global $hGUI1, $hGUI2

$hGUI1 = GUICreate("GUI Test 1", 300, 300, 200, 200)
_CreateControls()

$hGUI2 = GUICreate("GUI Test 2", 300, 300, 400, 400)
_CreateControls()
GUISetState()

While 1
    Sleep(20)
WEnd

Func _CreateControls()
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
    GUICtrlCreateButton("One", 100, 20, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
    GUICtrlCreateButton("Two", 100, 70, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
    GUICtrlCreateButton("Switch GUIs", 100, 140, 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
EndFunc   ;==>_CreateControls

Func _ButtonHit()
    Local $sButtonText = ControlGetText(@GUI_WinHandle, "", @GUI_CtrlId)
    Local $sWinTitle = WinGetTitle(@GUI_WinHandle)
    Switch $sButtonText
        Case "One", "Two"
            MsgBox(64, "Button", "You clicked button '" & $sButtonText & "', on GUI '" & $sWinTitle & "'", 5)
        Case "Switch GUIs"
            Switch @GUI_WinHandle
                Case $hGUI1
                    GUISetState(@SW_HIDE, $hGUI1)
                    GUISetState(@SW_SHOW, $hGUI2)
                    WinActivate($hGUI2)

                Case $hGUI2
                    GUISetState(@SW_HIDE, $hGUI2)
                    GUISetState(@SW_SHOW, $hGUI1)
                    WinActivate($hGUI1)
            EndSwitch
    EndSwitch
EndFunc   ;==>_ButtonHit

Func _Quit()
    Exit
EndFunc   ;==>_Quit

Hope that helps.

:D

Thanks for the example PsaltyDS! I have been wondering in the back of my head for sometime about how I could dynamically create controls, from lets say a data base and be able to "use" them without a handle.

Thanks again!


Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

There might be a way by storing a database on the internet and downloading the database, but I don't think that would be very easy (or exactly what you want). What you can do is store the coordinates and any other parameters that you may want in a .ini file and read them with Ini functions. If you are set on a database, then SQLite is the way to go (at least in autoit). SQLite is built into autoit so there is no need to have a dll or anything like that. SQLite is a lot more difficult than ini files, but if you are up to it, SQLite functions are available.

Edited by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

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