Introduction

Look in any editor,you can see Window menu that shows list of tabs.(see above image)
So that it is so much important in any editor to manage list of tabs.it is also used to change a tab easily without finding a tab in tabcontrol or in any tab selector. Just go to Window menu and select tab/window which you want to edit. It is so much simple to create windows list in C#.

So how to create Tabs Windows List

Create a windows list function that change in menu items by changing tabs. If you create a new tab then just call windows list function to modify drop down menu items of Window menu. Whenever tabs are changed in tabControl then just call windows list function. If you add new tab/remove tab then just call windows list function.

How it Works

Firstly, you have to remove all drop down menu items of Window menu. Then read all tabs from tabControl and add it to TabControl.TabCollection.
Now read each tabpage in TabCollection using foreach() statement. In foreach statement, create new menuitem using class ToolStripMenuItem and set text to each menuitem as each tabpage text. Now add each menuitem to Window menu. Finally, add click event handler to each menuitem. Create menuitem click events function. In this function, read clicked menuitem text and find tabpage in tabControl having text of clicked menuitem.

Start

Step 1 : Create new Windows Forms Application in C#.

Step 2
: Drag & Drop MenuStrip onto Form and add the following components to MenuStrip(menuStrip1),
File
New
Open
Close
Close All
Exit
Window
Close All Windows
---separator------
Step 3: Drag & Drop TabControl onto Form and remove all tabpages of TabControl(tabControl1).

Step 4: Declare "static int count" variable globally.
  1. public static int count = 1;
Step 5:

Add the following windows list function to your Form1 code. Whenever you will do changes in tabs then just call the following function to apply all changes to Window drop down menu items. I used function name "UpdateWindowsList_WindowMenu".
  1. //*************************************************************************************
  2. // Update windows list to Window menu
  3. //*************************************************************************************
  4. public void UpdateWindowsList_WindowMenu()
  5. {
  6. //get all tab pages from tabControl1
  7. TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
  8. //get windowToolStripMenuItem drop down menu items count
  9. int n = windowToolStripMenuItem.DropDownItems.Count;
  10. //remove all menu items from of windowToolStripMenuItem
  11. for (int i = n - 1; i >=2; i--)
  12. {
  13. windowToolStripMenuItem.DropDownItems.RemoveAt(i);
  14. }
  15. //read each tabpage from tabcoll and add each tabpage text to windowToolStripMenuItem
  16. foreach (TabPage tabpage in tabcoll)
  17. {
  18. //create Toolstripmenuitem
  19. ToolStripMenuItem menuitem = new ToolStripMenuItem();
  20. String s = tabpage.Text;
  21. menuitem.Text = s;
  22. if (tabControl1.SelectedTab == tabpage)
  23. {
  24. menuitem.Checked = true;
  25. }
  26. else
  27. {
  28. menuitem.Checked = false;
  29. }
  30. //add menuitem to windowToolStripMenuItem
  31. windowToolStripMenuItem.DropDownItems.Add(menuitem);
  32. //add click events to each added menuitem
  33. menuitem.Click += new System.EventHandler(WindowListEvent_Click);
  34. }
  35. }
  36. private void WindowListEvent_Click(object sender, EventArgs e)
  37. {
  38. //casting ToolStripMenuItem to ToolStripItem
  39. ToolStripItem toolstripitem = (ToolStripItem)sender;
  40. //create collection of tabs of tabContro1
  41. //check every tab text is equal to clicked menuitem then select the tab
  42. TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
  43. foreach (TabPage tb in tabcoll)
  44. {
  45. if (toolstripitem.Text == tb.Text)
  46. {
  47. tabControl1.SelectedTab = tb;
  48. //call UpdateWindowsList_WindowMenu() to perform changes on menuitems
  49. UpdateWindowsList_WindowMenu();
  50. }
  51. }
  52. }
Step 6: Perform operations of New, Open, Close, Close All, Exit, Close All Windows. The following are the actions defined separately.

New
  1. private void newToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3. TabPage tabpage = new TabPage();
  4. tabpage.Text = "Untitled " + count;
  5. //add richtextbox to tabpage
  6. RichTextBox richtextbox = new RichTextBox();
  7. richtextbox.Dock = DockStyle.Fill;
  8. tabpage.Controls.Add(richtextbox);
  9. tabControl1.TabPages.Add(tabpage);
  10. tabControl1.SelectedTab = tabpage;
  11. count++;
  12. //call windows list function
  13. UpdateWindowsList_WindowMenu();
  14. }
Open
  1. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3. TabPage tabpage = new TabPage();
  4. //add richtextbox to tabpage
  5. RichTextBox richtextbox = new RichTextBox();
  6. richtextbox.Dock = DockStyle.Fill;
  7. tabpage.Controls.Add(richtextbox);
  8. //read file using OpenFileDialog
  9. OpenFileDialog openfilediag = new OpenFileDialog();
  10. openfilediag.Filter = "All files|*.*";
  11. if(openfilediag.ShowDialog()==DialogResult.OK)
  12. {
  13. richtextbox.Text = File.ReadAllText(openfilediag.FileName);
  14. String filename = openfilediag.FileName.Substring(openfilediag.FileName.LastIndexOf("\\") + 1);
  15. tabpage.Text = filename;
  16. tabControl1.TabPages.Add(tabpage);
  17. tabControl1.SelectedTab = tabpage;
  18. }
  19. //call windows list function
  20. UpdateWindowsList_WindowMenu();
  21. }
Close
  1. private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3. if(tabControl1.TabCount>0)
  4. {
  5. tabControl1.TabPages.Remove(tabControl1.SelectedTab);
  6. //call windows list function
  7. UpdateWindowsList_WindowMenu();
  8. }
  9. }
Close All
  1. private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3. tabControl1.TabPages.Clear();
  4. }
Exit
  1. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3. Application.Exit();
  4. }
Close All Windows: Same as Close All,

Step 7
: Now add SelectedIndexChanged event to tabControl1 and call UpdateWindowsList_WindowMenu() function at last in it.
  1. private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. //call windows list function
  4. UpdateWindowsList_WindowMenu();
  5. }
Step 8: Run your form and check it out.
Complete Code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace WindowsListOfTabs
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16. //*************************************************************************************
  17. // Update windows list to Window menu
  18. //*************************************************************************************
  19. public void UpdateWindowsList_WindowMenu()
  20. {
  21. //get all tab pages from tabControl1
  22. TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
  23. //get windowToolStripMenuItem drop down menu items count
  24. int n = windowToolStripMenuItem.DropDownItems.Count;
  25. //remove all menu items from of windowToolStripMenuItem
  26. for (int i = n - 1; i >=2; i--)
  27. {
  28. windowToolStripMenuItem.DropDownItems.RemoveAt(i);
  29. }
  30. //read each tabpage from tabcoll and add each tabpage text to windowToolStripMenuItem
  31. foreach (TabPage tabpage in tabcoll)
  32. {
  33. //create Toolstripmenuitem
  34. ToolStripMenuItem menuitem = new ToolStripMenuItem();
  35. String s = tabpage.Text;
  36. menuitem.Text = s;
  37. if (tabControl1.SelectedTab == tabpage)
  38. {
  39. menuitem.Checked = true;
  40. }
  41. else
  42. {
  43. menuitem.Checked = false;
  44. }
  45. //add menuitem to windowToolStripMenuItem
  46. windowToolStripMenuItem.DropDownItems.Add(menuitem);
  47. //add click events to each added menuitem
  48. menuitem.Click += new System.EventHandler(WindowListEvent_Click);
  49. }
  50. }
  51. private void WindowListEvent_Click(object sender, EventArgs e)
  52. {
  53. //casting ToolStripMenuItem to ToolStripItem
  54. ToolStripItem toolstripitem = (ToolStripItem)sender;
  55. //create collection of tabs of tabContro1
  56. //check every tab text is equal to clicked menuitem then select the tab
  57. TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
  58. foreach (TabPage tb in tabcoll)
  59. {
  60. if (toolstripitem.Text == tb.Text)
  61. {
  62. tabControl1.SelectedTab = tb;
  63. //call UpdateWindowsList_WindowMenu() to perform changes on menuitems
  64. UpdateWindowsList_WindowMenu();
  65. }
  66. }
  67. }
  68. public static int count = 1;
  69. //Form1 Load
  70. private void Form1_Load(object sender, EventArgs e)
  71. {
  72. UpdateWindowsList_WindowMenu();
  73. }
  74. // File -> New
  75. private void newToolStripMenuItem_Click(object sender, EventArgs e)
  76. {
  77. TabPage tabpage = new TabPage();
  78. tabpage.Text = "Untitled " + count;
  79. //add richtextbox to tabpage
  80. RichTextBox richtextbox = new RichTextBox();
  81. richtextbox.Dock = DockStyle.Fill;
  82. tabpage.Controls.Add(richtextbox);
  83. tabControl1.TabPages.Add(tabpage);
  84. tabControl1.SelectedTab = tabpage;
  85. count++;
  86. //call windows list function
  87. UpdateWindowsList_WindowMenu();
  88. }
  89. // File -> Open
  90. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  91. {
  92. TabPage tabpage = new TabPage();
  93. //add richtextbox to tabpage
  94. RichTextBox richtextbox = new RichTextBox();
  95. richtextbox.Dock = DockStyle.Fill;
  96. tabpage.Controls.Add(richtextbox);
  97. //read file using OpenFileDialog
  98. OpenFileDialog openfilediag = new OpenFileDialog();
  99. openfilediag.Filter = "All files|*.*";
  100. if(openfilediag.ShowDialog()==DialogResult.OK)
  101. {
  102. richtextbox.Text = File.ReadAllText(openfilediag.FileName);
  103. String filename = openfilediag.FileName.Substring(openfilediag.FileName.LastIndexOf("\\") + 1);
  104. tabpage.Text = filename;
  105. tabControl1.TabPages.Add(tabpage);
  106. tabControl1.SelectedTab = tabpage;
  107. }
  108. //call windows list function
  109. UpdateWindowsList_WindowMenu();
  110. }
  111. // File -> Close
  112. private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  113. {
  114. if(tabControl1.TabCount>0)
  115. {
  116. tabControl1.TabPages.Remove(tabControl1.SelectedTab);
  117. //call windows list function
  118. UpdateWindowsList_WindowMenu();
  119. }
  120. }
  121. // File -> Close All
  122. private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
  123. {
  124. tabControl1.TabPages.Clear();
  125. }
  126. // File -> Exit
  127. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  128. {
  129. Application.Exit();
  130. }
  131. // tabControl1 Selected Index Changed
  132. private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
  133. {
  134. //call windows list function
  135. UpdateWindowsList_WindowMenu();
  136. }
  137. // Window -> Close All Windows
  138. private void closeAllWindowsToolStripMenuItem_Click(object sender, EventArgs e)
  139. {
  140. tabControl1.TabPages.Clear();
  141. }
  142. }
  143. }