Jump to content

Cleaning up code..


dustinisgod
 Share

Go to solution Solved by Jos,

Recommended Posts

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

  • Developers
  • Solution

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


 

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

  • Developers

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

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

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