Jump to content
Sign in to follow this  
Ventura

how to make two functions work in one button gui?

Recommended Posts

Ventura

I want if I press the func1/func2 button it will show func 1 msg, then if I press again it will show func 2 msg.. and it continues.. 

as I know I can make a another button and make it work, but I want it to work with one button.. so I don't know where to put.

#include <GUIConstantsEx.au3>
 $hGUI = GUICreate("Test", 100, 60) 
 $hButton_1 = GUICtrlCreateButton("Func1/Func2", 10, 10, 80, 40) 
 GUISetState()

 While 1
     Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
             Exit
         Case $hButton_1
             _Func_1()         
     EndSwitch
 WEnd

 Func _Func_1()
MsgBox(1,"Func1","This is Func 1")
 EndFunc

 Func _Func_2()
MsgBox(1,"Func2","This is Func 2")
 EndFunc

Share this post


Link to post
Share on other sites
JohnOne

You can do it with a flag variable, here I used $Func1

#include <GUIConstantsEx.au3>

Global $func1 = True
$hGUI = GUICreate("Test", 100, 60)
$hButton_1 = GUICtrlCreateButton("Func1/Func2", 10, 10, 80, 40)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            If $func1 Then
                _Func_1()
            Else
                _Func_2()
            EndIf
            $func1 = Not $func1
    EndSwitch
WEnd

Func _Func_1()
    MsgBox(1, "Func1", "This is Func 1")
EndFunc   ;==>_Func_1

Func _Func_2()
    MsgBox(1, "Func2", "This is Func 2")
EndFunc   ;==>_Func_2
  • Like 1

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
ViciousXUSMC

Johns way is probably way better, as a novice when I tried to do this I simply used a standard IF statement based on what I needed.

As an example if I wanted a button to enable/disable the Windows firewall service the button only had 1 function but it would check to see the current status of the computer and perform the If statement thus giving me a result of 2 functions.

If RegRead = (Firewall On) Then

RegWrite (Turn Firewall Off)

MsgBox Firewall Turned Off

Else If RegRead (Firewall Off) Then RegWrite (Turn Firewall On)

MsgBox Firewall Turned On

(Not my actual script just an outline of what it was doing)

Later I found that I can also call a simple If statement to see the current state and set the button text to the current status so before I click the button I know if its on/off.

Worked for me but glad to know a new way. 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites
Luigi

I thinking some like this...

#AutoIt3Wrapper_AU3Check_Parameters= -q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#Tidy_Parameters=/sf

#include-once
#include <Array.au3>
#include <Misc.au3>
#include <GUIConstantsEx.au3>

OnAutoItExitRegister("_EXIT_BEFORE")

Opt('GUIOnEventMode', 1)
Opt('GUIEventOptions', 1)
Opt('MustDeclareVars', 1)
Opt('WinWaitDelay', 25)

Global $aGuiSize[2] = [800, 600]
Global $sGuiTitle = "GuiTitle"
Global $hGui
Global $hButton

$hGui = GUICreate($sGuiTitle, $aGuiSize[0], $aGuiSize[1])
GUISetOnEvent($GUI_EVENT_CLOSE, '_EXIT')
$hButton = GUICtrlCreateButton("Button [OFF]", 10, 10, 80, 20)
GUICtrlSetOnEvent($hButton, "_HitButton")


GUISetState(@SW_SHOW, $hGui)

While Sleep(10)
WEnd

Func _EXIT()
    Exit
EndFunc   ;==>_EXIT

Func _EXIT_BEFORE($sInput = 0)
    If IsDeclared("sInput") Then ConsoleWrite("_exit[ " & $sInput & " ]" & @LF)
    GUIDelete($hGui)
EndFunc   ;==>_EXIT_BEFORE

Func _HitButton()
    Local Static $iHit = 0
    If $iHit Then
        $iHit = 0
        GUICtrlSetData($hButton, "Button [OFF]")
        SomeFunctionOFF()
    Else
        $iHit = 1
        GUICtrlSetData($hButton, "Button [ON]")
        SomeFunctionON()
    EndIf
EndFunc   ;==>_HitButton

Func SomeFunctionOFF()
    ConsoleWrite("SomeFunctionOFF()" & @LF)
EndFunc   ;==>SomeFunctionOFF

Func SomeFunctionON()
    ConsoleWrite("SomeFunctionON()" & @LF)
EndFunc   ;==>SomeFunctionON
Edited by Detefon

Visit my repository

Share this post


Link to post
Share on other sites
Melba23

Hi,

Can I join in too? ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

$hGUI = GUICreate("Test", 500, 500)
$cButton = GUICtrlCreateButton("Func 1", 10, 10, 80, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            Switch GUICtrlRead($cButton)
                Case "Func 1"
                    _Func_1()
                    GUICtrlSetData($cButton, "Func 2")
                Case "Func 2"
                    _Func_2()
                    GUICtrlSetData($cButton, "Func 1")
            EndSwitch
    EndSwitch
WEnd

Func _Func_1()
    MsgBox($MB_SYSTEMMODAL, "Func 1", "This is Func 1")
EndFunc   ;==>_Func_1

Func _Func_2()
    MsgBox($MB_SYSTEMMODAL, "Func 2", "This is Func 2")
EndFunc   ;==>_Func_2
M23
  • Like 1

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
Luigi

@Melba, yes you can... but, my script is big! muaaaaaaaaaahahahahhah!


Visit my repository

Share this post


Link to post
Share on other sites
JohnOne

here's a beta version.

#include <GUIConstantsEx.au3>

Global $aFuncs[]
$aFuncs[0] = _Func_1
$aFuncs[1] = _Func_2

Global $WhichFunc = 0
$hGUI = GUICreate("Test", 100, 60)
$hButton_1 = GUICtrlCreateButton("Func1/Func2", 10, 10, 80, 40)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            $aFuncs[$WhichFunc] ()
            $WhichFunc = Not $WhichFunc
    EndSwitch
WEnd

Func _Func_1()
    MsgBox(1, "Func1", "This is Func 1")
EndFunc   ;==>_Func_1

Func _Func_2()
    MsgBox(1, "Func2", "This is Func 2")
EndFunc   ;==>_Func_2
  • Like 1

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
ViciousXUSMC

I like this!  :shifty:

 

Yeah that makes my brain hurt, 

Share this post


Link to post
Share on other sites
MikahS

@Melba23 GUI god over here.. :)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
JohnOne

 

here's a beta version.

#include <GUIConstantsEx.au3>

Global $aFuncs[]
$aFuncs[0] = _Func_1
$aFuncs[1] = _Func_2

Global $WhichFunc = 0
$hGUI = GUICreate("Test", 100, 60)
$hButton_1 = GUICtrlCreateButton("Func1/Func2", 10, 10, 80, 40)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            $aFuncs[$WhichFunc] ()
            $WhichFunc = Not $WhichFunc
    EndSwitch
WEnd

Func _Func_1()
    MsgBox(1, "Func1", "This is Func 1")
EndFunc   ;==>_Func_1

Func _Func_2()
    MsgBox(1, "Func2", "This is Func 2")
EndFunc   ;==>_Func_2

Actually this does not need beta version, just change 

Global $aFuncs[]

To

Global $aFuncs[2]

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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  

×