第1个回答 2015-09-18
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb() As TextBox = {tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8, tb9, tb10, _
tb11, tb12, tb13, tb14, tb15, tb16, tb17, tb18, tb19, tb20}
For i = 0 To tb.Length - 1
If tb(i).Text <> "" Then tb(i).Enabled = False
Next
End Sub
第2个回答 2015-09-18
按下按钮后,依次检查20个文本框,代码如下
Private Sub Button1_Click(sender As Object, _
e As EventArgs) Handles Button1.Click
' 检查TextBox1 是否包含 a
If TextBox1.Text.Contains("a") Then
TextBox1.Enabled = False
Else
TextBox1.Enabled = True
End If
' 检查TextBox2 是否包含……
……
End Sub
追问这样查下去岂不是很麻烦……
追答也可以考虑遍历所有TextBox
For Each t As Control In Me.Controls
'如果控件是TextBox
If TypeOf t Is TextBox Then
With CType(t, TextBox)
'如果Text包含 "a"
If .Text.Contains("a") Then
.Enabled = False
End If
End With
End If
Next
从上面代码看出,遍历控件虽然代码简化很的多,但是没办法对不同的TextBox进行特定的检查了。
毕竟,有20个文本框需要判断,编程工作确实不小(力气活)
本回答被提问者和网友采纳