1

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?

4
  • 1
    Can you show us how you initially use $textbox? Do you declare it, or assign anything to it, prior to $textbox[$i] = New-Object System.Windows.Forms.RichTextBox? Commented Jan 5, 2016 at 18:05
  • I tryed to fill up the array with names, added the code to the original post Commented Jan 5, 2016 at 18:16
  • I think you might be overcomplicating this a bit Commented Jan 5, 2016 at 18:18
  • Yes I did... your solution is simple and works well. Commented Jan 5, 2016 at 18:23

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.