C# tutorials
13. Dynamic tab control
Keyboard navigation

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. Select Form1 (by clicking at the form at a point not covered by a button or the tab control).

16. Select KeyPreview in the Properties pane and change the value to True.

17. Click on the Event icon (the flash) at the top of the Properties pane.

18. Double-click on KeyDown and write or paste this code where the caret is:

try
{
  if (e.KeyCode == Keys.PageUp)
  {
    tabControl1.SelectedIndex = tabControl1.SelectedIndex + 1;
    e.Handled = true;
  }
  else if ((e.KeyCode == Keys.PageDown) && (tabControl1.SelectedIndex != 0))
  {
    tabControl1.SelectedIndex = tabControl1.SelectedIndex - 1;
    e.Handled = true;
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

19. Press F5 to start debugging the program.

20. Click several times on button1. Several tabs with rich textboxes should appear.

21. Press the Page Up key several times to move upwards in the tab order (until the last tab is reached).

22. Press the Page Down key several times to move downwards in the tab order (until the first tab is reached).

Comment on the first, second and third code snippet

See comments on C# tutorials: 12. Dynamic tab control with rich textbox.

Comments on the fourth code snippet

Line 3-7: If the Page Up key is pressed, move to the next tab in tabControl1.

Line 8-12: If the Page Down key is pressed and the first tab isn't selected, move to the previous tab in tabControl1.

Line 6 and line 11 aren't necessary for the program to work but good to include, because they silence an annoying bell sound each time the Page Up key or the Page Down key is pressed.


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.