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

  • Home
  • SEARCH
  • 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 4346242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:08:51+00:00 2026-05-21T12:08:51+00:00

This has happened many times before, but I never bothered to figure out why,

  • 0

This has happened many times before, but I never bothered to figure out why, and now I am tired of it:

For instance, I derive a class from RichTextBox or Panel, I rebuild my project to have the class added to the VS designer toolbox, and then I drag & drop the custom user control to a Form. Everything works fine, and I can run my project…

The problem comes when I edit properties of the Form or the custom user control through the designer. Sometimes, the designer removes the initialization line from its code-behind, causing an exception in the designer and the executable because the control remains uninitialized.

In other words, the following line is removed from say, Form1.Designer.cs:

this.customRichTextBox1=new CustomRichTextBox();

No other line is removed from the code-behind, so the attributes of the custom control are still set, although the variable stays uninitialized.

My solution has always been to manually initialize my user control in the designer code-behind, but the designer eventually removes it again.

I believe that this does not happen when I build a Custom UserControl through the designer (but I am not completely sure of this). It only happens when I define something like the following manually:

class CustomRichTextBox:RichTextBox{}

This is so annoying. What am I doing wrong?


As @Cody requested, here are the steps to reproduce the problem. I am using VS2010, but I’ve had this problem since 2005, I think.

Step 1. Create new Windows Forms Application, any Framework

Step 2. Add the following class below your main Form class: (It just happens that this is the control that is causing me this problem this time.)

class CustomRichTextBox : RichTextBox
{
    Timer tt = new Timer();

    internal CustomRichTextBox()
    {
        tt.Tick += new EventHandler(tt_Tick);
        tt.Interval = 200;
    }


    protected override void OnTextChanged(EventArgs e)
    {
        tt.Stop();
        tt.Start();
    }

    void tt_Tick(object sender, EventArgs e)
    {
        System.Diagnostics.Trace.WriteLine("Hello world!");
    }
}

Step 3. Press F6 to rebuild.

Step 4. Add the CustomRichTextBox control to your Form by dragging and dropping from the Toolbox.

Step 5. If you wish, you may press F5 to test the application, but it should work. Close the running application.

Step 6. Press F6 to rebuild, and at this point, the designer should crash with the following message: “The variable ‘customRichTextBox1’ is either undeclared or was never assigned.” (In one case, the whole VS completely crashed, but the error is usually contained within the designer.)

Step 7. To correct the issue, go into the code-behind and initialize the variable, but next time you rebuild, the initialization line will be gone.

  • 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-21T12:08:52+00:00Added an answer on May 21, 2026 at 12:08 pm

    Thanks to everyone who tried answering my question and who posted comments that helped me diagnose and solve the problem.

    The problem occurs when using an “internal” keyword with the control’s constructor. Changing it to “public” fixes the problem. The reason for this behavior might be that the Designer’s own classes cannot see the constructor because they are not within the namespace of my class unless it is marked public. This all makes sense, and I will use the public keyword from now on.

    The class does not need to be in its own individual file or be the first declared class in the file as other answers suggested.

    The following class works well because the constructor’s keyword was changed to public.

    class CustomRichTextBox : RichTextBox
    {
        Timer tt = new Timer();
    
        public CustomRichTextBox()
        {
            tt.Tick += new EventHandler(tt_Tick);
            tt.Interval = 200;
        }
    
    
        protected override void OnTextChanged(EventArgs e)
        {
            tt.Stop();
            tt.Start();
        }
    
        void tt_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("Hello world!");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has happened before to me, but I can't remember how I fixed it.
How many times has this happened to you? You are typing a long block
This has happened to me a few times and I'd like to save time
This problem has happened to me twice.Both times during programming with Borland C++.when i
I have done this many times and have never seen this. I have an
I have read many codes on this but none happened to solve the problem.
This has happened to me in the past and I somehow fixed it now
This has happened to me a million times and I'm finally getting around to
This has happened a couple of times to me know. If I add to
This has happened for every QDialog I pop that fires from either a mouseReleaseEvent

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.