dustinisgod Posted January 11, 2022 Share Posted January 11, 2022 So I am learning.. slowly.. I have a bit of code that's working great for me, but I am trying to learn how to clean it up a bit. Currently I have 4 button, and they all work, but I would like to figure out if there is a way to combine the "case" portion of the code. Each case is identical, so it seems redundant to have one for every button, instead of just having one that says, you pressed this button so I'm going to run this case with this button name. Hopefully this made sense, appreciate the help. #Region ### START Koda GUI section ### $Form1 = GUICreate("Form1", 30, 321, 740, 1071, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW,$WS_EX_TOPMOST)) $Button1 = GUICtrlCreateButton("FH1", 0, 28, 31, 27) $Button2 = GUICtrlCreateButton("FH2", 0, 96, 31, 27) $Button3 = GUICtrlCreateButton("FH3", 0, 140, 31, 27) $Button4 = GUICtrlCreateButton("FH4", 0, 184, 31, 27) GUISetState(@SW_SHOWNOACTIVATE) #EndRegion ### END Koda GUI section ### While 1 $msg = GUIGetMsg() Select Case $msg = $Button1 WinActivate("CMD") $message = "SEND FUNCTION: " & GUICtrlRead($Button1) TCPSend($connectedSocket,$message) Case $msg = $Button2 WinActivate("CMD") $message = "SEND FUNCTION: " & GUICtrlRead($Button2) TCPSend($connectedSocket,$message) Case $msg = $Button3 WinActivate("CMD") $message = "SEND FUNCTION: " & GUICtrlRead($Button3) TCPSend($connectedSocket,$message) Case $msg = $Button4 WinActivate("CMD") $message = "SEND FUNCTION: " & GUICtrlRead($Button4) TCPSend($connectedSocket,$message) EndSelect WEnd Link to comment Share on other sites More sharing options...
Developers Solution Jos Posted January 11, 2022 Developers Solution Share Posted January 11, 2022 Something like this? While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE MsgBox($MB_SYSTEMMODAL, "", "Dialog was closed") ExitLoop Case $Button1, $Button2, $Button3, $Button4 WinActivate("CMD") $message = "SEND FUNCTION: " & GUICtrlRead($msg) TCPSend($connectedSocket, $message) EndSwitch WEnd dustinisgod 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
dustinisgod Posted January 11, 2022 Author Share Posted January 11, 2022 That looks great. So doing the way you have shown, is there a limit or a best practice to how many buttons one could add? Link to comment Share on other sites More sharing options...
Developers Jos Posted January 11, 2022 Developers Share Posted January 11, 2022 That depends how many but it probably will work. There are also other option... this for example is a gui with 20 buttons ... just have a look and run it from SciTE and you will see in the OutputPane the message of the pressed Button: #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> Global $hButtons[20] Global $NbrButtons $Form1 = GUICreate("Form1", 600, 600, 10, 10) $guiX = 0 For $x = 0 To 19 $guiY = Int($x / 4) * 60 + 28 $guiX = Mod($x, 4) * 35 + 32 $hButtons[$x] = GUICtrlCreateButton("FH" & $x + 1, $guiX, $guiY, 31, 27) Next GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() For $x = 0 To 19 If $msg = $hButtons[$x] Then ConsoleWrite(" Button " & ($x + 1) & " pressed." & @CRLF) ExitLoop EndIf Next Switch $msg Case $GUI_EVENT_CLOSE MsgBox($MB_SYSTEMMODAL, "", "Dialog was closed") ExitLoop EndSwitch WEnd SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Nine Posted January 11, 2022 Share Posted January 11, 2022 If all buttons are created in sequence (like Jos example) and you are not creating/deleting buttons on the fly, you can even more simplified your code using this : While True $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE MsgBox($MB_SYSTEMMODAL, "", "Dialog was closed") ExitLoop Case $hButtons[0] to $hButtons[19] ConsoleWrite(" Button " & ($msg - $hButtons[0] + 1) & " pressed." & @CRLF) EndSwitch WEnd But be careful to follow the guidelines I gave you, otherwise, you must use Jos approach... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
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