本文介绍了对于每个文本框循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个 foreach 循环来检查面板中的每个 TextBox,如果它的 Text 什么都没有,则更改 BackColor.我尝试了以下方法:
I'm trying to make a foreach loop that checks every TextBox in a panel and changes BackColor if its Text is nothing. I've tried the following:
Dim c As TextBox
For Each c In Panel1.Controls
if c.Text = "" Then
c.BackColor = Color.LightYellow
End If
Next
但我收到错误:
无法将 System.Windows.Forms.Label 类型的对象转换为类型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
这篇关于对于每个文本框循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!