hannes08 Posted August 16, 2012 Posted August 16, 2012 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: expandcollapse popupDim $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]
Warsaw Posted August 16, 2012 Posted August 16, 2012 (edited) 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 August 16, 2012 by Warsaw
hannes08 Posted August 16, 2012 Author Posted August 16, 2012 (edited) Hi Warsaw, I'll look into this tomorrow. Thanks for the hint! Edit: Works like a charm Thanks a lot this saves me many lines of code Edited August 17, 2012 by hannes08 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now