C#中控制項的tag屬性
來源:互聯網
上載者:User
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屬性的使用,可以用來大量操作控制項,十分方便。