我有tabcontrol和几个标签页的表单,其中包含文本框和复选框中的许多设置 .
当用户从此表单按退出时,我必须检查数据是否已更改 .
为此,我想在表单上输入所有值的字符串,并将其与退出时所有值的字符串进行比较:
Private Function getsetupstring() As String
Dim setupstring As String = ""
For Each oControl As Control In Me.Controls
If TypeOf oControl Is CheckBox Then
Dim chk As CheckBox = CType(oControl, CheckBox)
setupstring &= chk.Checked.ToString
End If
If TypeOf oControl Is TextBox Then
setupstring &= oControl.Text.Trim.ToString
End If
Next
Return setupstring
End Function
但是,该代码不会遍历标签页上的控件,只有TabControl和几个位于表单顶部的按钮 .
怎么做才能列出所有控件,以便我可以选择值?
1 回答
Controls
仅包含父控件,而不包含相应的子控件 . 如果您想获得所有控件(父项和相应的子项),您可以依赖以下代码:其中
getAllControls
的定义如下:您拥有的其他替代方法是依赖
Controls.Find
方法,并将searchAllChildren
属性设置为True
.