Jump to content
Sign in to follow this  
gingerbloke

SIMPLE BUTTON QUESTION

Recommended Posts

gingerbloke

I know this will seem really simple but I am stumped.Posted Image

I am just trying to get 3 GUI buttons to call different functions but it will not work. I have tried looking on the other topics (and as you will probably tell, copied some of the code) but I cannot get the below script to work.

To explain, when I connect a USB Device, this funtion is called from the main program to create the Window and buttons, I get the window and buttons, but when I click on the buttons nothing happens. I have tried testing it with message boxes and everything but to no avail.

Any advice would be appreciated.

Func FirstRun ()
local $btna,  $btnb, $btnc
local $FIRSTRUN
local $FIRSTRUNTITLE
local $FIRSTRUNTEXT, $FIRSTRUNTEXTa
local $MOTIF
local $MSG

$FIRSTRUN = GUICreate($GuiTitle, 350, 70, 1, 1)
$FIRSTRUNTITLE = GUICtrlCreateLabel("", 110, 5, 300, 12)
$FIRSTRUNTEXT = GUICtrlCreateLabel("Do You Wish to Do?", 110, 5, 300, 12)
$FIRSTRUNTEXTa = GUICtrlCreateLabel("Please Select Your Options:", 110, 20, 300, 12)
$MOTIF = GUICtrlCreatePic($sFile, 0, 0, 100, 68) 
$btna = _GUICtrlButton_Create($FIRSTRUN, "STANDARD", 110, 40, 70, 20)
$btnb = _GUICtrlButton_Create($FIRSTRUN, "CLEAN-OUT", 190, 40, 70, 20)
$btnc = _GUICtrlButton_Create($FIRSTRUN, "RESTORE", 270, 40, 70, 20) 
; GUISetState(@SW_SHOW)
GUISetState() 
While 1
  GUIGetMsg()
  Select
   Case $GUI_EVENT_CLOSE
    ExitLoop
   Case $btna
    INCREMENTALBACKUP()
   Case $btnb
    CLEANUP()
   Case $btnc
    RESTORE()  
  EndSelect
WEnd
EndFunc

Gingerbloke

Share this post


Link to post
Share on other sites
Authenticity

You're mixing a few things that can't work together.

First, If you create controls or use functions other than the built-in ones (the built-in ones start with GUICtrlCreate*) you won't be able to get notified about their events using AutoIt's GUI message loop (GUIGetMsg). Instead you need to handle their events via handlers posted by $WM_NOTIFY, $WM_COMMAND, etc... Look up the help file for examples. A better alternative is to create them using the built-in functions, GUICtrlCreateButton() for example.

Second, this:

While 1
  GUIGetMsg()
  Select
   Case $GUI_EVENT_CLOSE
    ExitLoop
   Case $btna
    INCREMENTALBACKUP()
   Case $btnb
    CLEANUP()
   Case $btnc
    RESTORE()  
  EndSelect
WEnd
EndFunc

Is not correct, the correct way to handle a message loop is as follows:

While 1
  Local $Msg = GUIGetMsg()
  Select
   Case $Msg = $GUI_EVENT_CLOSE
    ExitLoop
   Case $Msg = $btna
    INCREMENTALBACKUP()
   Case $Msg = $btnb
    CLEANUP()
   Case $Msg = $btnc
    RESTORE()  
  EndSelect
WEnd
EndFunc

..or using a switch case block:

While 1
  
  Switch GUIGetMsg()
   Case $GUI_EVENT_CLOSE
    ExitLoop
   Case $btna
    INCREMENTALBACKUP()
   Case $btnb
    CLEANUP()
   Case $btnc
    RESTORE()  
  EndSwitch
WEnd
EndFunc

Third and last :) make sure you're not mixing event modes, i.e. in one part you use on event mode and on another part of the script you use a message loop.

You may want to start by learning the basics of AutoIt using the help file examples, Welcome to AutoIt 1-2-3 or the examples from the path "C:\Program Files\AutoIt3\Examples" (if installed on "C:\Program Files").

Edited by Authenticity

Share this post


Link to post
Share on other sites
gingerbloke

Posted Image Thanks for your help Authenticity,

I never knew there was a "C:\Program Files\AutoIt3\Examples", it shows that sometimes reading the Manual Helps!Posted Image

I modified one of the examples and it works. Looking at your explanation, I had read several posts and got well mixed up but for any newbies (like me) here is th button layout that works for me

[autoIt]

#include <GUIConstantsEx.au3>

FirstRun ()

Func FirstRun ()

local $btna, $btnb, $btnc

local $FIRSTRUNTEXT

local $msg

;Create window

GUICreate("Title", 350, 70,1,1)

$FIRSTRUNTEXT = GUICtrlCreateLabel("Do You Wish to Do?", 110, 5, 300, 12)

$btna = GUICtrlCreateButton("QUICK", 110, 40, 70, 20)

$btnb = GUICtrlCreateButton("CLEAN-OUT", 190, 40, 70, 20)

$btnc = GUICtrlCreateButton("RESTORE", 270, 40, 70, 20)

;Show window/Make the window visible

GUISetState(@SW_SHOW)

While 1

$msg = GUIGetMsg()

Select

;Check if user clicked on the close button

Case $msg = $GUI_EVENT_CLOSE

GUIDelete()

;Exit the script

Exit

Case $msg = $btna

MsgBox(64, "", "QUICK button!")

Case $msg = $btnb

MsgBox(64, "", "CLEAN-OUT button!")

Case $msg = $btnc

MsgBox(64, "", "RESTORE button!")

EndSelect

WEnd

EndFunc

[\autoIt]

Gingerbloke

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.