我如何在Winforms中关闭当前打开的标签页 [英] How i can close current open tabpage in winforms

查看:157
本文介绍了我如何在Winforms中关闭当前打开的标签页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在winforms中关闭tabcontrol中的当前(打开)选项卡....

解决方案

TabControl公开了一个"SelectedTab"属性,该属性将为您提供当前活动的TabPage.但是,TabPage本身并不公开``Remove方法(它确实公开了Dispose()方法,但我从未尝试使用此方法).

因此,您必须使用TabControl的TabPages集合公开的Remove方法,以及在删除TabControl时使用的方法...除非您一直对其进行引用:它不见了!

如您所知,您可以将TabPage的"Visible"属性设置为"false",但这无效.因此,要隐藏TabPage,必须将其删除!

以下是一些代码,这些代码将向您展示如何删除当前的TabPage,如何隐藏它,但保留对其的引用以及如何还原隐藏的最后一个TabPage:

1.假设您在WinForm上有一个带有三个按钮的TabControl,''tabControl1".一个隐藏当前选定的TabControl TabPage,并保存对其的引用;一个还原最后一个隐藏的TabPage;并且,一个永久删除TabPage.

2.假设在设计时将RestoreSavedTabButton的Enabled属性的状态设置为False. 私有 int savedTabPageIndex; 私有 无效 HideCurrentTabButton_Click(对象发​​件人,EventArgs e) { // 在这里变得偏执...如果您愿意,则抛出错误... 如果(tabControl1.TabPages.Count == 0 || tabControl1.SelectedTab == 返回; savedTabPage = tabControl1.SelectedTab; savedTabPageIndex = tabControl1.TabPages.IndexOf(savedTabPage); tabControl1.TabPages.Remove(savedTabPage); // 重新启用RestoreSavedTabButton // RestoreSavedTabButton.Enabled = true ; } 私有 无效 RestoreSavedTabButton_Click(对象发​​件人,EventArgs e) { // 在此抛出错误,因为这种情况永远不会发生? 如果(savedTabPage == )返回 ; tabControl1.TabPages.Insert(savedTabPageIndex,savedTabPage); // 防止不适当地重复使用RestoreSavedTabButton // RestoreSavedTabButton.Enabled = false ; } 私有 无效 RemoveCurrentTabButton_Click(对象发​​件人,EventArgs e) { // 在这里抛出错误?或禁用按钮,具体取决于? 如果(tabControl1.TabPages.Count == 0 )返回; tabControl1.TabPages.Remove(tabControl1.SelectedTab); }

讨论:这个简单的示例只能还原上一个隐藏的TabPage:如果要隐藏然后还原多个TabPage,则会出现其他问题,因为TabPages集合的索引顺序会发生变化.

...编辑#1 ...

进行细微调整,以适当地设置RestoreSavedTabButton的启用"状态

...结束编辑#1 ...


感谢BillWoodruff ....
还要感谢mjbohn ...


How i can close current (open) tabpage in tabcontrol in winforms....

解决方案

The TabControl exposes a ''SelectedTab property that will give you the current active TabPage. But, the TabPage does not expose a ''Remove method, itself (it does expose a Dispose() method, but I have never tried using this).

So you have to use the Remove method exposed by the TabPages collection of the TabControl, and when a TabControl is removed ... unless you''ve kept a reference to it: it is gone !

As you may know, you can set a TabPage''s ''Visible property to ''false, but that has no effect. So, to hide a TabPage, you must remove it !

Here''s some code that will show you how to remove the current TabPage, how to Hide it, but keep a reference to it, and how to restore the last TabPage hidden:

1. Assume you have a TabControl, ''tabControl1 on a WinForm with three buttons. One Hides the current selected TabControl TabPage, and saves a reference to it; one restores the last hidden TabPage; and, one Removes a TabPage permanently.

2. Assume you set the state of the RestoreSavedTabButton ''Enabled property to ''false at design-time.

private TabPage savedTabPage;

private int savedTabPageIndex;

private void HideCurrentTabButton_Click(object sender, EventArgs e)
{
    // get paranoid here ... throw an error if you wish ...
    if (tabControl1.TabPages.Count == 0 || tabControl1.SelectedTab == null) return;

    savedTabPage = tabControl1.SelectedTab;

    savedTabPageIndex = tabControl1.TabPages.IndexOf(savedTabPage);

    tabControl1.TabPages.Remove(savedTabPage);

    // re-enable the RestoreSavedTabButton
    //
    RestoreSavedTabButton.Enabled = true;
}

private void RestoreSavedTabButton_Click(object sender, EventArgs e)
{
    // throw an error here, since this should never happen ?
    if (savedTabPage == null) return;

    tabControl1.TabPages.Insert(savedTabPageIndex, savedTabPage);

    // prevent inappropriate re-use of the RestoreSavedTabButton
    //
    RestoreSavedTabButton.Enabled = false;
}

private void RemoveCurrentTabButton_Click(object sender, EventArgs e)
{
    // throw an error here ? Or disable the Button, depending ?
    if (tabControl1.TabPages.Count == 0) return;

    tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}

Discussion: this simple example only can restore the last TabPage hidden: if you wanted to hide, and then restore, multiple TabPages, other issues would arise, since the index order of the TabPages collection would be changing.

... edit #1 ...

minor tweaks to appropriately set the Enabled state of the RestoreSavedTabButton

... end edit #1 ...


Thanks BillWoodruff....
and also thanks to mjbohn...


这篇关于我如何在Winforms中关闭当前打开的标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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