我正在尝试创建一个 foreach 循环来检查面板中的每个 TextBox,如果它的 Text 什么都没有,则更改 BackColor。我尝试了以下方法:
Dim c As TextBox
For Each c In Panel1.Controls
if c.Text = "" Then
c.BackColor = Color.LightYellow
End If
Next
但我收到错误:
Unable to cast object of type System.Windows.Forms.Label to type System.windows.forms.textbox
最佳答案
你可以试试这样的:
Dim ctrl As Control
For Each ctrl In Panel1.Controls
If (ctrl.GetType() Is GetType(TextBox)) Then
Dim txt As TextBox = CType(ctrl, TextBox)
txt.BackColor = Color.LightYellow
End If
关于vb.net - 对于每个文本框循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13504280/