Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How can I reference a winforms control using a variable in Powershell?

$
0
0

I have a powershell winform that has a lot of text boxes, but for the sake of this post, lets say I have 5. I want to loop through the 5 text boxes to see if their text property is empty. If the textbox contains something, I want to add the text value to an array. Here's an example of what I'm trying to do.

textbox1.text = "Red"
textbox2.text = ""
textbox3.text = "Blue"
textbox4.text = ""
textbox5.text = ""

$MyArray = @()

for ($i = 1; $i -le 5; $i++) {
    If ($textbox[$i].text -ne "") {
        $MyArray += $textbox[$i].text
    }
}

I realize the textbox object is not an array so this code will not work. I guess the simple question is, How can I reference a set of static controls using a variable without having to do something like this

[object[]]$textboxes = New-Object System.Windows.Forms.TextBox

For ($i = 1; $i -lt 5; $i++) {
    $textboxes += New-Object windows.forms.textbox

$textboxes[$i].DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation

$textboxes[$i].Name = $textboxes[$i]

$BoxItems.Controls.Add($textboxes[$i])

}


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>