Clear all textboxes in a form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
mavis99
Posts: 4
Joined: Wed Jun 14, 2017 7:12 am

Clear all textboxes in a form

Post by mavis99 » Mon Apr 08, 2019 11:59 pm

Hi,

I have a form with 10 textboxes. With a button i want to clear all of them:

Code: Select all

$button_Click={
	foreach ($tb in (Get-Variable | where { $_.value -is [System.Windows.Forms.Textbox] }))
	{
		$tb.Text = ""
	}
}
The problem is, that $tb.Text is not an allowed option for this object, ironically $tb.Name is allowed.
Is it possible to clear them with a foreach?

Thanks in advance

jvierra
Posts: 13999
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Clear all textboxes in a form

Post by jvierra » Tue Apr 09, 2019 12:22 am

You are getting the variable and not the contents.

Code: Select all

$form1.Controls |
    Where-Object{ $_ -is [system.windows.forms.textbox] } | 
    ForEach-Object{ $_.Clear() }

User avatar
mavis99
Posts: 4
Joined: Wed Jun 14, 2017 7:12 am

Re: Clear all textboxes in a form

Post by mavis99 » Tue Apr 09, 2019 1:28 am

Thanks for your quick response :)
If I write a raw PS1 script, then your script runs as expected.
But if I use the Sapien Designer (Powershell Studio 2019 v5.6.160) and double click my button and add your script nothing happens.

User avatar
mavis99
Posts: 4
Joined: Wed Jun 14, 2017 7:12 am

Re: Clear all textboxes in a form

Post by mavis99 » Tue Apr 09, 2019 1:31 am

maybe the problem is, that all of my textboxes are within a tabcontrol

User avatar
mavis99
Posts: 4
Joined: Wed Jun 14, 2017 7:12 am

[SOLVED] Clear all textboxes in a form

Post by mavis99 » Tue Apr 09, 2019 2:00 am

Got it:

Code: Select all

$tabpage1.Controls | Where-Object{ $_ -is [System.Windows.Forms.Textbox] } | ForEach-Object { $_.clear() }
Thanks for your support :)

Locked