Tab控件选择

发布于 2024-07-08 23:06:41 字数 35 浏览 6 评论 0原文

如何实现一些选项卡需要通过某些事件或某些按钮单击来关闭?

How to implement some of the tabs need to close by some events or some button click?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-07-15 23:06:41

您可以像这样从 TabControl 中删除选项卡:

tabControl1.TabPages.Remove(tabControl1.SelectedTab);

关闭多个选项卡时,您可能需要首先删除索引号较高的选项卡,因为弹出选项卡时选项卡页的索引会发生变化:

private void button1_Click(object sender, EventArgs e)
{
    // Close second and fourth tab
    if (tabControl1.TabPages.Count > 3)
    {
        // Work backwards when removing tabs
        tabControl1.TabPages.RemoveAt(3);
        tabControl1.TabPages.RemoveAt(1);
    }
}

如果关闭选项卡后再次需要选项卡,那么 Hide() 将没有帮助。 您应该在内存中存储每个选项卡的引用,并在以后添加或插入它们:

tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Add(tabPage1);
tabControl1.TabPages.Insert(0, tabPage1);

使用下面的示例,您可以保留关闭的选项卡集合,并稍后将它们推送到 TabControl。 最好创建一个小类,允许您保存位置和对选项卡的引用。此示例使用具有相同功能的泛型 List 和 Control.Tag。

private List<TabPage> tabsClosed = new List<TabPage>();

private void button1_Click(object sender, EventArgs e)
{
    // Close second and fourth tab
    if (tabControl1.TabCount > 3)
    {
        // Keep a reference to tabs in memory before closing them
        tabsClosed.Add(tabControl1.TabPages[1]);
        tabsClosed.Add(tabControl1.TabPages[3]);

        // Store the tabs position somewhere
        tabControl1.TabPages[1].Tag = 1;
        tabControl1.TabPages[3].Tag = 3;

        // Work backwards when removing tabs
        tabControl1.TabPages.RemoveAt(3);
        tabControl1.TabPages.RemoveAt(1);
    }
}

private void button2_Click(object sender, EventArgs e)
{
    foreach (TabPage tab in tabsClosed)
    {
        //tabControl1.Controls.Add(tab);
        tabControl1.TabPages.Insert((int)tab.Tag, tab);
    }
    tabsClosed.Clear();
}

You can remove a tab from TabControl like this:

tabControl1.TabPages.Remove(tabControl1.SelectedTab);

When closing multiple tabs you may want to remove the tabs with a higher index number first as the index of tab pages change when you pop a tab:

private void button1_Click(object sender, EventArgs e)
{
    // Close second and fourth tab
    if (tabControl1.TabPages.Count > 3)
    {
        // Work backwards when removing tabs
        tabControl1.TabPages.RemoveAt(3);
        tabControl1.TabPages.RemoveAt(1);
    }
}

If you need the tabs again after closing them, then Hide() will not be helpful. You should store a reference for each tab in memory and add or insert them later:

tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Add(tabPage1);
tabControl1.TabPages.Insert(0, tabPage1);

Using the example below you may keep a collection of tabs that you closed and push them to the TabControl later on. Preferably you will create a small class that allows you to save the position and reference to tabs in. This example uses a generics List and Control.Tag that does the same.

private List<TabPage> tabsClosed = new List<TabPage>();

private void button1_Click(object sender, EventArgs e)
{
    // Close second and fourth tab
    if (tabControl1.TabCount > 3)
    {
        // Keep a reference to tabs in memory before closing them
        tabsClosed.Add(tabControl1.TabPages[1]);
        tabsClosed.Add(tabControl1.TabPages[3]);

        // Store the tabs position somewhere
        tabControl1.TabPages[1].Tag = 1;
        tabControl1.TabPages[3].Tag = 3;

        // Work backwards when removing tabs
        tabControl1.TabPages.RemoveAt(3);
        tabControl1.TabPages.RemoveAt(1);
    }
}

private void button2_Click(object sender, EventArgs e)
{
    foreach (TabPage tab in tabsClosed)
    {
        //tabControl1.Controls.Add(tab);
        tabControl1.TabPages.Insert((int)tab.Tag, tab);
    }
    tabsClosed.Clear();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文