如何添加和删除" custom" C#中的标签

时间:2014-10-11 13:08:26

标签: c# tabs tabcontrol

我正在创建一个需要添加或删除标签(制表符控件)的应用程序。我已经完成了标签的添加和删除,但我有自定义按钮而不是使用标签。 (单击此按钮将转到第一个标签页):

    //This will make it go to TAB 1
    private void button1_Click(object sender, EventArgs e)
    {
        tabControl1.SelectedIndex = 0; 
    }

   //This will change the MOUSEENTER to the correct image
    private void button1_MouseEnter(object sender, EventArgs e)
    {
        button1.MouseEnter += new EventHandler(button1_MouseEnter);
        this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Tab_Down));
    }
    //This will change the MOUSELEAVE to the correct image
    private void button1_MouseLeave(object sender, EventArgs e)
    {
        button1.MouseLeave += new EventHandler(button1_MouseLeave);
        this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Tab_Norm));
    }

但是,我将如何创建它,以便当您单击“添加标签”按钮时,它会创建一个新标签,但它也会创建一个新的按钮,其中包含tabControl1.SelectedIndex = 1;

修改

我这样做是为了添加一个新标签(到tabControl1):

 private void button2_Click(object sender, EventArgs e)
    {
        string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
        TabPage myTabPage = new TabPage(title);
        tabControl1.TabPages.Add(myTabPage);
    }

这会在现有的选项卡上添加一个新选项卡。但我如何做到这一点,以便它还创建一个新按钮,其中包含上面按钮的属性,但是这样做而不是tabControl1.SelectedIndex = 1;tabControl1.SelectedIndex = 3;并且每次添加新标签时都会上升? - 谢谢

1 个答案:

答案 0 :(得分:3)

经过一些更新后,这是我的答案的新版本。它告诉你如何

  • 添加Button,添加新TabPages并选择
  • Tab控件平局关闭' x'每个标签右侧的字符,如Firefox或Visual Studio都可以。

按照你想要的方式添加Button,也许是Text =" +"和高度一样的标签'高度。它自动定位在选项卡上。如果移动Tab或调整其大小,则可能需要重新定位。

Form_Load活动中,Tab设置为OwnerDrawFixed,并且每个页面的文字都会附加几个空格,以便为结束x腾出空间。代码创建一个类变量closeX来保存当前的x矩形并在MouseClick事件中对其进行测试。

确保连接tabControl1_DrawItemtabControl1_MouseClick事件!

    private void cb_addPage_Click(object sender, EventArgs e)
    {
        string title = "TabPage " + (tabControl1.TabCount + 1).ToString() + "   ";
        TabPage myTabPage = new TabPage(title);
        tabControl1.TabPages.Add(myTabPage);
        tabControl1.SelectedTab = myTabPage;
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
        cb_addPage.Top = tabControl1.Top;
        cb_addPage.Left = tabControl1.Right - cb_addPage.Width;
        foreach (TabPage tp in tabControl1.TabPages) tp.Text += "   ";
    }


    Rectangle closeX = Rectangle.Empty;

    private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
    {
        Size xSize = new Size(13,13);
        TabPage tp = tabControl3.TabPages[e.Index];
        e.DrawBackground();
        using (SolidBrush brush = new SolidBrush(e.ForeColor)  )
               e.Graphics.DrawString(tp.Text+ "   ", e.Font,  brush, 
                                     e.Bounds.X + 3, e.Bounds.Y + 4 );

        if (e.State == DrawItemState.Selected)
        {
           closeX = new Rectangle(
               e.Bounds.Right - xSize.Width, e.Bounds.Top, xSize.Width, xSize.Height);
           using (SolidBrush brush = new SolidBrush(Color.LightGray))
                e.Graphics.DrawString("x", e.Font, brush, 
                                       e.Bounds.Right - xSize.Width, e.Bounds.Y + 4);
        }

    }

    private void tabControl1_MouseClick(object sender, MouseEventArgs e)
    {
        if (closeX.Contains(e.Location))
            tabControl1.TabPages.Remove(tabControl1.SelectedTab);
    }

如果您想使用图片来装饰标签,请在表格中加入ImageList,使用图片加载图片,如果关闭按钮是第1张图片,请更改以下代码:

    if (e.State == DrawItemState.Selected)
    {
        closeX = new Rectangle(e.Bounds.Right - xSize.Width - 3, 
                               e.Bounds.Top + 5, xSize.Width, xSize.Height);
        e.Graphics.DrawImage(imageList1.Images[0], closeX, 
                             new Rectangle(0,0,16,16), GraphicsUnit.Pixel );
    }

我附上了几页的Tab截图

owner drawn tab with closing button

使用此enter image description here关闭按钮图片的人:

enter image description here

更新:请注意,如果您的TabPage包含IDisposable个对象,则应确保在删除页面之前将它们全部处理掉!有关示例代码,请参阅here