Jump to content

GUI Reference - OnEvent Mode


Recommended Posts

Hi

The below script is cut and paste from help. The help indicates that "@GUI_CTRLID would equal $okbutton", however if you put @GUI_CTRLID in messagebox it returns 4. It is a unique number to the control but less intuitive then a controlId would be. How would I get $okbutton in onevent mode? Could an alternative be to use @GUI_CTRLHANDLE somehow?

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode 
$mainwindow = GUICreate("Hello World", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
GUISetState(@SW_SHOW)

While 1
  Sleep(1000) ; Idle around
WEnd

Func OKButton()
 ;Note: at this point @GUI_CTRLID would equal $okbutton,
 ;and @GUI_WINHANDLE would equal $mainwindow
  MsgBox(0, "GUI Event", "You pressed OK!")
EndFunc

Func CLOSEClicked()
 ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
 ;and @GUI_WINHANDLE would equal $mainwindow 
  MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
  Exit
EndFunc
Link to comment
Share on other sites

I don't get the problem.

$okbutton and @GUI_CTRLID returns the same value so it shouldn't matter.

You're right there are work arounds.

1) I can keep track of the guictrl creation order and relate it to the @GUI_CTRLID #

2) I can record the guictrl handle upon creation using Guictrlgethandle and compare it to the @GUI_CTRLHANDLE

Sure there alternate ways.

I'm just wondering why @GUI_CTRLID returns a number instead of $okbutton....which is what the helpfile says should be returned...or am I reading the helpfile wrong?

Link to comment
Share on other sites

  • Moderators

picea892,

@GUI_CTRLID does not return the literal string "$okbutton", it returns the value of the variable $okbutton. Try running this:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1); Change to OnEvent mode
$mainwindow = GUICreate("Hello World", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
GUISetState(@SW_SHOW)

While 1
  Sleep(1000); Idle around
WEnd

Func OKButton()
;Note: at this point @GUI_CTRLID would equal $okbutton,
;and @GUI_WINHANDLE would equal $mainwindow
  MsgBox(0, "GUI Event", "You pressed OK!" & @CRLF & "$okbutton = " & $okbutton & @CRLF & "@GUI_CTRLID = " & @GUI_CTRLID );<<<< Should give you the answer!
EndFunc

Func CLOSEClicked()
;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
;and @GUI_WINHANDLE would equal $mainwindow 
  MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
  Exit
EndFunc

So there is no need to use either of your work arounds - as long as you use a variable to store the ControlID when the control is created, you always have the correct value to hand.

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

Wow....How many times I have used $msg = GUIGetMsg() and never understood how it worked. This is the same thing.....only in on-event mode. I'm feeling a little sheepish right now.

I'm really sorry for this basic question and I really appreciate the time you two took to set me straight. On-event mode is quite slick, I think I'm sold on using it.

Link to comment
Share on other sites

  • Moderators

picea892,

Delighted to have been of help!

There are good reasons to use both GUI modes - a lot depends on what you are trying to do. However, MessageLoop does allow parameters to be passed to the functions - you have to use martin's UDF to do the same with OnEvent.

So pick the one which works best for the script you are writing - and always remember that with Opt("GUIOnEventMode") you can use both modes in the same script, just not at the same time! ;-)

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