1

I built a dynamic Powershell GUI but i have trouble getting my buttons to work correctly.

I created a function to add texboxes and buttons. However they do not correspond correctly at this point. because i'm not sure how to bind the add_click to a specific textbox.

Here is the example code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(300,300)  

$ButtonAdd = New-Object System.Windows.Forms.Button
$ButtonAdd.Location = New-Object System.Drawing.Point(20,20)
$ButtonAdd.Size = New-Object System.Drawing.Size(50,30)
$ButtonAdd.Text = 'Add'
$Form.Controls.Add($ButtonAdd)

$global:Counter = 0
$global:ButtonPosY = 20
$global:DefaultTextValue = ""

$ButtonAdd.Add_Click{Add-Fields}

Function Create-Button {
    param ( $ButtonPosY, $TextBoxPosY)

    $TextBoxField = New-Object System.Windows.Forms.TextBox
    $TextBoxField.Location = New-Object System.Drawing.Point(181,$TextBoxPosY)
    $TextBoxField.Size = New-Object System.Drawing.Size(50,30)
    $TextBoxField.Text = $global:DefaultTextValue
    New-Variable -name TextBoxField$global:Counter -Value $TextBoxField -Scope Global -Force
    $Form.controls.Add($((Get-Variable -name TextBoxField$global:Counter).value))

    $ButtonClick = New-Object System.Windows.Forms.Button
    $ButtonClick.Location = New-Object System.Drawing.Point(100,$global:ButtonPosY)
    $ButtonClick.Size = New-Object System.Drawing.Size(50,30)
    $ButtonClick.Text = 'Click'
    New-Variable -name ButtonClick$global:Counter -Value $ButtonClick -Scope Global -Force
    $Form.controls.Add($((Get-Variable -name ButtonClick$global:Counter).value))

    $ButtonClick.Add_Click({
        $((Get-Variable -name TextBoxField$global:Counter -Scope Global).value).Text = 'hello'
    })
}

Function Add-Fields {

    $global:Counter = $global:Counter + 1
    $global:ButtonPosY = $global:ButtonPosY + 40

    Create-Button -TextBoxPosY $global:ButtonPosY -ButtonPosY $global:ButtonPosY

}

Create-Button -TextBoxPosY 21 -ButtonPosY 20

$Form.ShowDialog()

If the click button is pressed each time after new input is added everything works fine. however if multiple input fields are added first and then the click button is pressed the code breaks.

The Problem is here:

    $ButtonClick.Add_Click({...})

I don't know how to add the counter (in my case $global:Counter) to the $ButtonClick variable as in $ButtonClick0, $ButtonClick1, ... etc. So right now when i add more buttons by calling the function the input will always be applied to the last added textbox since the add_click is not linked to a individual $ButtonClick0 variable.

How would this be done right?

2
  • you cannot add event handlers in function. Handlers requires specific control objects. You can return the Object created in function and use the returned object to call $ReturnObject.Add_Click({'whatever'}) Commented Sep 9, 2019 at 4:01
  • How would i then know what button was pushed and then store the returned value in the correct TextBox? Please provide an example. Commented Sep 10, 2019 at 3:24

1 Answer 1

1

After some serious reading i figured it out.

A name needs to be added to $ButtonClick

$ButtonClick.Name = $global:Counter

And the Add_Click event should look the following:

$ButtonClick.Add_Click({
    [System.Object]$Sender = $args[0]
    $((Get-Variable -name ('TextBoxField' + [int]$Sender.Name)).value).Text = 'hello'
})

However i don't know if this is how it should be done. Please post your solution if it looks different.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.