Jump to content
Sign in to follow this  
faldo

Buttons and arrys

Recommended Posts

Heya,

I've made a GUI with a variable amount of buttons depending on the situation. I'll define that amount with a variable in my example for easy explanation.

Since the amount of buttons varries, i have to use "Case $msg >= and $msg <=" in the GUIGetMsg() part of the script, according to the AutoIt help file.

Here is the script:

$Number_of_buttons = 20
$Button[1]
For $i = 1 to $Number_of_buttons
    $Button[$i] = GUICtrlCreateButton("Button "&$i, 30, 20*$i, 120, 40)
Next

While 1
    $msg = GUIGetMsg()
        Select
            Case $msg >= $Button[1] And $msg <= $Button[$Number_of_buttons]
                msgbox (0, "", "A button was pushed") 
        EndSelect
WEnd

Now, the problem i've been getting is that sometimes i get a missmatch of controlIDs. Since i use >= <=, any controlID in between "$Button[1]" and "$Button[$Number_of_buttons]" will be triggered, even if the controlID doesn't belong to a button anymore due to some other GUI control taking that controlID.

Is there another way to write the above code in order to avoid getting a missmatch?

Edited by faldo

Share this post


Link to post
Share on other sites

Do you get it doing something like:

Global $gi_max_buttons = 20
Global $ga_buttons[1]
ReDim $ga_buttons[$gi_max_buttons]

For $i = 0 To $gi_max_buttons - 1
    $ga_buttons[$i] = GUICtrlCreateButton("Button " & $i, 30, 20 * $i, 120, 40)
Next

While 1
    Switch GUIGetMsg()
        Case $ga_buttons[0] To $ga_buttons[$gi_max_buttons - 1]
    EndSwitch
WEnd
?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Eh... this worked too using your select statement:

Global $h_gui = GUICreate("", 170, 500)
Global $gi_msg = 0
Global $gi_max_buttons = 20
Global $ga_buttons[1]
ReDim $ga_buttons[$gi_max_buttons]

For $i = 0 To $gi_max_buttons - 1
    $ga_buttons[$i] = GUICtrlCreateButton("Button " & $i, 30, 25 * $i, 120, 25)
Next
GUISetState()

While 1
    $gi_msg = GUIGetMsg()
    Select
        Case $gi_msg = -3
            Exit
        Case (($gi_msg >= $ga_buttons[0]) And ($gi_msg <= $ga_buttons[$gi_max_buttons -1]))
            ConsoleWrite("Button pushed...: " & $gi_msg & @CRLF)
    EndSelect
WEnd

So it's evident the code you're using ( which is obviously not this one you posted or else it'd error before you even got through the For/Next loop ), is having issues else where.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Thanx for your replies Smoke_n

The issue was not that the code didn't work, but that elsewhere in the code, i'm unintentionally replacing the ControlIDs of the buttons in this example.

I had hoped being able to resolve the issue with being more specific than using "$msg >= $Button[1] And $msg <= $Button[$Number_of_buttons]" complared to "$msg = $Button[1] OR $msg = $Button[2] OR $msg = $Button[3]..."

Lets say $Button[1] = 13 and $Button[2] = 14 (as ControlIDs)

If i delete $Button[2] and somehwere else in the code i create a checkbox, that checkbox can get a controlID of 14. When GUIGetMsg() then checks $msg and it returns 14, my Select-loop will think i've pushed button $Button[2] but in fact i've checked a checkbox. I hope i'm making sence at all :unsure:

Share this post


Link to post
Share on other sites

You could loop through the array (For-loop usually) and compare $gi_msg to the saved value. Why do you save the cId's if not for (something) like that?

Something like:

EndSelect

For $iX to UBound()

If $gi_msg = $array[$iX] then "button pushed"

Next

WEnd

I don't have AutoIt here so I can't create a full example, but give it a try yourself, and I will check back later tonight.

Also look at using OnEvent mode.

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...