1. 在TabControl的Tabpages中打開一個新的Form:

     //添加新頁簽,并選中

     tabControl1.TabPages.Add(new TabPage("SQL Window"));
     tabControl1.SelectedIndex = tabControl1.TabCount - 1;

     //要在新頁簽中打開新的form
     MultiTablesQuery sqlform = new MultiTablesQuery();
     sqlform.TopLevel = false;

     //設置新form的標題欄為隱藏,設form屬性 FormBorderStyle =None 也可以實現
     sqlform.ControlBox = false;
     sqlform.Text = "";

     //在Tabpages中打開new form
     sqlform.Parent = tabControl1.TabPages[tabControl1.TabCount - 1];
     sqlform.Show();

2. 雙擊TabPages時關閉該頁:

            TabControl tabControl1 = (TabControl)sender;
            Point pt = new Point(e.X, e.Y);

            for (int i = 4; i < tabControl1.TabCount; i++)
            {
                Rectangle recTab = tabControl1.GetTabRect(i);
                if (recTab.Contains(pt))
                {
                    TabPage seltab = this.tabControl1.SelectedTab;
                    int seltabindex = this.tabControl1.SelectedIndex;

                    tabControl1.Controls.Remove(seltab);
                    tabControl1.SelectTab(seltabindex - 1);

                    return;
                }
            }

3. 在每個TabPages頁簽上添加一個關閉按鈕:

 

Sample Image

Introduction

I was writing an application that uses a TabControl. I needed to give my user the ability to close tab pages dynamically, so I was looking for a tab control that has a close sign on every page, but could not find one. So I started developing one. The result of this development is the following class: TabControlEx.

Using the code

To use the the tab control, I perform the following action:

  1. Add a regular TabControl to my form.
  2. Edit the Designer file (if your form name is Form1, you need to edit the Form1.Designer.cs file).

You need to change the type of the tab control from "System.Windows.Forms.TabControl" to "Yoramo.GuiLib.TabControlEx". You also need to change the instantiation statement (in the "InitializeComponent" method) to create the correct type of tab control.

The TabControlEx Code

TabControlEx derives from System.Windows.Forms.TabControl. It implements the OnDrawItem to draw the 'x' sign on the page (Close sign) and the OnMouseClick to figure out if the close sign has been clicked. In the constructor, I have changed the DrawMode to TabDrawMode.OwnerDrawFixed to activate the OnDrawItem. In order to enable the application to refuse closing a tab, or to provide an event to enable saving data, I have defined an event called PreRemoveTabPage.

 

 Collapse

 

using System;
using System.Windows.Forms;
using System.Drawing;
namespace Yoramo.GuiLib
{
public delegate bool PreRemoveTab(int indx);
public class TabControlEx : TabControl
{
public TabControlEx()
: base()
{
PreRemoveTabPage = null;
this.DrawMode = TabDrawMode.OwnerDrawFixed;
}
public PreRemoveTab PreRemoveTabPage;
protected override void OnDrawItem(DrawItemEventArgs e)
{
Rectangle r = e.Bounds;
r = GetTabRect(e.Index);
r.Offset(2, 2);
r.Width = 5;
r.Height = 5;
Brush b = new SolidBrush(Color.Black);
Pen p = new Pen(b);
e.Graphics.DrawLine(p, r.X, r.Y, r.X + r.Width, r.Y + r.Height);
e.Graphics.DrawLine(p, r.X + r.Width, r.Y, r.X, r.Y + r.Height);
string titel = this.TabPages[e.Index].Text;
Font f = this.Font;
e.Graphics.DrawString(titel, f, b, new PointF(r.X + 5, r.Y));
}
protected override void OnMouseClick(MouseEventArgs e)
{
Point p = e.Location;
for (int i = 0; i < TabCount; i++)
{
Rectangle r = GetTabRect(i);
r.Offset(2, 2);
r.Width = 5;
r.Height = 5;
if (r.Contains(p))
{
CloseTab(i);
}
}
}
private void CloseTab(int i)
{
if (PreRemoveTabPage != null)
{
bool closeIt = PreRemoveTabPage(i);
if (!closeIt)
return;
}
TabPages.Remove(TabPages[i]);
}
}
}
第三段的演示代碼:  https://files.cnblogs.com/fmxyw/ClosableTabs.zip
第三段的來源:http://www.codeproject.com/KB/tabs/closabletabcontrolpage.aspx
posted on 2008-09-10 17:51  fmxyw  阅读(744)  评论(0)    收藏  举报