Following is What I am trying to acquire using powershell.
$usernames = @()
$usernames += [pscustomobject]@{
Firstnames=$firstname
}
$form = New-object System.Windows.Forms.Form
$form.Location = "500, 500"
$form.Text = "Add User names"
$textbox = New-object System.Windows.Forms.Textbox
$textbox.Location = "55, 130"
$textbox.size = "114, 40"
$textbox.text = ""
$button = New-object System.Windows.Forms.Button
$button.Location = "55, 150"
$button.size = "155, 23"
$button.text = Add User name
$button.Add_Click = {($firstname = $textbox.text)}
$form.controls.Add($button.Add_Click)
$form.controls.Add($textbox)
$form.ShowDialog() | Out-null
It's not entirely clear what your question is but presumably you want the .Text
values to be added to a list when clicking the button, in which case, you can approach as below. Note, you have to use a List<>
or a collection type that allows adding, an array (@()
with +=
is invalid).
Add-Type -AssemblyName System.Windows.Forms
# here is where users get added when clicking the button
$addedusers = [System.Collections.Generic.List[string]]::new()
$form = [System.Windows.Forms.Form]@{
Location = '500, 500'
Text = 'Add User names'
}
$textbox = [System.Windows.Forms.Textbox]@{
Location = '55, 130'
Size = '114, 40'
}
# this handler enables and disables the button based on the text value (if all blank = disabled)
$textbox.Add_TextChanged({
if ([string]::IsNullOrWhiteSpace($this.Text)) {
$button.Enabled = $false
return
}
$button.Enabled = $true
})
# start with the button in disabled state
$button = [System.Windows.Forms.Button]@{
Location = '55, 150'
Size = '155, 23'
Text = 'Add User name'
Enabled = $false
}
# handler to add the text value to the list<string>
$button.Add_Click({
$addedusers.Add($textbox.Text)
$textbox.Text = ''
})
$form.Controls.AddRange(@($textbox, $button))
$form.ShowDialog() | Out-Null
# show the added users when form is closed
$addedusers