如何添加和删除"自定义"标签在C# [英] How to add and remove "custom" tabs in C#

查看:228
本文介绍了如何添加和删除"自定义"标签在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


  • 添加按钮增加了一个新的的TabPages 并选中它

  • 请在设置页控制绘制关闭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.

请附加按钮像你想的那样,也许文本=+和喜欢的标签高度的高度。它是自动定位标签。您可能需要重新定位,如果你的设置页被移动或调整大小。

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 事件设置页设置为 OwnerDrawFixed 和每个网页上的文本被追加一些空白,使空间闭幕的X.该代码创建一个类变量 closeX 来保持当前的X矩形,并测试其在鼠标点击事件。

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_DrawItem tabControl1_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您的形式,图像(S)加载它,如果关闭按钮是第一形象,改变这样的代码:

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 );
    }



我添加了标签的截图用了几页

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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆