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.
- 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".
-
-
-
- public void UpdateWindowsList_WindowMenu()
- {
-
- TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
-
-
- int n = windowToolStripMenuItem.DropDownItems.Count;
-
-
- for (int i = n - 1; i >=2; i--)
- {
- windowToolStripMenuItem.DropDownItems.RemoveAt(i);
- }
-
-
- foreach (TabPage tabpage in tabcoll)
- {
-
- ToolStripMenuItem menuitem = new ToolStripMenuItem();
- String s = tabpage.Text;
- menuitem.Text = s;
-
- if (tabControl1.SelectedTab == tabpage)
- {
- menuitem.Checked = true;
- }
- else
- {
- menuitem.Checked = false;
- }
-
-
- windowToolStripMenuItem.DropDownItems.Add(menuitem);
-
-
- menuitem.Click += new System.EventHandler(WindowListEvent_Click);
- }
- }
-
- private void WindowListEvent_Click(object sender, EventArgs e)
- {
-
- ToolStripItem toolstripitem = (ToolStripItem)sender;
-
-
-
- TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
- foreach (TabPage tb in tabcoll)
- {
- if (toolstripitem.Text == tb.Text)
- {
- tabControl1.SelectedTab = tb;
-
- UpdateWindowsList_WindowMenu();
- }
- }
- }
Step 6: Perform operations of New, Open, Close, Close All, Exit, Close All Windows. The following are the actions defined separately.
New
- private void newToolStripMenuItem_Click(object sender, EventArgs e)
- {
- TabPage tabpage = new TabPage();
- tabpage.Text = "Untitled " + count;
-
- RichTextBox richtextbox = new RichTextBox();
- richtextbox.Dock = DockStyle.Fill;
- tabpage.Controls.Add(richtextbox);
-
- tabControl1.TabPages.Add(tabpage);
- tabControl1.SelectedTab = tabpage;
-
- count++;
-
-
- UpdateWindowsList_WindowMenu();
- }
Open
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- TabPage tabpage = new TabPage();
-
- RichTextBox richtextbox = new RichTextBox();
- richtextbox.Dock = DockStyle.Fill;
- tabpage.Controls.Add(richtextbox);
-
-
- OpenFileDialog openfilediag = new OpenFileDialog();
- openfilediag.Filter = "All files|*.*";
-
- if(openfilediag.ShowDialog()==DialogResult.OK)
- {
- richtextbox.Text = File.ReadAllText(openfilediag.FileName);
-
- String filename = openfilediag.FileName.Substring(openfilediag.FileName.LastIndexOf("\\") + 1);
-
- tabpage.Text = filename;
- tabControl1.TabPages.Add(tabpage);
- tabControl1.SelectedTab = tabpage;
- }
-
- UpdateWindowsList_WindowMenu();
- }
Close
- private void closeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if(tabControl1.TabCount>0)
- {
- tabControl1.TabPages.Remove(tabControl1.SelectedTab);
-
-
- UpdateWindowsList_WindowMenu();
- }
- }
Close All
- private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
- {
- tabControl1.TabPages.Clear();
- }
Exit
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
Close All Windows: Same as Close All,
Step 7: Now add SelectedIndexChanged event to tabControl1 and call UpdateWindowsList_WindowMenu() function at last in it.
- private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
- {
-
- UpdateWindowsList_WindowMenu();
- }
Step 8: Run your form and check it out.
Complete Code: