C#中控件的tag属性

tag属性在很多控件的属性中都会有,但是如何使用呢?tag的使用有何意义呢?


tag本身是“标签”的意思,顾名思义,就是给控件打上标签。
当项目中有很多类型名称各不相同的控件时,可以将这些控件打上相同的标签,即,将控件的tag值设置为同一个值,如,hide、TLB等等,然后用一段代码,进行相应的操作,如下:


for (int i = 0; i < toolStripMain.Items.Count; i++)
            {
                if (toolStripMain.Items[i].Tag == null) 
continue;
                if (toolStripMain.Items[i].Tag.ToString() != "TLB") 
continue;
                Found = false;
                if (arr != null)
                {
                    for (int j = 0; j < arr.Length; j++)
                    {
                        if (arr[j] == null) continue;
                        if (arr[j].BtName.ToUpper() == toolStripMain.Items[i].Name.ToUpper().ToUpper())
                        {
                            toolStripMain.Items[i].Enabled = arr[j].Enabled;
                            toolStripMain.Items[i].Visible = arr[j].Visuable;
                            toolStripMain.Items[i].ToolTipText = arr[j].BtDescription;
                            Found = true;
                            HasButtonShowAtLeastOnece = true;
                            break;
                        }
                    }
                }
                if (!Found) toolStripMain.Items[i].Visible = false;
            }




标签通常用来隐藏一些控件,使其在需要的时候显示。
tag属性的使用,可以用来批量操作控件,十分方便。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章