Jump to content

Cases with arrays


Recommended Posts

So i have made a button array with 10 buttons. $button[1] ... $button[10]

How can i simplify the guigetmsg cases...

$y  = 0

For $b = 1 to 10
    $button[$b] = GUICtrlCreateButton("Browse", 16, 16 + $y, 51, 21, 0)
    $y += 24
Next

While 1
$nMsg = GUIGetMsg()
    Switch $nMsg
        Case $button[1]
            ;...
Endswitch
Wend

...without going through all the buttons?

each button correlates to the same task with same array number..

like for button[3] changes input[3]...

Edited by billthecreator

[font=Microsoft Sans Serif]My Scripts: From Most recent to least.[/font]Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap

Link to comment
Share on other sites

That's alright. This is how everyone does it, and it isn't very slow at all.

Well, not everybody...
#include <GuiConstantsEx.au3>

Opt("GuiOnEventMode", 1)

Global $avButtons[10]; Change size of array to change number of buttons

$hGUI = GUICreate("Test", 300, 10 + (40 * UBound($avButtons)))
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
For $b = 0 to UBound($avButtons) - 1
    $avButtons[$b] = GUICtrlCreateButton("Button " & $b, 100, 10 + ($b * 40), 100, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
Next
GUISetState()

While 1
    Sleep(10)
Wend

Func _ButtonHit()
    Local $ctrlButton = @GUI_CtrlId
    Local $sText = ControlGetText($hGUI, "", $ctrlButton)
    MsgBox(64, "Button Hit", "You clicked " & $sText & "; CtrlID = " & $ctrlButton, 5)
EndFunc

Func _Quit()
    Exit
EndFunc

:unsure:

@billthecreator: Note that when you use event mode like this, keeping the CtrlId in an array at all is not really required (for basic functionality, anyway). All the button CtrlId's are in $avButtons, but the array is never used, except to determine the button count and therefore the GUI height.

:P

Edit: Clarity

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

so i have that through button 1 to 10, when you click each button, it opens the fileopendialog and sets inputs, which are arrays as well, and also sets labels (arrays). how would i make a function (like you did) to change the certain input and label that fits with the right button.

$y  = 0
$y1 = 0
$y2 = 0

For $b = 1 to 10
    $Button[$b] = GUICtrlCreateButton("Browse", 16, 16 + $y, 51, 21, 0)
    $y += 24
Next

For $i = 1 to 10
    $Input[$i]  = GUICtrlCreateInput("", 72, 16 + $y1, 233, 21)
    GUICtrlSetState(-1, $GUI_DISABLE)
    $y1 += 24
Next

For $l = 1 to 10
    $Label[$l]  = GUICtrlCreateLabel("File Size:", 312, 18 + $y2, 100, 17)
    $y2 += 24
Next
Edited by billthecreator

[font=Microsoft Sans Serif]My Scripts: From Most recent to least.[/font]Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap

Link to comment
Share on other sites

so i have that through button 1 to 10, when you click each button, it opens the fileopendialog and sets inputs, which are arrays as well, and also sets labels (arrays). how would i make a function (like you did) to change the certain input and label that fits with the right button.

$y  = 0
$y1 = 0
$y2 = 0

For $b = 1 to 10
    $Button[$b] = GUICtrlCreateButton("Browse", 16, 16 + $y, 51, 21, 0)
    $y += 24
Next

For $i = 1 to 10
    $Input[$i]  = GUICtrlCreateInput("", 72, 16 + $y1, 233, 21)
    GUICtrlSetState(-1, $GUI_DISABLE)
    $y1 += 24
Next

For $l = 1 to 10
    $Label[$l]  = GUICtrlCreateLabel("File Size:", 312, 18 + $y2, 100, 17)
    $y2 += 24
Next
If you are addressing what I posted, you would first have to use Event Mode, and do GuiCtrlSetOnEvent() for the buttons. Then, in the button event function, do an _ArraySearch() for @GUI_CtrlID within the $Button array. The index returned will be the index of the matching controls in $Input and $Label as well. You could also reduce that by creating all three matching controls within a single For/Next loop.

If you are not using Event Mode, then you could still _ArraySearch() with the value from GuiGetMsg(), but in most cases it won't match (GuiGetMsg() returns 0 while idle) and the search of the array will be done every cycle of the message loop.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

This is an "OnEvent disabled" example for your consideration.

Opt("GUIOnEventMode", 0);0=disabled, 1=OnEvent mode enabled
 
 Local $Button[10], $Input[10], $Label[10], $Lastbutton = 0
 
 $hGUI = GUICreate("Test", 420, 10 + (27 * UBound($Button)))
 
 For $b = 0 To 9
    $Button[$b] = GUICtrlCreateButton("Browse" & $b, 16, 16 + $b * 24, 51, 21, 0)
 ;$y += 24
 Next
 
 For $i = 0 To 9
    $Input[$i] = GUICtrlCreateInput("", 72, 16 + $i * 24, 233, 21)
    GUICtrlSetState(-1, 128); $GUI_DISABLE = 128 from GuiConstantsEx.au3
 ;$y1 += 24
 Next
 
 For $l = 0 To 9
    $Label[$l] = GUICtrlCreateLabel("File Size:", 312, 18 + $l * 24, 100, 17)
 ;$y2 += 24
 Next
 
 GUISetState()
 
 While 1
    $nMsg = GUIGetMsg()
    Switch ($nMsg - $Button[0])
        Case 0 To 9
            GUICtrlSetData($Input[0] + $Lastbutton, "")
            GUICtrlSetData($Label[0] + $Lastbutton, "File Size:")
            GUICtrlSetState($Input[0] + $Lastbutton, 128)
 
            GUICtrlSetData($Input[0] + $nMsg - $Button[0], " Button " & $nMsg - $Button[0] & " was pressed")
            GUICtrlSetData($Label[0] + $nMsg - $Button[0], "What the?")
            GUICtrlSetState($Input[0] + $nMsg - $Button[0], 64); $GUI_ENABLE = 64 from GuiConstantsEx.au3
            $Lastbutton = $nMsg - $Button[0]
    EndSwitch
    If $nMsg = -3 Then ExitLoop; $GUI_EVENT_CLOSE from GuiConstantsEx.au3
 WEnd
Link to comment
Share on other sites

Malkey, that will only work if you can be sure that the handles to the buttons increment by one each time a new one is created. This is not the case, there are no guarantees to control handles.

Manadar, I would guarantee that the control handles would not increment by one each time a new control was created. If control handles were used, the script would not work.
Link to comment
Share on other sites

You are assuming it. This part right here assumes that the handles are incremented by one each time.

Switch ($nMsg - $Button[0])
        Case 0 To 9

This could would break when:

$Button[0] = 827 ; ( random )

$Button[1] = 923

$Button[2] = 857 ;etc ...

Since $nMsg can now range be anything from 827 and 923 and 857. The statement ($nMsg - $Button[0]) can return values from -100 to 100, not falling inside the: Case 0 To 9 but being valid buttons.

Link to comment
Share on other sites

You are assuming it. This part right here assumes that the handles are incremented by one each time.

See example showing Control Id's . 1,500 controls created in 30 loops all incremented by one each time in each loop. And each time the script is run, same result.

What I am not certain about is if there are any circumstances in the running of a script that can change an individual control id value of a particular control. If there are circumstances, are they easily avoided to prevent selection problems?

$hGUI = GUICreate("Test", 420, 400)
 
 Local $sRes = ""
 For $num = 1 To 10
    Local $Button[50], $Input[50], $Label[50]
 
    $sRes &= "Loop " & $num & @CRLF & "Buttons "
    For $b = 0 To 49
        $Button[$b] = GUICtrlCreateButton("Browse" & $b, 16, 16 + $b * 24, 51, 21, 0)
        $sRes &= $Button[$b] - $Button[0] & "  "
    Next
    $sRes &= @CRLF & "Inputs "
 
    For $i = 0 To 49
        $Input[$i] = GUICtrlCreateInput("", 72, 16 + $i * 24, 233, 21)
        GUICtrlSetState(-1, 128); $GUI_DISABLE = 128 from GuiConstantsEx.au3
        $sRes &= $Input[$i] - $Input[0] & "  "
    Next
    $sRes &= @CRLF & "Labels "
 
    For $l = 0 To 49
        $Label[$l] = GUICtrlCreateLabel("File Size:", 312, 18 + $l * 24, 100, 17)
        $sRes &= $Label[$l] - $Label[0] & "  "
    Next
    $sRes &= @CRLF
 Next
 MsgBox(0, "", $sRes)
Link to comment
Share on other sites

Windows does not guarantee the numerical value of handles, and therefore you should not assume them even if it works in your script.

This is the exact some problem with using booleans in expressions in AutoIt. The conversion is not guaranteed, so even if it works you should not do it anyway.

Edited by Manadar
Link to comment
Share on other sites

Windows does not guarantee the numerical value of handles, and therefore you should not assume them even if it works in your script.

This is the exact some problem with using booleans in expressions in AutoIt. The conversion is not guaranteed, so even if it works you should not do it anyway.

Manadar, I assume you know the difference between a control id and a control handle.

I assume you incorrectly mentally classified the control id's in the script as control handles, and became fixated on handles, becoming oblivious to all else. Your knowledge about handles that you shared, I believe to be true and correct, but irrelevant.

If my second assumption is incorrect and you were meaning control id every time you mentioned handle, I searched for confirmation that control id's do or do not increment by one each time a new one is created.

I found nothing to contradict my second assumption. I could not find any randomness in the allocation of control id's. This is confirmed by the results of the example in post#13.

Some sites :-

http://www.autoitscript.com/forum/index.ph...st&p=639151

YellowLab - "All controls are created in increasing order"

-See Jon reference below for correct starting control id number.

http://www.autoitscript.com/forum/index.ph...st&p=157373

CyberSlug

- Explains using the GUI id as a base value to calculate control id belonging to that GUI, with example.

http://www.autoitscript.com/forum/index.ph...ost&p=32770

Jon - "nCtrlIdx = the index of a control WITHIN a window. Same as before numbered from AUT_GUI_FIRSTCONTROL (3) to MAX_CONTROLS. Like nWinIdx this is an internal number used for quickly accessing the array "

- AUT_GUI_FIRSTCONTROL (3) meaning 3 is the first control id number used on each AutoIt GUI.; and,

- MAX_CONTROLS is now 65532 - Maximum number of controls in GUI box.

I did find an answer to my concerns of post#13.

If a control is deleted and then another control is created during the running of the script, the new control's id value will be allocated the deleted control's identification number.

If anyone can confirm it is ok or bad to use the sequential numbering property of control id's, I would appreciate it.

Link to comment
Share on other sites

  • Moderators

Malkey,

My understanding of AutoIt ControlIDs and their allocation is exactly the same as yours.

I have used the "consecutive ControlID" and Switch code trick in many scripts and never had a problem. Furthermore, I entirely share your belief that a newly created control takes the first available ControlID (starting to search from 1 upwards) - which, if you have just deleted a control in the previous line, is that relinquished by the deleted control.

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

this is how i do it.

include <GUIConstants.au3>
#include<array.au3>

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Form1", 172, 456)

Dim $Button[15]
$varStartHeight = 16
For $x = 1 To UBound($Button) - 1
    $Button[$x] = GUICtrlCreateButton("Button " & $x, 40, $varStartHeight, 75, 25)
    GUICtrlSetOnEvent(-1, "label_test")
    $varStartHeight += 30
Next

GUISetState(@SW_SHOW)

While 1
    Sleep(50)
WEnd


Func label_test()

    Local $iD = @GUI_CtrlId

    $sea = _ArraySearch($Button, $iD)

    If Not @error Then
        MsgBox(0, "", "This is button no " & $sea, 0)
    EndIf

EndFunc   ;==>label_test

this way if you add buttons, labels whatver, your only seraching through the array you want.

Link to comment
Share on other sites

Proper Test

#include <GUIConstants.au3>
#include<array.au3>

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Form1", 172, 456)

Dim $Button[15]
$varStartHeight = 16
For $x = 1 To UBound($Button) - 1
    $Button[$x] = GUICtrlCreateButton("Button" & $x, 5, $varStartHeight, 55, 25)
    GUICtrlSetOnEvent(-1, "Button_test")
    $varStartHeight += 30
Next
Dim $label[15]
$varStartHeight = 16
For $x = 1 To UBound($Button) - 1
    $label[$x] = GUICtrlCreatelabel("Label" & $x, 70, $varStartHeight, 55, 25)
    GUICtrlSetOnEvent(-1, "label_test")
    $varStartHeight += 30
Next


GUISetState(@SW_SHOW)

While 1
    Sleep(50)
WEnd
Func button_test()

    Local $iD = @GUI_CtrlId

    $sea = _ArraySearch($Button, $iD)

    If Not @error Then
        MsgBox(0, "", "This is button no " & $sea&" But ID "&$iD, 0)
    EndIf

EndFunc   ;==>label_test

Func label_test()

    Local $iD = @GUI_CtrlId

    $sea = _ArraySearch($label, $iD)

    If Not @error Then
        MsgBox(0, "", "This is label no " & $sea&" But ID "&$iD, 0)
    EndIf

EndFunc   ;==>label_test
Link to comment
Share on other sites

You were right to use I mean control ID every time I said handle. Terminology is confusing sometime.

I stand by my point. There are no guarantees whatsoever of the order of control ids and may be subject to change in Windows with every update or next release, which will make your script entirely unusable.

As said before, this is the exact some problem with using booleans in expressions in AutoIt. The conversion is not guaranteed, so even if it works you should not do it anyway.

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