Accessing Winforms Controls From A Loop Variable
i'm developing powershell script creates user accounts in active directory. the data needed create accounts entered user via form i've created winforms. i have working version of script amount of code used create form has become unwieldy. as such, i'm working on alternate version creates form using couple of loops , 40-50 lines of code.
the code below i'll briefly explain way works. i have 2 (2) text files, 1 (labels.txt) contains names of labels , text values , (textboxes.txt) has names of textboxes. inside labels.txt file multiple lines of text in format <labelname,"labeltext"> (without '<>') , inside txtboxes.txt file several lines of text have have <txtboxname>. a couple of foreach loops read in values of each file , each line of file create corresponding form element.
it works , form looks great, granted there other more aesthetically pleasing changes made. the problem i'm unable extract values of textboxes. i inserted $txtbox @ top of foreach loop , displayed each of values in text file know read data in correctly. typing $txtbox in console after running script (or inserting @ end of loop) shows textbox object declared be. however, attempts access contents of textbox return nothing. any ideas i'm doing wrong? thanks!
[void][reflection.assembly]::loadwithpartialname("system.windows.forms") [void][reflection.assembly]::loadwithpartialname("system.drawing") $testform = new-object system.windows.forms.form $testform.size = new-object system.drawing.point 500, 500 $testform.text = "test form" $testform.font = new-object system.drawing.font("calibri",12) $testform.keypreview = $true $testform.add_keydown({if ($_.keycode -eq "enter") {$testform.close()}}) $testform.add_keydown({if ($_.keycode -eq "escape") {$testform.close()}}) $lbl_vertic = 30 $lbl_horiz = 30 $txtbox_vertic = 27 $txtbox_horiz = 170 foreach ($lbl in gc 'labels.txt') { $label = "" | select name, text $label.name, $label.text = $lbl.split(',') $label.name = new-object system.windows.forms.label $label.name.size = new-object system.drawing.point 120,20 $label.name.location = new-object system.drawing.point $lbl_horiz,$lbl_vertic $label.name.text = $label.text $testform.controls.add($label.name) $lbl_vertic += 40 } foreach ($txtbox in gc 'txtboxes.txt') { $txtbox = new-object system.windows.forms.textbox $txtbox.size = new-object system.drawing.point 200,20 $txtbox.location = new-object system.drawing.point $txtbox_horiz,$txtbox_vertic $testform.controls.add($txtbox) $txtbox_vertic += 40 } [void]$testform.showdialog()
this wont work expect.
foreach ($txtbox in gc 'txtboxes.txt')
{
$txtbox = new-object system.windows.forms.textbox
all on write value in $txtbox. changes string textbox object textbox name text file doing nothing. end textboxes on form, no real way reference them since txtbox keep getting overwritten.
you create array of textbox's , handle way or use findcontrol (might find() ) method of form but either way, you'll want change forloop bit doesnt on write txtbox value.
foreach ($txtboxname in gc 'txtboxes.txt')
{
$txtbox = new-object system.windows.forms.textbox
and set name property (you pass name find control)
$txtbox.name = $txtboxname
something that... havent done c#/form type stuff in while. might able find better solution on @ c# forums since more of forms issue , not ps issue. should @ least in right direction.
Windows Server > Windows PowerShell
Comments
Post a Comment