Jump to content

Can I Dynamically Create Variable Names?


 Share

Recommended Posts

I am trying to read from an INI file and then create a two-tabbed GUI dynamically based upon what I read from the INI.

I first count the number of section header in the INI and then run through two "For" loops to create "Buttons" for each of the applications that I read. I want to assign a variable to the Button based on the section header name so that I know what application to install, based on the button the user selects. Any idea on how to create a variable on the fly in my code? :P

;Example Code:
Dim $GUI_DOCKHEIGHT, $GUI_DOCKWIDTH, $GUI_EVENT_CLOSE, $Label1, $Label2, $Manual, $GUI
Dim $Application1, $Application2, $Application3, $Application4, $Application5, $Application6, $Application7, $Application8, $Application9, $Application10
Dim $ManualAutoInstall, $ManualAutoInstall1, $ManualAutoInstall2, $ManualAutoInstall3, $ManualAutoInstall4, $ManualAutoInstall5, $ManualAutoInstall6, $ManualAutoInstall7, $ManualAutoInstall8, $ManualAutoInstall9, $ManualAutoInstall10
Dim $AutoAutoInstall, $AutoAutoInstall1, $AutoAutoInstall2,$AutoAutoInstall3,$AutoAutoInstall4,  $AutoAutoInstall5,  $AutoAutoInstall6, $AutoAutoInstall7, $AutoAutoInstall8, $AutoAutoInstall9, $AutoAutoInstall10 
#Region ### START Koda GUI section ### Form=
$AppInstall = GUICreate(IniRead("applicationstest.ini","GUIConfig","GuiTitle",""), IniRead("applicationstest.ini","GUIConfig","GuiWidth",""), IniRead("applicationstest.ini","GUIConfig","GuiHeight",""), IniRead("applicationstest.ini","GUIConfig","GUILeft",""), IniRead("applicationstest.ini","GUIConfig","GUITop",""))
$Auto = GUICtrlCreateTab(IniRead("applicationstest.ini","GUIConfig","TabLeft",""), IniRead("applicationstest.ini","GUIConfig","TabTop",""), IniRead("applicationstest.ini","GUIConfig","TabWidth",""), IniRead("applicationstest.ini","GUIConfig","TabHeight",""))
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
#Region Tab1 - Auto Apps
$Auto = GUICtrlCreateTabItem("Auto")
$AutoAppList = IniReadSectionNames("applicationstest.ini")
$RadioHeight = 150
For $i = 1 to $AutoAppList[0]
    $ApplicationName = IniRead("applicationstest.ini",$AutoAppList[$i],"Name","")
    $ApplicationDistribMode = IniRead("applicationstest.ini",$AutoAppList[$i],"DistributionMode","")
    $ApplicationInstallScript = IniRead("applicationstest.ini",$AutoAppList[$i],"InstallScript","")
    If $ApplicationDistribMode = "Auto" Then $AutoAppListName = GUICtrlCreateLabel($ApplicationName, 30, $RadioHeight, 140, 17)
    If $ApplicationDistribMode = "Auto" AND $ApplicationInstallScript <> "" Then $AutoApplistName = GUICtrlCreateButton("Auto Install", 245, $RadioHeight, 75, 17)
    If $ApplicationDistribMode = "Auto" Then $RadioHeight = $RadioHeight + 20
Next
$AutoQUITTop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$AutoUPDATETop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$AutoUpdateINI = GUICtrlCreateButton("UPDATE", 375, $AutoUPDATETop, 100, 25)
$AutoQuit = GUICtrlCreateButton("QUIT", 485, $AutoQUITTop, 100, 25)
#EndRegion
#Region Tab2 - Manual Apps
$Manual = GUICtrlCreateTabItem("Manual")
$ManualAppList = IniReadSectionNames("applicationstest.ini")
$RadioHeight = 150
For $j = 1 to $ManualAppList[0]
    $AppID = $j
    $ApplicationName = IniRead("applicationstest.ini",$ManualAppList[$j],"Name","")
    $ApplicationDistribMode = IniRead("applicationstest.ini",$ManualAppList[$j],"DistributionMode","")
    $ApplicationInstallScript = IniRead("applicationstest.ini",$ManualAppList[$j],"InstallScript","")
    If $ApplicationDistribMode = "Manual" Then $ManualAppList[$j] = GUICtrlCreateLabel($ApplicationName, 30, $RadioHeight, 140, 17)
    If $ApplicationDistribMode = "Manual" AND $ApplicationInstallScript <> "" Then $ManualAutoInstall = GUICtrlCreateButton("AutoInstall", 245, $RadioHeight, 75, 17)
    If $ApplicationDistribMode = "Manual" Then $RadioHeight = $RadioHeight + 20
    $ManualAutoInstall = $ManualAutoInstall & $AppID
;~  msgbox(4096,"Button Info", $ManualAutoInstall)
Next
$ManualQUITTop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$ManualUPDATETop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$ManualUpdateINI = GUICtrlCreateButton("UPDATE", 375, $ManualUPDATETop, 100, 25)
$ManualQuit = GUICtrlCreateButton("QUIT", 485, $ManualQUITTop, 100, 25)

#EndRegion
GUICtrlCreateTabItem("")
$Header = GUICtrlCreateLabel(IniRead("applicationstest.ini","GUIConfig","Title",""), IniRead("applicationstest.ini","GUIConfig","TitleLeft",""), IniRead("applicationstest.ini","GUIConfig","TitleDown",""), IniRead("applicationstest.ini","GUIConfig","TitleWidth",""), IniRead("applicationstest.ini","GUIConfig","TitleHeight",""))
GUICtrlSetFont(-1, 20, 800, 0, "Arial Black")
$Intro = GUICtrlCreateLabel(IniRead("applicationstest.ini","GUIConfig","Info",""), 20, 56, 600, 55)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While $GUI <> "QUIT"
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE

Case $AutoQuit
    Exit
Case $ManualQuit
    Exit    
Case $AutoUpdateINI
    msgbox(4096,"Hold on!", "Not done yet")
Case $ManualUpdateINI
    msgbox(4096,"Hold on!", "Not done yet") 
Case $Application1
    msgbox(4096,"App 1", "Application 1 Install Script goes here")
Case $Application2
    msgbox(4096,"App 2", "Application 2 Install Script goes here")
Case $Application3
    msgbox(4096,"App 3", "Application 3 Install Script goes here")
EndSwitch
WEnd

;Example INI Content:(applicationstest.ini)

[GUIConfig]

GUITitle="AppInstall"

GUIHeight="500"

GUIWidth="625"

GUILeft="-1"

GUITop="-1"

Title="AppInstall"

TitleLeft="48"

TitleDown="8"

TitleHeight="42"

TitleWidth="531"

Info="AppInfo Description"

InstallerInfo="Installer"

InstallInfo="Installed?"

Tab1Name="Auto"

Tab2Name="Manual"

TabHeight="380"

TabWidth="577"

TabLeft="24"

TabTop="112"

UpdateButtonName="UPDATE"

QuitButtonName="QUIT"

[Application1]

Name=Microsoft Word

DistributionMode=Manual

InstallScript="test.bat"

[Application2]

Name=Microsoft Excel

DistributionMode=Manual

InstallScript="test.bat"

[Application3]

Name=Microsoft Access

DistributionMode=Auto

InstallScript="test.bat"

Edited by SmOke_N
Added tags
Link to comment
Share on other sites

If you put code tags around your code, it makes posts with code easier to read. You lose formatting without it.

You could use a 2-dimensional array. Store the app name in 1 dimension, and the button in the other, i.e.:

$AppButtons[0][0] = "App 1 Name"
$AppButtons[0][1] = GUICreateButton("App 1 Button")

$AppButtons[1][0] = "App 2 Name"
$AppButtons[1][1] = GUICreateButton("App 2 Button")
Link to comment
Share on other sites

For every new app you need a new button and then that requires a new function the way you are doing it at the moment. If you use an array as rundmv suggests then you can avoid that by having one function for all apps

Case $App[0][0] to $App[$lastapp][0]
  for $n = 0 to $lastapp
    if $nMsg = $App[$n][0] then
      commonFunc($n)
      exitloop
     endif
    next

and in the function you can have some code to work out which button was pressed and then which other related properties to use.

You could also use on event mode and there is a udf I wrote to cope with the sort of problem you are dealing with and IMO it simplifies things, but it's a matter of opinion I suppose. For each application you read from the ini file there are various other pieces of information related to that application. Say these are $item1,$item2 etc.

So you could read all these items from ini as you already do, then cretae the button and then set an event to call the common function when the button is pressed like this

GuiCtrlCreateButton(......
SetOnEvent(-1,"commonFunc",5,$ParamByVal,$Item1,$ParamByVal,$Item2....)

.
.
.
Func commonFunc($par1,$par2,$par3.....)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Guys - This is what I came up with off of your suggestions. It is working now! I really appreciate the help.

include <array.au3>
Dim $GUI_DOCKHEIGHT, $GUI_DOCKWIDTH, $GUI_EVENT_CLOSE, $Label1, $Label2, $Manual, $GUI
Dim $Application1, $Application2, $Application3, $Application4, $Application5, $Application6, $Application7, $Application8, $Application9, $Application10
Dim $ManualAutoInstall, $ManualAutoInstall1, $ManualAutoInstall2, $ManualAutoInstall3, $ManualAutoInstall4, $ManualAutoInstall5, $ManualAutoInstall6, $ManualAutoInstall7, $ManualAutoInstall8, $ManualAutoInstall9, $ManualAutoInstall10
Dim $AutoAutoInstall, $AutoAutoInstall1, $AutoAutoInstall2,$AutoAutoInstall3,$AutoAutoInstall4,  $AutoAutoInstall5,  $AutoAutoInstall6, $AutoAutoInstall7, $AutoAutoInstall8, $AutoAutoInstall9, $AutoAutoInstall10 
Dim $AppButtons
;~ Local $AppButtons[10][10]
;~ $AppButtons[0][1] = "test"
#Region ### START Koda GUI section ### Form=
$AppInstall = GUICreate(IniRead("applicationstest.ini","GUIConfig","GuiTitle",""), IniRead("applicationstest.ini","GUIConfig","GuiWidth",""), IniRead("applicationstest.ini","GUIConfig","GuiHeight",""), IniRead("applicationstest.ini","GUIConfig","GUILeft",""), IniRead("applicationstest.ini","GUIConfig","GUITop",""))
$Auto = GUICtrlCreateTab(IniRead("applicationstest.ini","GUIConfig","TabLeft",""), IniRead("applicationstest.ini","GUIConfig","TabTop",""), IniRead("applicationstest.ini","GUIConfig","TabWidth",""), IniRead("applicationstest.ini","GUIConfig","TabHeight",""))
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
#Region Tab1 - Auto Apps
$Auto = GUICtrlCreateTabItem("Auto")
$AutoAppList = IniReadSectionNames("applicationstest.ini")
Global $avArray[1000][1000]
$RadioHeight = 150
For $i = 2 to $AutoAppList[0]
    $ApplicationName = IniRead("applicationstest.ini",$AutoAppList[$i],"Name","")
    $ApplicationDistribMode = IniRead("applicationstest.ini",$AutoAppList[$i],"DistributionMode","")
    $ApplicationInstallScript = IniRead("applicationstest.ini",$AutoAppList[$i],"InstallScript","")
    $avArray[$i][0] = $AutoAppList[$i]
    
    If $ApplicationDistribMode = "Auto" Then $AutoAppListName = GUICtrlCreateLabel($ApplicationName, 30, $RadioHeight, 140, 17)
    If $ApplicationDistribMode = "Auto" AND $ApplicationInstallScript <> "" Then $avArray[$i][0] = GUICtrlCreateButton("Auto Install", 245, $RadioHeight, 75, 17)
;~  msgbox(0,"ButtonName",$avArray[$i][0])
    If $ApplicationDistribMode = "Auto" Then $RadioHeight = $RadioHeight + 20
Next
$AutoQUITTop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$AutoUPDATETop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$AutoUpdateINI = GUICtrlCreateButton("UPDATE", 375, $AutoUPDATETop, 100, 25)
$AutoQuit = GUICtrlCreateButton("QUIT", 485, $AutoQUITTop, 100, 25)
#EndRegion
#Region Tab2 - Manual Apps
$Manual = GUICtrlCreateTabItem("Manual")
$ManualAppList = IniReadSectionNames("applicationstest.ini")
;~ Local $avArray[1000][1000]
$RadioHeight = 150
For $j = 2 to $ManualAppList[0]
    $AppID = $j
    $ApplicationName = IniRead("applicationstest.ini",$ManualAppList[$j],"Name","")
    $ApplicationDistribMode = IniRead("applicationstest.ini",$ManualAppList[$j],"DistributionMode","")
    $ApplicationInstallScript = IniRead("applicationstest.ini",$ManualAppList[$j],"InstallScript","")
    $avArray[$i][0] = $ManualAppList[$j]
    If $ApplicationDistribMode = "Manual" Then $ManualAppList[$j] = GUICtrlCreateLabel($ApplicationName, 30, $RadioHeight, 140, 17)
    If $ApplicationDistribMode = "Manual" AND $ApplicationInstallScript <> "" Then $avArray[$j][0] = GUICtrlCreateButton("AutoInstall", 245, $RadioHeight, 75, 17)
    If $ApplicationDistribMode = "Manual" Then $RadioHeight = $RadioHeight + 20
    $ManualAutoInstall = $ManualAutoInstall & $AppID
;~  msgbox(4096,"Button Info", $ManualAutoInstall)
Next
$ManualQUITTop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$ManualUPDATETop=IniRead("applicationstest.ini","GUIConfig","TabHeight","")+75
$ManualUpdateINI = GUICtrlCreateButton("UPDATE", 375, $ManualUPDATETop, 100, 25)
$ManualQuit = GUICtrlCreateButton("QUIT", 485, $ManualQUITTop, 100, 25)

#EndRegion
GUICtrlCreateTabItem("")
$Header = GUICtrlCreateLabel(IniRead("applicationstest.ini","GUIConfig","Title",""), IniRead("applicationstest.ini","GUIConfig","TitleLeft",""), IniRead("applicationstest.ini","GUIConfig","TitleDown",""), IniRead("applicationstest.ini","GUIConfig","TitleWidth",""), IniRead("applicationstest.ini","GUIConfig","TitleHeight",""))
GUICtrlSetFont(-1, 20, 800, 0, "Arial Black")
$Intro = GUICtrlCreateLabel(IniRead("applicationstest.ini","GUIConfig","Info",""), 20, 56, 600, 55)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###



While $GUI <> "QUIT"
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE

Case $AutoQuit
    Exit
Case $ManualQuit
    Exit    
Case $AutoUpdateINI
    msgbox(4096,"Hold on!", "Not done yet")
Case $ManualUpdateINI
    msgbox(4096,"Hold on!", "Not done yet") 
Case $avArray[2][0]
    msgbox(4096,"App 1", "Application 1 Install Script goes here")
Case $avArray[3][0]
    msgbox(4096,"App 2", "Application 2 Install Script goes here")
Case $avArray[4][0]
    msgbox(4096,"App 3", "Application 3 Install Script goes here")  
Case $avArray[5][0]
    msgbox(4096,"App 4", "Application 4 Install Script goes here")  
Case $avArray[6][0]
    msgbox(4096,"App 5", "Application 5 Install Script goes here")  
Case $avArray[7][0]
    msgbox(4096,"App 6", "Application 6 Install Script goes here")  
Case $avArray[8][0]
    msgbox(4096,"App 7", "Application 7 Install Script goes here")  
Case $avArray[9][0]
    msgbox(4096,"App 8", "Application 8 Install Script goes here")  
Case $avArray[10][0]
    msgbox(4096,"App 9", "Application 9 Install Script goes here")  
Case $avArray[11][0]
    msgbox(4096,"App 10", "Application 10 Install Script goes here")    
Case $avArray[12][0]
    msgbox(4096,"App 11", "Application 11 Install Script goes here")    
Case $avArray[13][0]
    msgbox(4096,"App 12", "Application 12 Install Script goes here")        
;~ Case $avArray2[2][0]
;~  msgbox(4096,"App2","Application 2 Install Script Goes here")    
EndSwitch
WEndoÝ÷ Ù«­¢+ÙmU%
½¹¥t)U%Q¥Ñ±ôÅÕ½ÐíÁÁ%¹Íѱ°ÅÕ½Ðì)U%!¥¡ÐôÅÕ½ÐìÔÀÀÅÕ½Ðì)U%]¥Ñ ôÅÕ½ÐìØÈÔÅÕ½Ðì)U%1ÐôÅÕ½Ðì´ÄÅÕ½Ðì)U%Q½ÀôÅÕ½Ðì´ÄÅÕ½Ðì)Q¥Ñ±ôÅÕ½ÐíÁÁ%¹Íѱ°ÅÕ½Ðì)Q¥Ñ±1ÐôÅÕ½ÐìÐàÅÕ½Ðì)Q¥Ñ±½Ý¸ôÅÕ½ÐìàÅÕ½Ðì)Q¥Ñ±!¥¡ÐôÅÕ½ÐìÐÈÅÕ½Ðì)Q¥Ñ±]¥Ñ ôÅÕ½ÐìÔÌÄÅÕ½Ðì)%¹¼ôÅÕ½ÐíÁÁ%¹¼ÍÉ¥ÁÑ¥½¸ÅÕ½Ðì)%¹Íѱ±É%¹¼ôÅÕ½Ðí%¹Íѱ±ÈÅÕ½Ðì)%¹Íѱ±%¹¼ôÅÕ½Ðí%¹Íѱ±üÅÕ½Ðì)9µôÅÕ½ÐíÕѼÅÕ½Ðì)9µôÅÕ½Ðí5¹Õ°ÅÕ½Ðì)Q!¥¡ÐôÅÕ½ÐìÌàÀÅÕ½Ðì)Q]¥Ñ ôÅÕ½ÐìÔÜÜÅÕ½Ðì)Q1ÐôÅÕ½ÐìÈÐÅÕ½Ðì)QQ½ÀôÅÕ½ÐìÄÄÈÅÕ½Ðì)UÁÑ   ÕÑѽ¹9µôÅÕ½ÐíUAQÅÕ½Ðì)EեѠ ÕÑѽ¹9µôÅÕ½ÐíEU%PÅÕ½Ðì(()mÁÁ±¥Ñ¥½¸Åt)9µôÅÕ½ÐíÁÀÄÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸Ét)9µôÅÕ½ÐíÁÀÈÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õ5¹Õ°)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸Ít)9µôÅÕ½ÐíÁÀÌÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸Ñt)9µôÅÕ½ÐíÁÀÐÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸Õt)9µôÅÕ½ÐíÁÀÔÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õ5¹Õ°)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸Ùt)9µôÅÕ½ÐíÁÀØÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸Ýt)9µôÅÕ½ÐíÁÀÜÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸át)9µôÅÕ½ÐíÁÀàÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õ5¹Õ°)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸åt)9µôÅÕ½ÐíÁÀäÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸ÄÁt)9µôÅÕ½ÐíÁÀÄÀÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸ÄÅt)9µôÅÕ½ÐíÁÀÄÄÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õ5¹Õ°)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì()mÁÁ±¥Ñ¥½¸ÄÉt)9µôÅÕ½ÐíÁÀÄÈÅÕ½Ðì)¥ÍÑÉ¥ÕÑ¥½¹5½õÕѼ)%¹Íѱ±MÉ¥ÁÐôÅÕ½ÐíÑÍйÐÅÕ½Ðì
Edited by Wader
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...