Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2004

Tip: Looking for answers? Try searching our database.

Clearing Multiple TextBoxes in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bernden - 23 Jul 2004 13:45 GMT
Good Morning

I am working on a C# project  that has a form which contains multiple
columns of 10 textBoxes in seperate groupBoxes on a tabControl.
When the form loads, I would like to make sure that text property of each
individual textBox is set to "0.00" .
When the form closes, I would like to ensure that the text property of ALL
textboxes is re-set to "null" or "  " .

Is there a way to do this without having to list each textBox and set its
text property individually ?

Any help / insight would be appreciated.

Dennis
Tim Wilson - 23 Jul 2004 13:55 GMT
You should be able to adapt the recursive code at the link below.
http://www.google.ca/groups?hl=en&lr=&ie=UTF-8&selm=%23o4DxjF4DHA.2332%40TK2MSFT
NGP10.phx.gbl


Signature

Tim Wilson
.Net Compact Framework MVP

> Good Morning
>
[quoted text clipped - 11 lines]
>
> Dennis
bernden - 23 Jul 2004 16:30 GMT
Good Morning Tim

Thank you for that quick and timely reply.
I tried it out and it worked like a charm. In fact it worked so well that I
adjusted it to suit the needs of specific events
within my little program and they did the job perfectly.
Here is an example of those adjustments. Perhaps another C# novice like
myself can benefit from your assistance as I did.

sample
   private void cboxS1_Checked(object sender, System.EventArgs e)
   {
       foreach(Control ctrl in this.gboxStdS1.Controls)
           {
               if (ctrl is TextBox)
                   ((TextBox)ctrl).Text = "0.00" ;
           }

Thanks again
Dennis

> You should be able to adapt the recursive code at the link below.

http://www.google.ca/groups?hl=en&lr=&ie=UTF-8&selm=%23o4DxjF4DHA.2332%40TK2MSFT
NGP10.phx.gbl


> > Good Morning
> >
[quoted text clipped - 11 lines]
> >
> > Dennis
Rob Styles - 26 Jul 2004 21:49 GMT
A more encapsulated approach is to make the textbox itself responsible
for setting and resetting its values - by inheriting and changing the
TextBox behaviour.

First create a new type of Control:

/// <summary>
/// CustomTextBox takes responsibility for
/// initialising and resetting itself.
/// </summary>
public class CustomTextBox : System.Windows.Forms.TextBox
{
    private void SetToNull(object sender, EventArgs eventArgs)
    {
        Text = string.Empty;
    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        Text = "0.00";
        Form form = FindForm();
        form.Closed += new EventHandler(SetToNull);
    }
}

Then change the codebehind of the form to use the custom textbox:

private void InitializeComponent()
{
    ...
    this.textBox1 = new CustomTextBox();
    ...
}

That way there's no risk of you changing other text boxes by mistake.

rob styles

> Good Morning
>
[quoted text clipped - 11 lines]
>
> Dennis

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.