Jump to content

How do i control a button created with an array?


Recommended Posts

Hello

I hope you can understand what i mean.

I generate buttons, depending on data.

$button is an array in this case.

So it goes something like this:

Do
  $count = $count + 1
  $button[$count] = GUICtrlCreateButton("Name" & $counter, 400, etc....
Until $count = 5

Is there any way to control the buttons and check the number in []?

Or do i have to write all the possible cases by hand like this?

case $button[1]
case $button[2]

Is there any kind of wildcard or something i could use to make one case for all $button[ ] and pass the arraynumber on like this?

case $button[arraynumber]
          Msgbox(0, "test", "Button: " & arraynumber & " pressed")

I hope you understand what i mean.

Edited by bleh
Link to comment
Share on other sites

  • Moderators

bleh,

Use a For...To...Step...Next loop - look in the Help file for details. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

bleh,

Because that is the answer to your question: :)

Is there any kind of wildcard or something i could use to make one case for all $button[ ] and pass the arraynumber on like this?

You do it like this:

$iMsg = GUIGetMsg()
Switch $iMsg
    
    ; First deal with all the other Cases
    
    ; Now the buttons in the array
    Case Else
        For $i = 0 To UBound($button) - 1
            If $iMsg = $button[$i] Then
                ; We have found the button which was actioned
                ; Here we put the code to run for the  button
                ; And because there is no point looking any further
                ExitLoop
            EndIf
        Next
EndSwitch

All clear now? :)
 

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Okay. I did that but now the buttons get constantly pressed to the point that i have to use "stop executing".

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $button[5]
$counter = 0
$coordinates = 48

$Form1 = GUICreate("Testwindow", 615, 438, 192, 124)
GUISetState(@SW_SHOW)

Do
    $button[$counter] = GUICtrlCreateButton("Button" & $counter, $coordinates, 320, 75, 25)
    $counter = $counter + 1
    $coordinates = $coordinates + 80
Until $counter = 4

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 0 To UBound($button) ;- 1
                If $nMsg = $button[$i] Then
                    MsgBox(0, "test", $i)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

I don't really understand that, to be honest.

Link to comment
Share on other sites

  • Moderators

bleh,

That is happening because your array is not full - AutoIt interprets the empty elements as 0 and so fires when it gets the default 0 return from GUIGetMsg/ ;)

The trick is to check if there is actually a ControlID in the array before comparing it:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $button[5]
$counter = 0
$coordinates = 48

$Form1 = GUICreate("Testwindow", 615, 438, 192, 124)
GUISetState(@SW_SHOW)

Do
    $button[$counter] = GUICtrlCreateButton("Button" & $counter, $coordinates, 320, 75, 25)
    $counter = $counter + 1
    $coordinates = $coordinates + 80
Until $counter = 4

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 0 To UBound($button) - 1
                If $button[$i] Then ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Is there something in the array
                    If $nMsg = $button[$i] Then
                        MsgBox(0, "test", $i)
                        ExitLoop
                    EndIf
                EndIf
            Next
    EndSwitch
WEnd

All clear now? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here's a way to do it so that, if you add other controls, you'll still be looking at the correct controls being fired.

Plus it gets rid of the Do loop with all the strange math you're doing in it.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $button[5]
$counter = 0
$coordinates = 48

$Form1 = GUICreate("Testwindow", 615, 438, 192, 124)
GUISetState(@SW_SHOW)

For $counter = 0 To 4
    $button[$counter] = GUICtrlCreateButton("Button" & $counter, $coordinates + (80  * $counter), 320, 75, 25)
Next
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button[0] To $button[4]
            For $I = 0 To 4
                If $nMsg = $button[$I] Then
                    MsgBox(0, "test", $I)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thanks to you both. I really appreciate it.

I have some kind of combination of both your examples that seems to work.

Here it is, if you are interested:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $button[5]
$counter = 0
$coordinates = 48

$Form1 = GUICreate("Testwindow", 615, 438, 192, 124)
GUISetState(@SW_SHOW)

Do
    $button[$counter] = GUICtrlCreateButton("Button" & $counter, $coordinates, 320, 75, 25)
    $counter = $counter + 1
    $coordinates = $coordinates + 80
Until $counter = 4

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button[0] To $button[3]
            For $i = 0 To UBound($button)
                If $nMsg = $button[$i] Then
                    MsgBox(0, "test", $i)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd 

 

Plus it gets rid of the Do loop with all the strange math you're doing in it.

What's wrong with Do loops and why is my math strange?

Edited by bleh
Link to comment
Share on other sites

 

Thanks to you both. I really appreciate it.

I have some kind of combination of both your examples that seems to work.

Here it is, if you are interested:

What's wrong with Do loops and why is my math strange?

Nothing wrong with Do loops, but your's is totally unnecessary when a For...Next loop is much easier to work with. You already know how many iterations your loop is going to be, so why make it so you have to add 1 to the counter every time, when the For loop does it for you. Normally you'd use the Do loop if you're not sure how long you need to loop that part of the script.

If your loop had a lot of things going on and the code was longer, in 6 months when you revisit the code, you might not see where the variable $coordinate is being updated and you'll be sitting there wondering why the controls don't line up the way you wanted them to. The way I did it, the $coordinate variable never changes, and the $counter variable is used to update the actual control positions. Much easier to troubleshoot several months down the road.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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