vb.net panel里的控件如何遍历
我直接遍历画面中的textbox没问题,可是画面中panel中的textbox就遍历不到'功能遍历所有comboboxtextbox,并清空值DimctAsControl...
我直接遍历画面中的textbox没问题,可是画面中panel中的textbox就遍历不到
'功能 遍历 所有 combobox textbox,并清空值
Dim ct As Control
For Each ct In Me.Controls
If (TypeOf ct Is TextBox) Then
ct.Text = ""
ElseIf (TypeOf ct Is ComboBox) Then
Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
cb.SelectedIndex = -1
End If
Next 展开
'功能 遍历 所有 combobox textbox,并清空值
Dim ct As Control
For Each ct In Me.Controls
If (TypeOf ct Is TextBox) Then
ct.Text = ""
ElseIf (TypeOf ct Is ComboBox) Then
Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
cb.SelectedIndex = -1
End If
Next 展开
2个回答
展开全部
Public Sub ReSetText(Control ctrl)
Dim ct As Control
For Each ct In ctrl.Controls
Try
For Each ct2 As Control In ct.Controls
ReSetText(ct2)
Next
Catch
End Try
If (TypeOf ct Is TextBox) Then
ct.Text = ""
ElseIf (TypeOf ct Is ComboBox) Then
Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
cb.SelectedIndex = -1
End If
Next
End Sub
因为textbox在窗体里的panel里,你只遍历窗体的控件是不够的。
追问
Public Sub ReSetText(Control ctrl) 程序有问题 ,能定义一个直接写在我代码里的吗
追答
抱歉最近一般用c#把语法弄混了,解决这个问题要靠递归,所以写成一个方法比较好。
Public Sub ReSetText( ctrl As Control)
Dim ct As Control
For Each ct In ctrl.Controls
Try
For Each ct2 As Control In ct.Controls
ReSetText(ct2)
Next
Catch
End Try
If (TypeOf ct Is TextBox) Then
ct.Text = ""
ElseIf (TypeOf ct Is ComboBox) Then
Dim cb As System.Windows.Forms.ComboBox = DirectCast(ct, System.Windows.Forms.ComboBox)
cb.SelectedIndex = -1
End If
Next
End Sub
用的时候就直接是
ReSetText(Me)
2015-04-01
展开全部
容器不同 你用for each 找panel里的控件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询