Zsolt
Zsolt

Reputation: 39

Use array or variable for naming textbox object

I am working on a program that has an unknown number of textboxes displayed. These textboxes later will display data about different servers (ping data for ex.) It is unknown because I don't know how many servers will be in work, they will be automatically selected from a list... it can be 2, 3 or 15. The text boxes will appear in a new window that is sized according to the number of servers. The problem is that I have difficulties referring to the textboxes later in the program.

My first attempt was this:

Created a function to display the textboxes:

function c_inputbox ($iname, $iposx, $iposy, $isizex, $isizey)
{
   $iname = New-Object System.Windows.Forms.richTextBox 
   $iname.Location = New-Object System.Drawing.Size($iposx, $iposy) 
   $iname.Size = New-Object System.Drawing.Size($isizex, $isizey) 
   $iname 
}

Then I generate the textboxes, $objform1 is the main window, $x and $y are variables to position the textboxes in colums:

foreach ($srv in $stringArray)
{
   $name = "textbox" + $i   
   $objform1.Controls.Add((c_inputbox $name $x ($y + 20)  350 100))
   $i ++
}

It displayes the textboxes as I want but referring to the .text property doesn't work anymore. I tried several ways to test it:

$textbox1.text = "test"
$name.text = "test"

My second attempt was to store the names in an array, I tried even filling up the array with names before declaring the texboxes ($length contains the number of servers):

$j = 1
for ($j; $j -le $length; $j++)
{
   $textbox[$j] = "textbox" + $j
}

So now the array should contain "textbox1", "textbox2", etc. Then I try to call them again to define them as textbox objects:

$textbox[$i] = New-Object System.Windows.Forms.richTextBox 
$textbox[$i].Location = New-Object System.Drawing.Size($positionx, $positiony) 
$textbox[$i].Size = New-Object System.Drawing.Size(350, 100) 
$objform1.Controls.Add($textbox[$i])

But again PowerShell returns an error:

Cannot index into a null array.
+              $objform1.Controls.Add($textbox[ <<<< $i])
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Any idea how to make this happen, or if it is even possible to do in PowerShell?

Upvotes: 1

Views: 1680

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174815

Create and assign the control to a local variable, configure it as necessary, then add it to an array before adding it to Form.Controls:

$TextBoxes = @()
for($i = 0; $i -lt $stringArray.Count; $i++)
{
    # Create the textbox
    $name = "textbox$i"
    $textBox = c_inputbox $name $x ($y + 20)  350 100

    # Customise it
    $textBox.Text = $stringArray[$i]

    # Add to array
    $TextBoxes += $textBox

    # Add to Form Controls, index -1 is the last item in the array 
    $objform1.Controls.Add($TextBoxes[-1])

}

Now you can use $TextBoxes to refer to the boxes, or $TextBoxes[$index] to refer to a specific one

Upvotes: 2

Related Questions