Jump to content

I need to get the variable name from any one of 100 buttons - variable name not control ID or handle


Go to solution Solved by pixelsearch,

Recommended Posts

I created 100 buttons using for / next loops and assigned each one a variable name that's an array

So button 1 is $buts[1]... this all works fine

for event handling I created a simple select / case
 

While 1
    $msg = GUIGetMsg(1)
        If $msg[0] = $GUI_EVENT_CLOSE Then ExitLoop
        Select
            Case $msg[0] > 0
                msgbox(0, "result", $msg[2])
        EndSelect

WEnd

This too works, but I get the control ID or the handle - eventually it will do something with the button pressed but I can't do like change its color or record what button was pushed with the handle or control id - I need $buts[X]

Is there a way to extract that info or a better way to approach this? (100 IF statements would do it but that's not better)

thanks in advance

Link to comment
Share on other sites

  • Solution

Did you create the 100 buttons all in a row, without creating any other control in the interval ?

If yes, then the 100 buttons id's should be consecutive (except if you deleted some other controls before the buttons were initially created). Just keep the id of the 1st button $buts[0] during the creation process (for example $buts[0] got an id = 10) or better, see the code below.

Later, when you retrieve any id button from the loop (for example button id 45 was pressed), then you know its variable name is $buts[45 - 10], ie. $buts[35]

This could work :

Local $idMsg
While 1
    $idMsg = GUIGetMsg()
    Switch $idMsg
        ...
        Case $idButs[0] To $idButs[99]
            ; the button pressed got an id = $idMsg, variable name is $idButs[$idMsg - $idButs[0]]

After that be careful : if you delete any other control from the GUI (including the 100 buttons) then recreating the 100 buttons may not give you a continuous sequence of button id's

Edited by pixelsearch
typo
Link to comment
Share on other sites

I went this way and it worked but it felt klooji.☝️

I wracked my brain over it and eventually thought to scour the documentation for UDFs for both arrays and buttons

and there it was:

_GUICtrlButton_GetText

I didn't need the original name except to extract the number... I scrapped the array of names and switched the event mode so I didn't need to do more that 'write' the number on the button at creation, then retrieve it when clicked. 😀

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