如何添加和删除“自定义"C# 中的选项卡

时间:2023-03-29
本文介绍了如何添加和删除“自定义"C# 中的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在制作一个需要添加或删除选项卡(选项卡控件)的应用程序.我已经很好地完成了标签的添加和删除,但是我有自定义按钮而不是使用标签.(此按钮点击后会跳转到第一个标签页):

I am making an application that requires tabs (tab-control) to be added or removed. I have done the add and remove for tabs fine, but I have custom buttons instead of using the tabs. (This button will go the the first tab page when clicked):

    //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; 的新按钮,例如.

However, how will I create it so when you click the "Add Tab" button it creates a new tab but it also creates a new button with tabControl1.SelectedIndex = 1; in it for example.

编辑

我这样做是为了添加一个新选项卡(到 tabControl1):

I have done this to add a new tab (to 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; 每次添加新标签时都会上升?- 谢谢

This adds a new tab on top of the existing ones fine. But I how do I do it so that it also creates a new button with the properties of the button above but makes it so instead of tabControl1.SelectedIndex = 1; it does tabControl1.SelectedIndex = 3; and goes up every time I add a new tab? - Thanks

推荐答案

经过几次更新,这里是我的答案的新版本.它告诉你如何

After a few updates here is a new version of my answer. It shows you how to

  • 添加一个 Button,它会添加一个新的 TabPages 并选择它
  • 使 Tab 控件在每个选项卡的右侧绘制结束x"字符,就像 Firefox 或 Visual Studio 那样.
  • Add a Button which adds a new TabPages and selects it
  • Make the Tab control draw closing 'x' characters to the right of each tab, like Firefox or Visual Studio do it.

使添加 Button 像你想要的那样,也许是 Text="+" 和标签的高度一样的高度.它会自动定位在选项卡上.如果您的 Tab 被移动或调整大小,您可能需要重新定位它.

Make the add Button like you want it, maybe the Text="+" and the height like the tabs' height. It is automatically positioned on the tab. You may need to reposition it, if your Tab is moved or resized.

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

In the Form_Load event the Tab is set to OwnerDrawFixed and each page's text is append by a few blanks to make room for the closing x. The code creates a class variable closeX to hold the current x-rectangle and tests it in the MouseClick event.

确保连接 tabControl1_DrawItemtabControl1_MouseClick 事件!

Make sure to wire up the tabControl1_DrawItem and tabControl1_MouseClick events!

    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,使用图像加载它,如果关闭按钮是第一个图像,请像这样更改代码:

If you want to use an Image to adorn the tabs, include an ImageList with your form, load it with the Image(s) and if the close button is the 1st image, change the code like this:

    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 截图

I append a screenshot of the Tab with a few pages

还有一个使用这个 关闭按钮图像:

And one using this close button image:

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

Update: Note that if your TabPage contains IDisposable objects you should make sure they all get disposed before you remove the page! See here for example code!

这篇关于如何添加和删除“自定义"C# 中的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:修改 RichTextBox 中的默认选项卡大小 下一篇:如何创建没有选项卡标题的 TabControl?

相关文章