topten

Dynamic buttons in Switch Case

4 posts in this topic

I create "dynamic" buttons

$button[$z]

How can I deal with them in Switch Case?

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

; Mouse coords relative to GUI client area
Opt("MouseCoordMode", 2)

; Set target coords
Global $iTgt_Left = 10, $iTgt_Right = 210, $iTgt_Top = 10, $iTgt_Bot = 110

; Create GUI
$hGUI = GUICreate("Test", 300, 200)

;$cTarget = GUICtrlCreateLabel("", $iTgt_Left, $iTgt_Top, $iTgt_Right - $iTgt_Left, $iTgt_Bot - $iTgt_Top, $SS_BLACKFRAME)
;GUICtrlSetState(-1, $GUI_DISABLE)
Global $button[5]
$y = 10
for $z = 1 to 4
    $button[$z] = GUICtrlCreateButton("button", 10, $y, 80, 23)
    $y = $y+20
next



$cLabel = GUICtrlCreateButton("Move me", 10, 170, 80, 23)
$cLabel1 = GUICtrlCreateButton("Move me", 10, 130, 80, 23)
;GUICtrlSetBkColor(-1, 0x00FF00)

$cButton = GUICtrlCreateButton("Me too", 10, 150, 80, 23)

GUISetState()


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            ; If the mouse button is pressed - get info about where
            $cInfo = GUIGetCursorInfo($hGUI)
            ; Is it over a control
            $iControl = $cInfo[4]
            Switch $iControl
                ; If it is a control we want to move
            ;   for $z = 1 to 4
                Case $cLabel, $cButton, $cLabel1, $button[$z]; HERE I GET AN ERROR
                    ; Work out offset of mouse on control
                    $aPos = ControlGetPos($hGUI, "", $iControl)
                    $iSubtractX = $cInfo[0] - $aPos[0]
                    $iSubtractY = $cInfo[1] - $aPos[1]
                    ; And then move the control until the mouse button is released
                    Do
                        $cInfo = GUIGetCursorInfo($hGUI)
                        ControlMove($hGUI, "", $iControl, 10, $cInfo[1] - $iSubtractY)
                    Until Not $cInfo[2]
                    ; See if the mouse was released over the target
                    $aMPos = MouseGetPos()
                    If $aMPos[0] > $iTgt_Left And $aMPos[0] < $iTgt_Right Then
                    

            EndSwitch
    EndSwitch
WEnd

Great thanx in advance!

Share this post


Link to post
Share on other sites



Hello. I'm not sure if you meant this.
 

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

; Mouse coords relative to GUI client area
Opt("MouseCoordMode", 2)

; Set target coords
Global $iTgt_Left = 10, $iTgt_Right = 210, $iTgt_Top = 10, $iTgt_Bot = 110

; Create GUI
$hGUI = GUICreate("Test", 300, 200)

;$cTarget = GUICtrlCreateLabel("", $iTgt_Left, $iTgt_Top, $iTgt_Right - $iTgt_Left, $iTgt_Bot - $iTgt_Top, $SS_BLACKFRAME)
;GUICtrlSetState(-1, $GUI_DISABLE)
Global $button[4]
$y = 10
For $z = 0 To 3
    $button[$z] = GUICtrlCreateButton("button" & $z + 1, 10, $y, 80, 23)
    $y = $y + 20
Next



$cLabel = GUICtrlCreateButton("Move me", 10, 170, 80, 23)
$cLabel1 = GUICtrlCreateButton("Move me", 10, 130, 80, 23)
;GUICtrlSetBkColor(-1, 0x00FF00)

$cButton = GUICtrlCreateButton("Me too", 10, 150, 80, 23)

GUISetState()

Local $iMsg = 0
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit


        Case $button[0] To $button[3]
            MsgBox(0, "", GUICtrlRead($iMsg))
    EndSwitch

WEnd

also Autoit array index begin from 0.

Saludos


 

venezuela.png Would you like to say thank to Danyfirex using a warmy way?  offer me a cup of coffee... coffee.png

autoit_scripter_blue_userbar.png

       AutoIt...

 

 

Share this post


Link to post
Share on other sites

Fantastic! Great thanx. Merry Christmas!:dance:

Share this post


Link to post
Share on other sites

You could also make it more dynamic, by using a slight amendment like the following.

Global $b, $a = 4
For $z = 0 To $a
    $b = $z
    $button[$z] = GUICtrlCreateButton("button" & $z + 1, 10, $y, 80, 23)
    $y = $y + 20
Next

 Case $button[0] To $button[$b]

That way, $a could be the flexible result of an INI file or Inputbox etc.

 

 

 

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
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