C# tutorials
12. Dynamic tab control with rich textbox

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.


To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag three buttons to the top of Form 1.

6. Drag a tab control to Form 1. Resize the tab control if you like.

7. Right-click on tabControl1, and click on Properties.

8. Make sure the name in the Properties pane is tabControl1 and not tabPage1.

9. Select TabPages in the Properties pane and click on the button with three dots to the right of (Collection).

10. Click on remove twice and then on OK (to remove tabPage1 and tabPage2).

11. Select Visible in the Properties pane and change the value to False (so that the empty tab control is hidden at first when the program is started).

12. Click on button1 and write or paste this code where the caret is:

try
{
  if (tabControl1.Visible == false)
  {
    tabControl1.Visible = true;
  }
  TabPage tp = new TabPage();
  int tc = (tabControl1.TabCount + 1);
  tp.Text = "New " + tc.ToString();
  tabControl1.TabPages.Add(tp);
  RichTextBox rtb = new RichTextBox();
  rtb.Dock = DockStyle.Fill;
  tp.Controls.Add(rtb);
  return;
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

13. Click on button2 and write or paste this code where the caret is:

try
{
  if (tabControl1.Visible == false)
  {
    return;
  }
  else
  {
    tabControl1.TabPages.Remove(tabControl1.SelectedTab);
    if (tabControl1.TabCount == 0)
    {
      tabControl1.Visible = false;
    }
    return;
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

14. Click on button3 and write or paste this code where the caret is:

try
{
  if (tabControl1.TabCount == 0)
  {
    MessageBox.Show("Click on button1 to create a new tab.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
  }
  else
  {
    int selectedTab = tabControl1.SelectedIndex;
    Control ctrl = tabControl1.Controls[selectedTab].Controls[0];
    RichTextBox rtb = ctrl as RichTextBox;
    rtb.Text = "TEST";
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

15. Press F5 to start debugging the program.

16. Click on button1. A tab with the text New 1 and a rich textbox should appear.

17. Click on button1 once more. A tab with the text New 2 and a rich textbox should appear.

18. Click on button 3. The text TEST should appear in the rich textbox in the selected tab.

19. Click on button2. The selected tab should disappear.

20. Click on button2 once more. The remaining tab should disappear, and so should the container tabControl1.

Comments on the first code snippet

Line 3-6: If no tab page have been created and the tab control is still hidden, make the tab control visible.

Line 7: Create the new tab page tp.

Line 8-9: Count the number of tab pages in tabControl1 (starting with 0), and set the text for the new tab page tp to New plus the number of tab pages. You can change the text to whatever you like. If you want tab pages without text, you can remove line 8-9.

Line 10: Add the tab page tp to tabControl1.

Line 11: Create the new rich textbox rtb.

Line 12: Set the dock style to fill (to make the rich textbox fill the entire tab page tp). There are several other settings you can add here, for example:

rtb.AcceptsTab = true;
rtb.BackColor = Color.FromArgb(255, 255, 0);
rtb.DetectUrls = false;
rtb.Font = new Font("Arial", 10);
rtb.ForeColor = Color.FromArgb(0, 0, 255);

Line 13: Add the rich textbox rtb to the tab page tp.

Comments on the second code snippet

Line 3-6: If there are no tab pages, do nothing.

Line 9: If there are at least one tab page, remove the selected tab page.

Line 10-13: If the last tab page has been removed, make tabControl1 invisible.

Comments on the third code snippet

Line 3-6: If no tab page have been created, display a message.

Line 9: Get the index number (starting from 0) of the selected tab.

Line 10: Create the control ctrl. tabControl1 is the tab control you dragged to the form earlier. Controls[selectedTab] is the selected tab. Controls[0] is the first control in the selected tab, in this case a rich textbox.

Line 11: Convert the control ctrl to the rich textbox rtb with the C# keyword as (to make it possible to use C# methods like Cut, Copy and Paste, that aren't available if this conversion aren't made).

Line 12: Set the text of the rich textbox in the selected tab to TEST. Or add some more useful code, for example one of these lines:

rtb.BackColor = Color.FromArgb(191, 255, 255);
rtb.Clear();
rtb.Copy();
rtb.Cut();
rtb.Focus();
rtb.Font = new Font("Courier New", 12);
rtb.ForeColor = Color.FromArgb(255, 0, 0);
rtb.Hide();
rtb.Paste();
rtb.ReadOnly = true;
rtb.Text = rtb.TextLength.ToString();
rtb.Visible = false;
rtb.ZoomFactor = 2;


Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.