Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Creating TextBox objects with a Loop (Inside a function)

$
0
0

Hello All,

So recently I've been doing some PS'ing and I've finished los of scripts with GUI's.

I've encountered a problem in this script though, I've asked my Company's Senior Developer and He's stumped, Powershell isn't his thing but is this even possible or can I do this a better way?

Function LoadForm
{
  ([int] $BoxCount)
   $BoxCount = 1
   $i = 1
     
[array]$global:objArray = @()

foreach ( $settings in $Form.program.section1.settings )
 {
   $global:ControlToAdd = New-Object System.Windows.Forms.TextBox
  
   $ControlToAddLabel = New-Object System.Windows.Forms.Label
  
   $ControlToAdd.Name = '$TextBox' + $BoxCount
   $ControlToAddlabel.Name = $BoxCount
  
   $ControlToAdd.BackColor = [System.Drawing.Color]::FromArgb(255,152,251,152)
  
   $ControlToAdd.DataBindings.DefaultDataSourceUpdateMode = 0
   $ControlToAddLabel.DataBindings.DefaultDataSourceUpdateMode = 0

  If ($BoxCount -eq 7)
       {   $System_Drawing_Point.X  = $System_Drawing_Point.X + 300
        $System_Drawing_Point_Label.X  = $System_Drawing_Point_Label.X + 300
           $i = 1
    }
   
   $System_Drawing_Point.Y = 5 + ($i * 25)
   $System_Drawing_Point_Label.Y = 5 + ($i * 25)
  
   $ControlToAdd.Location = $System_Drawing_Point
   $ControlToAddLabel.Location = $System_Drawing_Point_Label
  
   $System_Drawing_Size = New-Object System.Drawing.Size
   $System_Drawing_Size.Height = 23
   $System_Drawing_Size.Width = 130
  
   $ControlToAdd.Size = $System_Drawing_Size
   $ControlToAddlabel.Size = $System_Drawing_Size
  
   $ControlToAddLabel.Text = $settings.id
  
   $global:objArray += $ControlToAdd
  
   $Details.Controls.Add($ControlToAdd)
   $Details.Controls.Add($ControlToAddLabel)

   $BoxCount++
   $i++
 }
}

 

This is only 10% of this function but It's the only bit that matters.

So what I'm trying to do in that code is read value from an XML (Works) to then run this loop for each value in the XML, It runs 8 times on the standard XML so lets use that as the loop count.

I want to create 8 Textboxes all with different name which will then be displayed on the page, This works however i cannot then, Later in the program refer back to the value in these textboxes apart from the last one because $ControlToAdd is overwriten every time the loop runs.

I want to refer back to them like $SomeValue =  $TextBox5.Text, To do this is tried setting $ControlToAdd.Name to what I want it to be named, Still to no avail

 

Please Help!


Viewing all articles
Browse latest Browse all 8583

Trending Articles