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屬性的使用,可以用來批量操作控件,十分方便。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章