Re: Clear all TextBox on a Form

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



static void ClearTextBoxes(Control.ControlCollection controls)
{
// Cycle through the controls.
foreach (Control control in controls)
{
// The textbox.
TextBoxBase textBox = (control as TextBoxBase);

// If there is a textbox, then clear it.
if (textBox != null)
{
// Set the text to an empty string.
textBox.Text = "";
}
}
}

Note that this will clear RichTextBoxes as well. If you want to only
clear textboxes, then you need to change the "(control as TextBoxBase)"
statement to "(control as TextBox)".

Furthermore, if you have a need to clear composite controls, then you
will have to iterate through the controls collection of each control you
come across at the top level, and so on, and so on until there are no more
children.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"RP" <rpk.general@xxxxxxxxx> wrote in message
news:1187120796.743372.77410@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Oh, I want that itself. Can you please illustrate on how to use
ControlCollection?

On Aug 15, 12:38 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
No, there isn't. You will have to cycle through all the textboxes on
the form, and then clear the text out yourself. It should be easy enough
to
do, given a ControlsCollection instance, you can use the "as" operator to
determine if the control is a TextBox or not (or, TextBoxBase). If it
is,
then you can set the Text property to an empty string.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx




.



Relevant Pages

  • Re: Problem optaining focus when calling select in UserControl
    ... > that this is a bug, which I'm not quite sure of, then you can file a bug ... >> UserControl in the GotFocus-event of a TextBox. ... >>> The Control class uses a selection scheme which tries to find the ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Write, Move, Delete text on PictureBox
    ... Public Function WYSIWYG_TextBoxText(TB As TextBox) As String ... Simply place a Label control ... Now, whenever you want create a Label, you would just use ... Err.Raise 10001, "InitializeTextBox Subroutine", _ ...
    (comp.lang.basic.visual.misc)
  • Re: System.Windows.Forms.DateTimePicker
    ... and that is to "float" the textbox over whatever cell the user ... in my StartDate. ... Does anybody know how to put a masked textbox in a datagridview control? ... Dim dateAppt As DateTime ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • RE: Overriding TEXTBOX controls vb.net
    ... and once i create the control which is from the ... textbox in the design time, say for example border i changed, theirafter this ... then use this custom control in your projects. ... Microsoft Online Community Support ...
    (microsoft.public.vsnet.general)
  • Re: Symbols to replace text in an InputBox used for password.
    ... I'm new to using the Control Toolbox. ... Microsoft PowerPoint MVP ... I've inserted two shapes with the textbox in which the password is ...
    (microsoft.public.powerpoint)