Jump to content

Referencing dynamic variable in For To Next Loop


kjpolker
 Share

Recommended Posts

I have been searching for what I am sure is any easy fix for hours now and I just can't seem to find the write keywords for what I am looking for to snag an answer. Even the title to this bugs me...

Anyways I have a For To Next loop that I am trying to call a different variable in each loop and I can't seem to find the right approach.

$label18 = GUICtrlCreateLabel("...", 147, 52, 100, 21)
$combobox18 = GUICtrlCreateCombo("--Select--", 16, 47, 100, 21)
$label19 = GUICtrlCreateLabel("...", 147, 52, 100, 21)
$combobox19 = GUICtrlCreateCombo("--Select--", 16, 47, 100, 21)
$label20 = GUICtrlCreateLabel("...", 147, 52, 100, 21)
$combobox20 = GUICtrlCreateCombo("--Select--", 16, 47, 100, 21)
$label21 = GUICtrlCreateLabel("...", 147, 52, 100, 21)
$combobox21 = GUICtrlCreateCombo("--Select--", 16, 47, 100, 21)
;Ignore the parameters

Func attach()
   Local $label , $combobox
   For $i2 = 18 To 21 Step +1
      MsgBox(0, "", GUICtrlRead($label & $i2)) ;check to see if I can do this, returns blank even though $label18 is not blank
      If GUICtrlRead($label & $i2) <> "..." And GuiCtrlRead($combobox & $i2) <> "--Select--" Then
         $read = GUICtrlRead($combobox & $i2)
      EndIf
   Next
EndFunc

;Should return text of $combobox18 to 21 as long as they are not "..." and "--select--"

I am trying to store the value of $combobox18 - $combobox21 in a $read variable (not array, just a single variable as there is more to this function. I just watered it down to get to the point).

combobox and label variables share the same numbers (example, $combox18 and $label18)

I have tried "$label" & $i2

So how do I smash those two variables together to achieve the actual variable?

Edited by kjpolker
more information
Link to comment
Share on other sites

Okay I see now how to utilize it, last problem is I can't Eval() a function?

Func attach()
   Local $label , $combobox
   For $i2 = 18 To 21 Step +1
      If GUICtrlRead(Eval('label' & $i2)) <> "..." And GuiCtrlRead(Eval('combobox' & $i2)) <> "--Select--" Then
         $read = GUICtrlRead(Eval('combobox' & $i2))
      EndIf
      HotKeySet($read, Eval('attach' & $i2))
   Next
EndFunc

Func attach18()
    MsgBox(0, "", "This worked")
EndFunc

Not able to get the HotKey assigned...

Link to comment
Share on other sites

Not exactly sure what the end goal is here but as a rule of thumb if you're  attempting to identify a variable  by name within your own script then something is off.  This is just a quick example based off of what you've posted thus far.   I'm sure that the combobox changing could be setup to some type of message and then dealt with that way.  

HotKeySet("{ESC}","exit1")

GUICreate("gui",500,500)

dim $control[3][2]
dim $key_func[3][2]

$key_func[0][0]="1"
$key_func[0][1]="one"
$key_func[1][0]="2"
$key_func[1][1]="two"
$key_func[2][0]="3"
$key_func[2][1]="three"

$control[0][0] = GUICtrlCreateLabel("...", 20, 20, 100, 21)
$control[0][1] = GUICtrlCreateCombo("--Select--", 20, 50, 100, 21)
GUICtrlSetData($control[0][1],"one")
$control[1][0] = GUICtrlCreateLabel("...", 120, 20, 100, 21)
$control[1][1] = GUICtrlCreateCombo("--Select--", 120, 50, 100, 21)
GUICtrlSetData($control[1][1],"two")
$control[2][0]= GUICtrlCreateLabel("...", 200, 20, 100, 21)
$control[2][1] = GUICtrlCreateCombo("--Select--", 200, 50, 100, 21)
GUICtrlSetData($control[2][1],"three")

GUISetState(@SW_SHOW)

while True ;probably good place for message loop...
    $y=0
    for $x = 0 to 2
        attach($control[$x][0],$control[$x][1],$key_func[$x][0],$key_func[$x][1])
        if $key_func[$x][0]="" then $y+=1
    Next
    
    if $y=3 then ExitLoop
    Sleep(50)
    WEnd
MsgBox('','','exited loop',1) ;end of program

Func attach($label,$combobox, ByRef $key, ByRef $func)
      If $key <> "" And GuiCtrlRead($combobox) <> "--Select--" Then
        HotKeySet($key,$func)
        $key=""
        $func=""
      EndIf

EndFunc

func one()
    MsgBox('','','one',1)
EndFunc

func two()
    MsgBox('','','tw0',1)
    EndFunc

func three()
    MsgBox('','','three',1)
EndFunc

func exit1()
    Exit
    EndFunc

 

Link to comment
Share on other sites

I realized that when using HotKeySet you're already defining the function as a string so I did not need Eval()

The purpose of all this was to set several hotkeys based on 2 paired variables at a time. So if everything checked out (combobox was populated and label was populated it would pair them into a hotkey. I am certain there are cleaner ways to do this but it was in my realm of knowledge.

Func attach()
   Local $label , $combobox
   For $i2 = 18 To 21 Step +1
      If GUICtrlRead(Eval('label' & $i2)) <> "..." And GuiCtrlRead(Eval('combobox' & $i2)) <> "--Select--" Then
         $read = GUICtrlRead(Eval('combobox' & $i2))
      EndIf
      HotKeySet($read, 'attach' & $i2)
   Next
EndFunc

Func attach18() ;pairs the hotkey in combobox 18 to the function in label18
    MsgBox(0, "", "This worked")
EndFunc

Thank you for the input on Eval() and Execute() I have revised some code to utilize those features!

Link to comment
Share on other sites

1 hour ago, kjpolker said:

 

The purpose of all this was to set several hotkeys based on 2 paired variables at a time. So if everything checked out (combobox was populated and label was populated it would pair them into a hotkey. I am certain there are cleaner ways to do this but it was in my realm of knowledge.

 

I definitely got that aspect of if.  If you look at what i posted the controls are looped through and sent to the function ahead of time, It completely eliminates the need to use eval or whatever.  In your above example you acknowledge the fact that $label and $combobox should be in there somewhere but those variables are never used.  Just send the variable as a parameter and problem solved.  I'm honestly not even sure why functions such as eval even exist bc in a situation like this its just giving you a crutch and a way to do things in a very unconventional way.  I realize not everyone can be the master of everything right away but this is just the beginning of a bad habit.  Arrays are what they are for this exact reason, to shuffle through a number of related variables in an organized manner. its the only real container type that exists in autoit so you should probably get acquainted.  I'll also say based on what you've posted so far your code has no way to know when, if or which hotkeys have been set.  My code at least takes care of that.  I do have one question tho.... What is the purpose of the label changing? 

Edited by markyrocks
Link to comment
Share on other sites

The purpose of the label is part of the grand scheme here. I select a pre-existing file on the computer and store the filename in the label. The path is stored in another variable. So when a hotkey is selected from the drop down, and a file exists it pairs the hotkey to launch the file. In this case I utilized all the keyboard shortcuts of {BROWSER_HOME} {LAUNCH_MAIL} {LAUNCH_APP1} {LAUNCH_APP2} {BROWSER_HOME}.

In the real world use I paired the media key to open Spotify and not Groove, with the ability to set other hotkeys to any file. It is messy in the sense that I do reset all hotkeys upon one of those two (label,combobox) changing and run through the learned code from this forum to set each box to it's file if they are populated correctly. Does that make sense?

a snippet of my actual code:

Func attach()
   HotKeySet("{BROWSER_HOME}")
   HotKeySet("{LAUNCH_MEDIA}")
   HotKeySet("{LAUNCH_MAIL}")
   HotKeySet("{LAUNCH_APP1}")
   HotKeySet("{LAUNCH_APP2}")
   For $key = 32 To 126 Step +1
       HotKeySet(Chr($key))
    Next
   Local $label , $combobox, $attach
   For $i2 = 18 To 21 Step +1
      If GUICtrlRead(Eval('label' & $i2)) <> "..." And GuiCtrlRead(Eval('combobox' & $i2)) <> "--Select Hotkey--" Then
         $read = GUICtrlRead(Eval('combobox' & $i2))
         If $read = "Launch Media" Then
            $read = "{LAUNCH_MEDIA}"
         ElseIf $read = "Home" Then
            $read = "{BROWSER_HOME}"
         ElseIf $read = "Mail" Then
            $read = "{LAUNCH_MAIL}"
         ElseIf $read = "Explorer" Then
            $read = "{LAUNCH_APP1}"
         ElseIf $read = "Calculator" Then
            $read = "{LAUNCH_APP2}"
         EndIf
         HotKeySet($read, "attach" & $i2)
      EndIf
   Next
EndFunc

Func attach18()
   ShellExecute($filepath1)
EndFunc

 

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