如何唯一标识选项卡控件

本文关键字:标识 选项 控件 唯一 何唯一 | 更新日期: 2023-09-27 17:59:19

当满足特定条件时,我计划做这样的事情,并且下面的代码将运行多次。

TabPage newtabcontrol = new TabPage();
tabControl1.Controls.Add(newtabcontrol);

但是我怎么知道我刚刚添加的最新选项卡的选项卡索引呢?

如何唯一标识选项卡控件

您可以为TabPage指定一个唯一的名称

例如

TabPage newtabcontrol = new TabPage();
newtabcontrol.Name = "ID-1";
tabControl1.Controls.Add(newtabcontrol);

要找到tabPage,可以使用

var tabPage = tabControl1.TabPages["ID-1"]
if (tabPage != null)
{
    // perform action
}

对于添加的最后一个选项卡页面:

TabPage newtabcontrol = new TabPage();
tabControl1.Controls.Add(newtabcontrol);
TabPage temp = tabControl1.TabPages[tabControl1.TabCount - 1];