TabControl AddingTab事件 [英] TabControl AddingTab event

查看:61
本文介绍了TabControl AddingTab事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabControl,我要防止在其中添加现有的TabPage(它们由名称标识),而是将SelectedTabPage设置为此精确选项卡.

I have a TabControl in which I want to prevent adding existing TabPage (they are identified by a name) and instead set the SelectedTabPage to this precise tab.

我想知道是否有一个事件在将页面添加到TabControl之前立即触发.如果不是,使用TabPages(列表)的事件CollectionChanged是正确的选择吗?

I wish to know if there are an event that triggers right before a page is being added to the TabControl. If not, would using the event CollectionChanged of the TabPages (list) be a correct alternative ?

推荐答案

尝试类似的方法,我正在检查TabControl页面集合中是否有与尝试添加的页面同名的页面.存在,我将焦点设置在现有实例上,否则将新页面添加到TabControl.看看这样的事情是否对您有用.

Try something like this, I am checking the TabControl page Collection for a page with the same name as the Page that is trying to be added, if it exists I am setting focus to the existing instance, otherwise adding the new page to the TabControl. See if something like this works for you.

private void button1_Click(object sender, EventArgs e)
{
    TabPage tp = new TabPage();
    tp.Name = tabPage1.Name;
    var temp =tabControl1.Controls.Find(tp.Name,true);
    if( temp.Length > 0)
    {
        tabControl1.SelectedTab = (TabPage) temp[0];
    }
    else
        tabControl1.Controls.Add(tp);
}


最有可能在添加控件后触发.


Anything having to do with the ControlCollection will most likely be triggered after the control has been added.

从上面的链接:

您可以通过将控件传递到Contains方法来确定控件是否是集合的成员.若要获取控件在集合中位置的索引值,请将控件传递到IndexOf方法.可以通过调用CopyTo方法将集合复制到数组中.

You can determine if a Control is a member of the collection by passing the control into the Contains method. To get the index value of the location of a Control in the collection, pass the control into the IndexOf method. The collection can be copied into an array by calling the CopyTo method.


如果您希望通过在TabControl中添加ExtensionMethod来清理代码,请检查现有页面,设置焦点或从此处添加.


If you want you could cleanup your code some by adding an ExtensionMethod to your TabControl Check for an existing page, set focus or add from there.

示例:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static bool AddPage(this TabControl tc, TabPage tp)
        {
            var matchedPages = tc.Controls.Find(tp.Name, false);
            if ( matchedPages.Length > 0)
            {
                tc.SelectedTab = (TabPage)matchedPages[0];
                return true;
            }
            else
            {
                tc.TabPages.Add(tp);
                tc.SelectedTab = tp;
                return false;
            }

        }
    }
}

用法:

tabControl1.AddPage(tp);

这篇关于TabControl AddingTab事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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