Page 1 of 1

Clear all textboxes in a form

Posted: Mon Apr 08, 2019 11:59 pm
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

Re: Clear all textboxes in a form

Posted: Tue Apr 09, 2019 12:22 am
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() }

Re: Clear all textboxes in a form

Posted: Tue Apr 09, 2019 1:28 am
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.

Re: Clear all textboxes in a form

Posted: Tue Apr 09, 2019 1:31 am
by mavis99
maybe the problem is, that all of my textboxes are within a tabcontrol

[SOLVED] Clear all textboxes in a form

Posted: Tue Apr 09, 2019 2:00 am
by mavis99
Got it:

Code: Select all

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