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 7739877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:38:02+00:00 2026-06-01T08:38:02+00:00

I’m creating a simple notepad type of application with the tab functionality. I’m creating

  • 0

I’m creating a simple notepad type of application with the tab functionality. I’m creating the TabControl, its TabPages and RichTextBoxes at run-time. I have them instantiated at class scope. And there is a MenuStrip item called New, by clicking that you can add more tab pages.

TabControl tbcEditor = new TabControl();
TabPage tbPage = new TabPage();
RichTextBox rtb = new RichTextBox();

private void frmTextEditor_Load(object sender, EventArgs e)
{
     Controls.Add(tbcEditor);
     tbcEditor.Dock = DockStyle.Fill;
     tbcEditor.TabPages.Add(tbPage);
     tbPage.Controls.Add(rtb);
     rtb.Dock = DockStyle.Fill;
}

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
     //TabPage tbPage = new TabPage();
     //RichTextBox rtb = new RichTextBox();

     tbPage.Controls.Add(rtb);
     rtb.Dock = DockStyle.Fill;
     tbcEditor.TabPages.Add(tbPage);
}

The problem I’m facing is a bit difficult to explain. I’ll try my best. When the form loads, everything works as expected. The TabControl creates with a TabPage with a RichTextBox added. However if I click that New button to add another page, it goes bonkers. A new TabPage gets created but without a RichTextBox added. No errors are thrown either. If I un-comment out those 2 lines(under MenuItem click event), which creates 2 instances of TabPage and RichTextBox, everything works as I want.

Now my first question is why do I have to make new instances of only those 2 types(TabPage, RichTextBox) again but not TabControl? As you can see in the last line, I can use tbcEditor once again. But not tbPage and rtb.

Sure I can go on declaring them again at local scope but another issue arises then. If I want to say, add copy, paste functionality, I should do something like this,right?

Clipboard.SetDataObject(rtb.SelectedText);

But I can’t access rtb since it is declared as local.

I’m very baffled by this so any suggestions, ideas on how to overcome these 2 issues would be greatly appreciated.

Thank you.

  • 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-06-01T08:38:04+00:00Added an answer on June 1, 2026 at 8:38 am

    If I un-comment out those 2 lines(under MenuItem click event), which creates 2 instances of TabPage and RichTextBox, everything works as I want.

    When you uncomment those lines, you are adding the same instance of the rich textbox and tab page to the container panel again which is meaningless. Instead add new controls foreach tabpage. (I hope thats the requirement)

    Now my first question is why do I have to make new instances of only those 2 types(TabPage, RichTextBox) again but not TabControl?

    TabControl is the parent control which has TabPages as child controls. You can have multiple tabs under one TabControl. So you need not create TabControls other than the tbcEditor you have already added. We do not add container controls more than once (unless its the requirement). Do we need more forms? No, just one form which can hold all the child controls right. Similarly just one TabControl which can hold a collection of TabPages. You would need more TabControls only if you want sub-tabs foreach new tab which I guess is not the requirement..

    But I can't access rtb since it is declared as local.

    This is no big deal. You can do in two ways:

    1) Search for your appropriate control by looping. The SelectedTab property gives what you want.

     private void someEvent(object sender, EventArgs e)
     {           
            foreach (Control c in tbcEditor.SelectedTab.Controls)
            {
                if (c is RichTextBox)
                {
                    Clipboard.SetDataObject(((RichTextBox)c).SelectedText);
                    break; //assuming u have just one main rtb there
                }
            }
     }
    

    2) Tag each rtb to the tabPage when you create it, and then you can get the tag element of the selected tab page to get the rich text box. I would go for this approach.

    Edit: (In general pls make the following changes too to your code):

     TabControl tbcEditor = new TabControl();
    
     private void frmTextEditor_Load(object sender, EventArgs e)
     {
          Controls.Add(tbcEditor);
          tbcEditor.Dock = DockStyle.Fill;
          AddMyControlsOnNewTab();
     }
    
    private void AddMyControlsOnNewTab()
    {
        TabPage tbPage = new TabPage();
        RichTextBox rtb = new RichTextBox();
        tbPage.Tag = rtb; //just one extra bit of line.
    
        tbcEditor.TabPages.Add(tbPage);
        tbPage.Controls.Add(rtb);
        rtb.Dock = DockStyle.Fill;
    }
    
    private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        AddMyControlsOnNewTab();
    }
    

    Now, you can call it like this:

     private void someEvent(object sender, EventArgs e)
     {           
            RichTextBox rtb= (RichTextBox)tbcEditor.SelectedTab.Tag;
            Clipboard.SetDataObject(rtb.SelectedText);
            //or even better in just a line,
            //Clipboard.SetDataObject(((RichTextBox)tbcEditor.SelectedTab.Tag).SelectedText);
     }
    

    What you have to consider here is which is the control that you first get and which is the one you do not get. You would get TabPage anyways but not the RichTextBox. So you have to tag RichTextBox to TabPage. You have to cast it since Tag is of type object, so you have to specify which kind of object it is. Finally, this method has the advantage that you need not loop through a list, so its more performant. And that you can have more RichTextBoxes in the TabPage (provided you want to copy text from only one set of RichTextBoxes, one from each TabPage)..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

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.