Sign in to follow this  
Followers 0

Auto increment variable name

11 posts in this topic

Posted

Hello,

I'm using a button which creates a new input box. However, I want that input box to have a unique name. How can I make it add a number behind the variable so it will name them uniquely?

So it will create with each press of a button:

$CaseNum1 = GUICtrlCreateInput("blah", 32, 40, 121, 21)

$CaseNum2 = GUICtrlCreateInput("blah", 32, 40, 121, 21)

$CaseNum3 = GUICtrlCreateInput("blah", 32, 40, 121, 21)

$CaseNum4 = GUICtrlCreateInput("blah", 32, 40, 121, 21)

$CaseNum5 = GUICtrlCreateInput("blah", 32, 40, 121, 21)

$CaseNum6 = GUICtrlCreateInput("blah", 32, 40, 121, 21)

Thanks!

Dennis

p.s: bonus question: how do i read all these values based on a array?

Share this post


Link to post
Share on other sites



Posted (edited)

Use Arrays!

bonus answer:

dim $array[20]
;somethinghere to fill the array
For $i=0 to ubound($array)-1 ;for loop for each array element
msgbox(0,"",$array[$i])
next

cheers

Edited by torels

Share this post


Link to post
Share on other sites

Posted

hi torels, thank you for your swift answer.

I'm kind of new with Autoit, so could you please provide me a concrete example how to implement this?

Many thanks,

Dennis

Share this post


Link to post
Share on other sites

Posted

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $array[10]
;somethinghere to fill the array

Local $msg

GUICreate("My GUI") ; will create a dialog box that when displayed is centered

For $i = 0 To UBound($array) - 1 ;for loop for each array element
    GUICtrlCreateLabel($i,10,13 + $i*20)
    $array[$i] = GUICtrlCreateInput("", 30, 10 + $i*20, 121, 20)
Next

GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Share this post


Link to post
Share on other sites

Posted

I created:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $array[10]
;somethinghere to fill the array

Local $msg

GUICreate("My GUI") ; will create a dialog box that when displayed is centered

For $i = 0 To UBound($array) - 1 ;for loop for each array element
    GUICtrlCreateLabel($i,10,13 + $i*20)
    $array[$i] = GUICtrlCreateInput("", 30, 10 + $i*20, 121, 20)
Next
$SendButton = GUICtrlCreateButton("SND", 325, 10, 50, 20)

GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Switch $msg 
        Case $SendButton
        dim $array[10]
        ;somethinghere to fill the array
        For $i=0 to ubound($array)-1 ;for loop for each array element
        msgbox(0,"",$array[$i])
        next
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
GUIDelete()

However, its not returning the inputbox value in the msgbox. What is wrong?

Share this post


Link to post
Share on other sites

Posted

its not returning the inputbox value in the msgbox. What is wrong?

1) You not should to Dim it again.

2) You need to read the value using GuiCtrlRead().

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $array[10]
;somethinghere to fill the array

GUICreate("My GUI") ; will create a dialog box that when displayed is centered

For $i = 0 To UBound($array) - 1 ;for loop for each array element
    GUICtrlCreateLabel($i,10,13 + $i*20)
    $array[$i] = GUICtrlCreateInput("", 30, 10 + $i*20, 121, 20)
Next

$SendButton = GUICtrlCreateButton("SND", 325, 10, 50, 20)

GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
    Switch $msg 
        Case $SendButton
            ;dim $array[10]
            
            ;somethinghere to fill the array
            For $i = 0 To UBound($array)-1 ;for loop for each array element
                MsgBox(0, "", GUICtrlRead($array[$i]))
            next
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

Posted

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Dim $array[10]
;somethinghere to fill the array

Local $msg

GUICreate("My GUI") ; will create a dialog box that when displayed is centered

For $i = 0 To UBound($array) - 1 ;for loop for each array element
    GUICtrlCreateLabel($i, 10, 13 + $i * 20)
    $array[$i] = GUICtrlCreateInput("", 30, 10 + $i * 20, 121, 20)
Next
$SendButton = GUICtrlCreateButton("SND", 325, 10, 50, 20)

GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $SendButton
            ;somethinghere to fill the array
            For $i = 0 To UBound($array) - 1 ;for loop for each array element
                MsgBox(0, "", GUICtrlRead($array[$i]))
            Next
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
GUIDelete()

Share this post


Link to post
Share on other sites

Posted

Yet another demo, where the array of input control IDs is used to do something functional (modified from KaFu's example):

#include <GUIConstantsEx.au3>

Global $avInputs[10], $iValNew, $iValOld = 0

GUICreate("My GUI"); will create a dialog box that when displayed is centered
For $i = 0 To UBound($avInputs) - 1;for loop for each array element
    GUICtrlCreateLabel($i, 10, 13 + ($i * 20))
    $avInputs[$i] = GUICtrlCreateInput("0", 30, 10 + ($i * 20), 121, 20)
Next
$Label_Total = GUICtrlCreateLabel("Total = " & $iValOld, 10, 20 * (UBound($avInputs) + 1), 121, 20)
GUISetState(@SW_SHOW); will display an empty dialog box

; Run the GUI until the dialog is closed
Do
    $iValNew = 0
    For $n = 0 To UBound($avInputs) - 1
        $iValNew += Number(GUICtrlRead($avInputs[$n]))
    Next
    
    If $iValNew <> $iValOld Then
        $iValOld = $iValNew
        GUICtrlSetData($Label_Total, "Total = " & $iValOld)
    EndIf
Until GUIGetMsg() = $GUI_EVENT_CLOSE

:)

Share this post


Link to post
Share on other sites

Posted

ok, last question: As I mentioned before I use a button to create a new inputbox. This was simplified for you to understand. To get some background on what i would like to do, please look here.

Basically i'm slowly building a array, based on if there are cases or not. When i want a extra case, i click the button for it and i want it to:

create a tab [got this working]

create a inputbox with a unique id

create a combobox with a unique id

create a edit box with a unique id

In the end i would like to grab all the filled in inputboxes, comboboxes and editboxes from every tab so i can do stuff with it (in this case, putting it in a email and send it away).

How can I fill the array slowly so it won't create 10 inputboxes at once, but one everytime i press a button?

Thanks you very much for your support,

Dennis

Share this post


Link to post
Share on other sites

Posted

If you insist on creating dynamic variables you can use Assign() although Arrays are a better fit for controls.

Dim $TempID

For $i = 1 To 6
    $TempID = GUICtrlCreateInput("blah", 32, 40, 121, 21)
    Assign("CaseNum", $TempID, 2)
Next

Share this post


Link to post
Share on other sites

Posted

ok, last question: As I mentioned before I use a button to create a new inputbox. This was simplified for you to understand. To get some background on what i would like to do, please look here.

Basically i'm slowly building a array, based on if there are cases or not. When i want a extra case, i click the button for it and i want it to:

create a tab [got this working]

create a inputbox with a unique id

create a combobox with a unique id

create a edit box with a unique id

In the end i would like to grab all the filled in inputboxes, comboboxes and editboxes from every tab so i can do stuff with it (in this case, putting it in a email and send it away).

How can I fill the array slowly so it won't create 10 inputboxes at once, but one everytime i press a button?

Thanks you very much for your support,

Dennis

So you have the IDs of tab, input, combo, and edit. That's four elements for each tab. You can keep that in a 1D array by some basic math:
Global $iTab = 0; Tab instance 0 is the first tab
Global $avTabControls[$iTab * 4]
$avTabControls[($iTab * 4)] = GUICtrlCreateTabItem(...); Tab control ID
$avTabControls[($iTab * 4) + 1] = GUICtrlCreateInput(...); Input control ID
$avTabControls[($iTab * 4) + 2] = GUICtrlCreateCombo(...); Combo control ID
$avTabControls[($iTab * 4) + 3] = GUICtrlCreateEdit(...); Edit control ID

The first tab instance is 0, so the controls IDs are at [0] thru [3], the second tab instance is 1, so the IDs are at [4] thru [7], etc.

If you are already comfortable with 2D arrays, my preference is to track them that way:

Global $avTabControls[3][4]

$avTabControls[0][0] = GUICtrlCreateTabItem(...); Tab control ID
$avTabControls[0][1] = GUICtrlCreateInput(...); Input control ID
$avTabControls[0][2] = GUICtrlCreateCombo(...); Combo control ID
$avTabControls[0][3] = GUICtrlCreateEdit(...); Edit control ID

The first tab's controls are at [0][0] thru [0][3], then second tab's ID are at [1][0] thru [1][3], etc.

:)

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  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.