How to enumerate textbox controls on a form?

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
rtiel1
Posts: 15
Joined: Thu Jul 26, 2012 11:34 pm

How to enumerate textbox controls on a form?

Post by rtiel1 » Tue Feb 05, 2013 5:54 am

How can I enumerate all textboxes on a form? I want to clear all textboxes on a form with one click without having to specify each control. When I use eg $testform.Controls it returns only buttons, groupboxes etc but no text- or comboboxes. I'm using Powershell Studio 2012 latest version.

Thnx Remco

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

How to enumerate textbox controls on a form?

Post by davidc » Tue Feb 05, 2013 6:06 am

The Controls property only contains the direct child controls. So if the textboxes are located in a group box or any other container control, then you will need to use the groupbox's Controls property instead.

foreach($control in $groupBox.Controls)
{
if($control -is [System.Windows.Forms.TextBox])
{
$control.Enabled = $false
}
}

Note: You can tell that a control is a container by using the following:

if( $control -is [System.Windows.Forms.ContainerControl])
....

David
David
SAPIEN Technologies, Inc.

User avatar
rtiel1
Posts: 15
Joined: Thu Jul 26, 2012 11:34 pm

How to enumerate textbox controls on a form?

Post by rtiel1 » Tue Feb 05, 2013 6:11 am

Thnx for the explanation, another thing learned today :-)