以编程方式更改 Tab 键顺序

发布于 2024-08-27 12:13:20 字数 114 浏览 12 评论 0原文

如何以编程方式对 TabControl 中的选项卡重新排序?我需要根据某些条件对选项卡进行排序。

如果可以通过设计器进行重新排序,我想我们也必须能够在运行时通过代码来完成。

How do I programmatically reorder the tabs in a TabControl? I need to sort the tabs depending on some conditions.

If it's possible to do the reordering through the designer, i guess we must be able to do it through code at runtime too.

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

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

发布评论

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

评论(7

2024-09-03 12:13:20
  1. 创建一个新表单。
  2. 创建一个新的 TabControl。
  3. 请注意,它有两个 TabPage 控件,TabPage1 是第一个选项卡。
  4. 在表单的Load事件中,添加
    • this.TabControl1.TabPages.Remove(this.TabPage2)
    • this.TabControl1.TabPages.Insert(0, this.TabPage2)
  5. 运行表单。
  6. 请注意 TabPage2 现在是第一个选项卡。

请注意,如果您无法删除标签页,它仍会显示在原来的位置。换句话说,您将在同一选项卡页上拥有两个选项卡。

  1. Create a new Form.
  2. Create a new TabControl.
  3. Notice it has two TabPage controls, and TabPage1 is the first tab.
  4. In form's Load event, add
    • this.TabControl1.TabPages.Remove(this.TabPage2)
    • this.TabControl1.TabPages.Insert(0, this.TabPage2)
  5. Run the form.
  6. Notice TabPage2 is now the first tab.

Note that if you fail to remove the tab page, it will still show at its old location. In other words, you will have two tabs of the same tab page.

半仙 2024-09-03 12:13:20

您必须重新定义标签页集合,才能更改标签页的索引。

You have to redefine your tab page collection, in order to change the index of your tab pages.

情定在深秋 2024-09-03 12:13:20

下面的代码行也可以做到这一点,这种解决方案也适用于其他没有直接排序方式的数据:
- 转换为列表
- 对列表进行排序
- 把它放回去

public static void Sort(TabControl tabControl)
{
    var tabList = tabControl.TabPages.Cast<TabPage>().ToList();
    tabList.Sort(new TabPageComparer());
    tabControl.TabPages.Clear();
    tabControl.TabPages.AddRange(tabList.ToArray());
}

public class TabPageComparer : IComparer<TabPage>
{
    public int Compare(TabPage x, TabPage y)
    {
        return string.Compare(x.Text, y.Text);
    }
}

The following lines of code can also do the trick, this kind of solution also works for other kind of data that has no direct way of sorting:
- convert to a list
- sort the list
- put it back

public static void Sort(TabControl tabControl)
{
    var tabList = tabControl.TabPages.Cast<TabPage>().ToList();
    tabList.Sort(new TabPageComparer());
    tabControl.TabPages.Clear();
    tabControl.TabPages.AddRange(tabList.ToArray());
}

public class TabPageComparer : IComparer<TabPage>
{
    public int Compare(TabPage x, TabPage y)
    {
        return string.Compare(x.Text, y.Text);
    }
}
一生独一 2024-09-03 12:13:20

失去的是对的。下面是一个快速示例代码。

我有一个带有 2 个选项卡的选项卡控件(tabpage1、tabpag2)

然后我声明两个选项卡页并将选项卡控件中的现有选项卡存储在其中。

abPage tbp1 = new TabPage();
TabPage tbp2 = new TabPage();

tbp1 = tabControl1.TabPages[0];
tbp2 = tabControl1.TabPages[1];

然后单击按钮,

tabControl1.TabPages.Remove(tabControl1.TabPages[0]);

如果您想更改顺序,我使用“现在”删除了选项卡,那么您必须按该顺序将其添加到选项卡

//Order changed    
tabControl1.TabPages.Add(tbp2);
tabControl1.TabPages.Add(tbp1);

注意:这是未经测试的快速代码。

thelost is right. Below is a quick sample code.

I have a tab control with 2 tabs (tabpage1, tabpag2)

Then I declare two tabpages and store the existing tabs in the tabcontrol in it.

abPage tbp1 = new TabPage();
TabPage tbp2 = new TabPage();

tbp1 = tabControl1.TabPages[0];
tbp2 = tabControl1.TabPages[1];

Then on a button click I removed the tabs using

tabControl1.TabPages.Remove(tabControl1.TabPages[0]);

Now if you want to change the order then you will have top add it to the tab in that order

//Order changed    
tabControl1.TabPages.Add(tbp2);
tabControl1.TabPages.Add(tbp1);

Note: This is untested quick code.

醉城メ夜风 2024-09-03 12:13:20

进入 Designer.cs 文件

,您会发现

/// [Your TabControl Name]
yourTabControl.Controls.Add(yourPage1);
yourTabControl.Controls.Add(yourPage2);
yourTabControl.Controls.Add(yourPage3);

添加顺序是选项卡控件中选项卡页的顺序。按照您的意愿更改顺序。 TabControl.Controls 的删除和添加功能将帮助您,正如 Shoban 所回答的那样。

Go inside Designer.cs file

There you will find

/// [Your TabControl Name]
yourTabControl.Controls.Add(yourPage1);
yourTabControl.Controls.Add(yourPage2);
yourTabControl.Controls.Add(yourPage3);

The adding order is your tabpages' order in the tabcontrol. Change the order how you wish. Remove and Add functions of TabControl.Controls will help you as Shoban answered.

沫离伤花 2024-09-03 12:13:20

在 InitilizaComponent() 之后尝试这个。此代码将让您可以自由地在 .cs 文件中以编程方式更改它。

        this.tabReceive.Controls.Remove(this.metroTabPage4);
        this.tabReceive.Controls.Remove(this.metroTabPage5);

        this.tabReceive.Controls.Add(this.metroTabPage4);
        this.tabReceive.Controls.Add(this.metroTabPage5);

try this after Initilizacomponent(). this code will give you freedom to change it programmatically in .cs file.

        this.tabReceive.Controls.Remove(this.metroTabPage4);
        this.tabReceive.Controls.Remove(this.metroTabPage5);

        this.tabReceive.Controls.Add(this.metroTabPage4);
        this.tabReceive.Controls.Add(this.metroTabPage5);
我爱人 2024-09-03 12:13:20

有时我有带有多个选项卡页面的选项卡控件。在运行时,选项卡页面变得不可见(通过删除它们)并稍后再次添加。

此后,选项卡页面的顺序可能会错误。我使用此代码再次对它们重新排序:

public void ReorderTabPages()
{
    // Demo code to reorder tabControl with tabPages
    // where some tabPages may be unwanted at the moment

    // caution: events like "SelectedIndexChanged" does fire!

    // List of wanted tab pages
    List<TabPage> wantedTabPages = new List<TabPage>();

    // remember the current selected tab
    TabPage currentTabPage = this.tabControl.SelectedTab;

    // check if all possibly active tabs are currently visible
    // check it in the order they should be displayed
    // after that they are in the correct order in "wantedTabPages"
    if (this.tabControl.TabPages.IndexOf(this.tabPage_01) >= 0)
        wantedTabPages.Add(this.tabPage_01);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_02) >= 0)
        wantedTabPages.Add(this.tabPage_02);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_03) >= 0)
        wantedTabPages.Add(this.tabPage_03);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_04) >= 0)
        wantedTabPages.Add(this.tabPage_04);

    this.tabControl.SuspendLayout();

    // remove all currently visible tab pages
    for (int i = this.tabControl.TabPages.Count - 1; i >= 0; i--)
        this.tabControl.TabPages.RemoveAt(i);

    // add the tabPages in the correct order
    foreach (var wantedPage in wantedTabPages)
        this.tabControl.TabPages.Add(wantedPage);

    // restore the currently selected tabPage
    this.tabControl.SelectedTab = currentTabPage;

    this.tabControl.ResumeLayout();
}

Sometimes I have tabControls with several tabPages. At runtime tabPages are made invisible (by removing them) an added later again.

After this the tabPages may be in wrong order. I use this code to redorder them again:

public void ReorderTabPages()
{
    // Demo code to reorder tabControl with tabPages
    // where some tabPages may be unwanted at the moment

    // caution: events like "SelectedIndexChanged" does fire!

    // List of wanted tab pages
    List<TabPage> wantedTabPages = new List<TabPage>();

    // remember the current selected tab
    TabPage currentTabPage = this.tabControl.SelectedTab;

    // check if all possibly active tabs are currently visible
    // check it in the order they should be displayed
    // after that they are in the correct order in "wantedTabPages"
    if (this.tabControl.TabPages.IndexOf(this.tabPage_01) >= 0)
        wantedTabPages.Add(this.tabPage_01);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_02) >= 0)
        wantedTabPages.Add(this.tabPage_02);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_03) >= 0)
        wantedTabPages.Add(this.tabPage_03);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_04) >= 0)
        wantedTabPages.Add(this.tabPage_04);

    this.tabControl.SuspendLayout();

    // remove all currently visible tab pages
    for (int i = this.tabControl.TabPages.Count - 1; i >= 0; i--)
        this.tabControl.TabPages.RemoveAt(i);

    // add the tabPages in the correct order
    foreach (var wantedPage in wantedTabPages)
        this.tabControl.TabPages.Add(wantedPage);

    // restore the currently selected tabPage
    this.tabControl.SelectedTab = currentTabPage;

    this.tabControl.ResumeLayout();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文