Awa*_*eem 2 c# if-statement visual-studio winforms
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");
}
Run Code Online (Sandbox Code Playgroud)
意思是说如果标签不存在,我的其他部分就不起作用。
只需像这样循环容器:
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以跳过类型检查部分并直接输入名称:
foreach(Control ctrl in panel1.Controls)
{
if(ctrl.Name == "label1")
{
// check ctrl.Name
}
}
Run Code Online (Sandbox Code Playgroud)
注意:这只是循环直接控制。如果里面有一个容器,panel1你将无法控制它。