Jump to content

button functions


Recommended Posts

I am new to autoit and have enjoyed playing with it so far have not had many problems. It seems pretty easy even for me with no programming knowledge other than html.

So far I have made a gui with some buttons and i have made a script with several different functions now the part I have not been able to figure out is how to make a function start when a button is pressed. Any help with this problem will be appreciated.

Link to comment
Share on other sites

Welcome to the forums :x

There are two ways to process clicks and other actions in AutoIt, both are detailed in the GUI reference section of the helpfile. The first is Message-Loop mode:

#include <GUIConstantsEx.au3>

GUICreate("Testing a message loop")
$hMyButton = GUICtrlCreateButton("Click me!", 2, 2, 100, 30)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hMyButton
            MyFunction()
    EndSwitch
WEnd

Func MyFunction()
    MsgBox(0, "Test", "Hello World!")
EndFunc

And the second is OnEvent mode:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

GUICreate("Testing a message loop")
GUISetOnEvent($GUI_EVENT_CLOSE, "MyExit")

$hMyButton = GUICtrlCreateButton("Click me!", 2, 2, 100, 30)
GUICtrlSetOnEvent($hMyButton, "MyFunction")

GUISetState()

While 1
    Sleep(10)
WEnd

Func MyFunction()
    MsgBox(0, "Test", "Hello World!")
EndFunc

Func MyExit()
    Exit
EndFunc

Edit: It seems that Melba23 is being a bit slow this morning, it is unusual that I can beat him to the post :P It must have been a very heavy night last night.

Edited by Mat
Link to comment
Share on other sites

  • Moderators

digitaloutlaw,

Welcome the the AutoIt forum. :x

I have not been able to figure out is how to make a function start when a button is pressed

Does this give you a clue? :P

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$hButton = GUICtrlCreateButton("Press", 10, 10, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            _Function()
    EndSwitch
WEnd

Func _Function()
    MsgBox(0, "", "Hi there!")
EndFunc

You might find the excellent tutorials here and here to be of interest if you wish to progress in your AutoIt knowledge. There are even video tutorials on YouTube if you prefer watching to reading. :shifty:

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