Jump to content

GUIOnEvent and Variable number of Controls


hannes08
 Share

Recommended Posts

Hi Folks,

maybe this is a stupid question, but anyway here it goes.

I have a Script in GUI mode running on event mode.

I also have a variable number of buttons the user may press.

This is an example of how I'm doing it right now:

Dim $aButtons[4]

Opt("GUIOnEventMode", 1)
GUICreate("MyGUI", 800, 600, -1, -1, BitOr($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME, $WS_MAXIMIZEBOX))
GuiSetOnEvent($GUI_EVENT_CLOSE, "ClickClose")
_CreateButtons(4)
GUISetState(@SW_SHOW)

While 1
    Sleep(1000)
WEnd

Func _CreateButtons($iAmount)
    Local $x, $y
    For $i = 1 To $iAmount
        $aButtons[$i] = GUICtrlCreateButton("Button " & $i,  10 + $i * 50, 30 + $y, 40, 40)
        GuiCtrlSetOnEvent(-1, "ClickButton" & $i)
        If $i >= $iAmount / 2 Then $y = 70
    Next
EndFunc

Func ClickClose()
    Exit
EndFunc

Func ClickButton1()
    _ClickedButton(1)
EndFunc

Func ClickButton2()
    _ClickedButton(2)
EndFunc

Func ClickButton3()
    _ClickedButton(3)
EndFunc

Func ClickButton4()
    _ClickedButton(4)
EndFunc

Func _clickedButton($id)
    ConsoleWrite("Well, now you've clicked on " & $id & @CRLF)
EndFunc

The "problem" is that I need to create a function for each button, which I believe is suboptimal. :)

The amount of available buttons is configurable over an ini file, so it's not hard-coded as in the example.

Any hint is appreciated.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Try changing it like this:

GuiCtrlSetOnEvent(-1, "ClickButton")

Func ClickButton()
For $i = 1 To $iAmount
  If @GUI_CtrlId = $aButtons[$i] Then _clickedButton($i)
Next
EndFunc

I use this method for something I made and it works well.

Edited by Warsaw
Link to comment
Share on other sites

Hi Warsaw,

I'll look into this tomorrow. Thanks for the hint! :)

Edit: Works like a charm :D

Thanks a lot this saves me many lines of code :D

Edited by hannes08
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
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

×
×
  • Create New...