Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5928297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:16:34+00:00 2026-05-22T14:16:34+00:00

I am creating a C# WinForms application in VS 2010. I have 10 TextBoxes.

  • 0

I am creating a C# WinForms application in VS 2010. I have 10 TextBoxes.

For TextBox1, I handled the events for GotFocus, LostFocus, etc. in the following manner:

this.richTextBox1.GotFocus += new System.EventHandler(this.RichTextBox1_GotFocus);
this.richTextBox1.LostFocus += new System.EventHandler(this.RichTextBox1_LostFocus);

private void TextBox1_GotFocus(object sender, System.EventArgs e)
{
    TextBox1.BackColor = Color.White;
}

private void TextBox1_LostFocus(object sender, System.EventArgs e)
{
    TextBox1.BackColor = Color.LightSteelBlue;
}

And now I need to write the same code for all of my textboxes. Is it instead possible to have a common GotFocus, LostFocus, etc. event handler method for all textboxes to do the above things?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:16:34+00:00Added an answer on May 22, 2026 at 2:16 pm

    If you really need this for 10 different textboxes, the best (and most object-oriented) thing to do would be to create your own custom textbox control that inherits from System.Windows.Forms.TextBox.

    Since you’re inheriting from the base TextBox class, you get all of its functionality for free, and you can also implement the custom behavior that you want. Then just use this custom control as a drop-in replacement for the standard textbox control wherever you want controls with this behavior on your form. This way, you don’t have to attach any event handlers at all! The behavior is built right into your control, following the guidelines of encapsulation and don’t-repeat-yourself.

    Sample class:

    public class FocusChangeTextBox : TextBox
    {
        public FocusChangeTextBox()
        {
        }
    
        protected override void OnGotFocus(EventArgs e)
        {
            // Call the base class implementation
            base.OnGotFocus();
    
            // Implement your own custom behavior
            this.BackColor = SystemColors.Window;
        }
    
        protected override void OnLostFocus(EventArgs e)
        {
            // Call the base class implementation
            base.OnLostFocus();
    
            // Implement your own custom behavior
            this.BackColor = Color.LightSteelBlue;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating C# winforms application, which connects to the Database. Because I have
I am creating a small modal form that is used in Winforms application. It
When creating scrollable user controls with .NET and WinForms I have repeatedly encountered situations
I'm creating a HUD window for inspecting biz entities in my WinForms application. I
Im creating a C# Application based on WinForms / KryptonForms, and as the application
I am creating an application in .NET using winforms. That application must draw on
When creating an instance of a button within a .NET WinForms application, the .CausesValidation
I have a new Winforms application that is replacing an existing Access order processing
I have a settings file in my Winforms application called Settings.settings with a partial
I have an application in Embarcadera RAD Studio 2010 (C++) . Is it possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.