Amne
Amne

Reputation: 83

How to remove form "items" in Powershell

I have a query. I have this Powershell form, with a textbox, a button and a label! When I press the button it creates a label onto the form with information regarding what I've entered into the textbox. And that works. Now, what I'm trying to achieve now is: when I press the button a second time, I would like to information to be replaced with new information (if the text that I've entered into the textbox has been updated, for instance) But I cannot workaround on how to achieve that.

I've tried with both $main_form.Remove($mylabel) and $main_form.Refresh() after I've pressed the button a second time - but none have been successful!

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Testing"
$main_form.Width = 715
$main_form.height = 300
$main_form.FormBorderStyle = "FixedDialog"
$main_form.AutoSize = $true
$main_form.MaximizeBox = $false
$main_form.MinimizeBox = $false

$CheckInfo = New-Object System.Windows.Forms.TextBox
$CheckInfo.Width = 200
$CheckInfo.Height = 100
$CheckInfo.Location = New-Object System.Drawing.Point(250,53)
$CheckInfo.AutoSize = $true
$main_form.Controls.Add($CheckInfo)
$CheckButton = New-Object System.Windows.Forms.Button
$CheckButton.Location = New-Object System.Drawing.Point(250,100)
$CheckButton.Size = New-Object System.Drawing.Size(100,20)
$CheckButton.Text = "Check Information test"
$CheckButton.AutoSize = $true
$main_form.Controls.Add($CheckButton)

$CheckButton.Add_Click({
    $CheckButtonLabelsuccess = New-Object System.Windows.Forms.Label
    $CheckButtonLabelsuccess.Text = "$CheckInfo.Text"
    $CheckButtonLabelsuccess.Location = New-Object System.Drawing.Point(250,150)
    $CheckButtonLabelsuccess.ForeColor = "#50ed07"
    $CheckButtonLabelsuccess.AutoSize = $true
    $main_form.Controls.Add($CheckButtonLabelsuccess)
})

$main_form.Topmost = $true
$main_form.ShowDialog()

The example above does not include the $main_form.Remove() or $main_form.Refresh() codes, since I've probably added them in the wrong place for them to fully work, I cut them out for now

So my question: Where would I enter those codes ( .Remove() , .Refresh() ) and how do they work? I've tried searching around but didn't find a solution that fitted my situation. I am sorry if this has been asked here before, but I couldn't find any look-alike

Any help getting me to solve this is hugely appreciated :)

Upvotes: 2

Views: 3380

Answers (1)

Theo
Theo

Reputation: 61148

If I understand the question correctly, you would like the label removed whenever the user clicks the button for the second time.

You can do this by giving the label you dynamically create a Name and use that name to test if the label is already added to the form or not.

Change your Click event to:

$CheckButton.Add_Click({
    if ($main_form.Controls.ContainsKey("MyInfoLabel")) {
        # the label is already present. Remove it
        $main_form.Controls.RemoveByKey("MyInfoLabel")
    }
    else {
        # no control with that name found, so create it
        $CheckButtonLabelsuccess = New-Object System.Windows.Forms.Label
        $CheckButtonLabelsuccess.Name = "MyInfoLabel"
        $CheckButtonLabelsuccess.Text = "Info blablabla"
        $CheckButtonLabelsuccess.Location = New-Object System.Drawing.Point(250,150)
        $CheckButtonLabelsuccess.ForeColor = "#50ed07"
        $CheckButtonLabelsuccess.AutoSize = $true
        $main_form.Controls.Add($CheckButtonLabelsuccess)
    }
})

Hope that helps

Upvotes: 4

Related Questions