简体   繁体   English

Windows窗体面板中是否存在如何查找标签

[英]how to find a label is exist or not in Windows Forms panel

if (panel1.Contains(label1)) // if label1 is exist it shows label is exist if label2 is not exist then mean else part... how to identify it is not exist.
        {
            MessageBox.Show("Label 1 is Exist");
        }

mean to say my else part is not working if the label is not exist. 意思是说如果标签不存在我的其他部分不起作用。

Just loop the container like this: 只需像这样循环容器:

foreach(Control ctrl in panel1.Controls)
{
    // Check if control is of type label
    if(ctrl.GetType() == typeof(Label))
    {
        // check the name of the label
        if(ctrl.Name == "label1")
        {
            // do what ever you want
            MessageBox.Show("Label 1 existing");
        }
    }
}

You could also skip the type-check part and straight go for the name: 您也可以跳过类型检查部分并直接输入名称:

foreach(Control ctrl in panel1.Controls)
{
    if(ctrl.Name == "label1")
    {
        // check ctrl.Name
    }
}

Note: This is just looping direct controls. 注意:这只是循环直接控制。 If there is a container inside panel1 you wont get its controls. 如果panel1有一个容器,你就不会得到它的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM