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.
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mavis99
Posts: 4
Last visit: Mon Apr 03, 2023 10:55 pm

Clear all textboxes in a form

Post by mavis99 »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Clear all textboxes in a form

Post by jvierra »

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
Last visit: Mon Apr 03, 2023 10:55 pm

Re: Clear all textboxes in a form

Post by mavis99 »

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
Last visit: Mon Apr 03, 2023 10:55 pm

Re: Clear all textboxes in a form

Post by mavis99 »

maybe the problem is, that all of my textboxes are within a tabcontrol
User avatar
mavis99
Posts: 4
Last visit: Mon Apr 03, 2023 10:55 pm

[SOLVED] Clear all textboxes in a form

Post by mavis99 »

Got it:

Code: Select all

$tabpage1.Controls | Where-Object{ $_ -is [System.Windows.Forms.Textbox] } | ForEach-Object { $_.clear() }
Thanks for your support :)
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked