Need to create, use and store dynamic object names in Powershell

Multi tool use
Multi tool use


Need to create, use and store dynamic object names in Powershell



I am loading in a text file and looping through each line, and trying to print a checkbox, in a powershell form. However, the way below, all checkboxes have the same variable/object name, which makes it impossible to tell them apart.



I need a way to dynamically create $checkbox0 through $checkbox(# of lines in text file, which can change), and fill them in below, as well as store the names, so i can validate if they are clicked later


$pFile = Get-Content "C:results.txt"
$rowCounter = 0
foreach($line in $pFile){
$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$checkbox.AutoSize = "true"
$checkbox.TabIndex = $rowCounter
$checkbox.Text = $line
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 25
$yValue = (20 * $rowCounter)
$System_Drawing_Point.Y = $yValue
$checkbox.Location = $System_Drawing_Point
$checkbox.DataBindings.DefaultDataSourceUpdateMode = 0
$Form1.Controls.Add($checkbox)
$rowCounter = $rowCounter + 1
}





Any particular reason why you can't simply use an array instead of individually named numbered variables?
– Ansgar Wiechers
Jul 1 at 22:58





Care to elaborate? I dont know how many rows of the text file there will be, so i dont know how many variables i will need.
– Me Myself
Jul 1 at 23:29





What do you think you can do via $checkbox0 that you cannot do via $checkbox[0]?
– Ansgar Wiechers
Jul 2 at 7:41


$checkbox0


$checkbox[0]




1 Answer
1



Control has Name property which is a string. Assigning a unique name to the control, you can find it later in the Controls collection of the parent control.


Control


Name


Controls



Also if you correctly assign an event handler to an event of the control, you can receive the control in the sender parameter of the event handler.



In the following code, I've created a list of CheckBox controls and handles their CheckedChanged event for them:


CheckBox


CheckedChanged


Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$flp = New-Object System.Windows.Forms.FlowLayoutPanel
$form.Controls.Add($flp)
#Let's say you read the following values from file
$array = ("Lorem", "Ipsum", "Dolor")
$i = 0
$array | % {
$checkBox = New-Object System.Windows.Forms.CheckBox
$checkBox.Text = $_
$checkBox.Name = "checkBox_$i"
$flp.Controls.Add($checkBox)
$checkBox.Add_CheckedChanged({
$index = $flp.Controls.IndexOf($this)
$name = $this.Name
$text = $this.Text
$value = $this.Checked
[System.Windows.Forms.MessageBox]::Show("Index: $index" +"`n" +
"Name: $name" + "`n" +
"Text: $text" + "`n" +
"Value: $value" + "`n")
})
$i++
}
$form.ShowDialog()
$form.Dispose()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Ltx,qWBqsDJqJz2h0ZF0qfGp93huBOkawNOazO9xWr8MU,LkEoA1oO 52IYFO E4UQ1OKc 2vVksxDyLrh
wvlgUE6,Vednn3dbJPUfvTIAvl,5 woYOChy3vDa8a6QK3gy05sRJGHh gmNYQy3tVZH

Popular posts from this blog

Rothschild family

Boo (programming language)