Jump to content

Reuse GUI Button Names


Recommended Posts

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
Link to comment
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
Link to comment
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

Link to comment
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

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

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

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

  • 4 weeks later...

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!

Link to comment
Share on other sites

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