tabcontrol是C#windows窗体程序中的一个控件——选项卡控件
我学到这里时有两个问题困扰了我好久:1.怎么将tabcontrol传到各个窗体,然后就TabPage加入到tabcontrol。2.mainMainTab(tabcontrol的名字).SelectedIndex = MainMain.i(MainMain是我的主窗口,i是int型的数);怎么写。
1.一开始我将tabcontrol以静态方法传递,但报错了,因为tabcontrol是非静态变量。我最后通过构造函数传递给下一个窗口
project_main p_M = new project_main(ref this.mainMainTab);//我的下个窗口
//添加导航栏
TabPage Page = new TabPage();
Page.Name = "项目管理";
Page.Text = "项目管理";
p_M.FormBorderStyle = FormBorderStyle.None; //隐藏子窗体边框(去除最小花,最大化,关闭等按钮)
p_M.TopLevel = false; //设置为非顶级控件(很重要,否则加不进去)
Page.Controls.Add(p_M);
mainMainTab.Visible = true;
//加入tabpage中
mainMainTab.TabPages.Add(Page);
mainMainTab.SelectedIndex = MainMain.i;//跳到此页面
MainMain.i++;
p_M.Show();
//下图是project_main 页面需要写的。
结果图:
2.完成上面后,点开一个新的窗口不会跳转过去,所以tabcontrol.SelectedIndex = i(i是int型变量)就是有必要的了。一般装有tabcontrol的窗体为0,也为主页面面,一般担当导航页。
我设i为导航页(主页面)的全局变量:
public static int i = 1;
如果有其它窗口打开,调动i,且i++
mainMainTab.SelectedIndex = MainMain.i;//跳到此页面
MainMain.i++;//我的主页面是MainMain
如果窗口关闭,i--
//绘制“X”号即关闭按钮
private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);
//先添加TabPage属性
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text
, this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);
//再画一个矩形框
using (Pen p = new Pen(Color.White))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
}
//填充矩形框
Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
}
//画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
//"\"线
Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
e.Graphics.DrawLine(objpen, p1, p2);
//"/"线
Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
e.Graphics.DrawLine(objpen, p3, p4);
}
e.Graphics.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//关闭按钮功能
private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
//如果鼠标在区域内就关闭选项卡
bool isClose = x > myTabRect.X && x < myTabRect.Right
&& y > myTabRect.Y && y < myTabRect.Bottom;
if (isClose == true)
{
this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
MainMain.i--;
}
}
}
//双击选项卡关闭页面
private void MainTabControl_MouseDoubleClick(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
private void MainMain_Load(object sender, EventArgs e)
{
//清空控件
//this.MainTabControl.TabPages.Clear();
//绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE / 2);
this.tabControl1.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
this.tabControl1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDoubleClick);
}
补充: public int CLOSE_SIZE = 12;
关闭i--中代码是我借鉴网上的