如何检查表格上的任何标签是否包含“abc”一词?

我想创建一个条件来检查是否有任何标签包含“abc”一词,如果包含,它会阻止按钮。我有 label1、label2、label3 等形式。

我尝试让主标签设置检查包含,但没有这样的事情。

定义后:

Label slabel = new Label();

我正在尝试检查“if”,但总是返回 null 错误。

if(slabel.Contains("abc"))

请帮忙 !


猛跑小猪
浏览 153回答 1
1回答

一只甜甜圈

试试这个:对于Windows Forms:bool found = false;foreach (Control c in this.Controls){&nbsp; &nbsp; if (c is Label && c.Text.Contains("abc"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}button1.Enabled = !found;对于WPF:bool found = false;for (int i = 0; i < VisualTreeHelper.GetChildrenCount(this.Content as DependencyObject); i++){&nbsp; &nbsp; var v = VisualTreeHelper.GetChild(this.Content as DependencyObject, i);&nbsp; &nbsp; if (v is Label && (v as Label).Content.ToString().Contains("abc"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}button.IsEnabled = !found;
打开App,查看更多内容
随时随地看视频慕课网APP