我想創(chuàng)建一個條件來檢查是否有任何標(biāo)簽包含“abc”一詞,如果包含,它會阻止按鈕。我有 label1、label2、label3 等形式。我嘗試讓主標(biāo)簽設(shè)置檢查包含,但沒有這樣的事情。定義后:Label slabel = new Label();我正在嘗試檢查“if”,但總是返回 null 錯誤。if(slabel.Contains("abc"))請幫忙 !
1 回答

一只甜甜圈
TA貢獻1836條經(jīng)驗 獲得超5個贊
試試這個:
對于Windows Forms:
bool found = false;
foreach (Control c in this.Controls)
{
if (c is Label && c.Text.Contains("abc"))
{
found = true;
break;
}
}
button1.Enabled = !found;
對于WPF:
bool found = false;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(this.Content as DependencyObject); i++)
{
var v = VisualTreeHelper.GetChild(this.Content as DependencyObject, i);
if (v is Label && (v as Label).Content.ToString().Contains("abc"))
{
found = true;
break;
}
}
button.IsEnabled = !found;
- 1 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消