Creating Windows List Of Tabs In C#

 

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


Similar Articles