How to check controls created at runtime in a windows form application?
-
Hi Here is the code that I've written. Private Function CheckControl(ByVal ctrlName As String) As Boolean Dim ctrl As Control Dim blnResult As Boolean For Each ctrl In Me.Controls If ctrl.Name = ctrlName Then blnResult = True End If Next Return blnResult End Function The problem is that it does not give the correct answer i.e. even if the control exists, it returns false. Thanks
reman
-
Hi Here is the code that I've written. Private Function CheckControl(ByVal ctrlName As String) As Boolean Dim ctrl As Control Dim blnResult As Boolean For Each ctrl In Me.Controls If ctrl.Name = ctrlName Then blnResult = True End If Next Return blnResult End Function The problem is that it does not give the correct answer i.e. even if the control exists, it returns false. Thanks
reman
Remember a control (panel, groupbox etc) can have a collection of controls, you are only checking the top level of controls on the form. You need to check if a control has controls and call the same procedure from within your foreach loop. Example
For Each ctrl In Me.Controls If ctrl.Name = ctrlName Then blnResult = True End If CheckControl(ctrl) Next
Never underestimate the power of human stupidity RAH
-
Hi Here is the code that I've written. Private Function CheckControl(ByVal ctrlName As String) As Boolean Dim ctrl As Control Dim blnResult As Boolean For Each ctrl In Me.Controls If ctrl.Name = ctrlName Then blnResult = True End If Next Return blnResult End Function The problem is that it does not give the correct answer i.e. even if the control exists, it returns false. Thanks
reman
Hi Amer Rehman; If you are using .Net Framework 2.0 or higher you can use the Control.ControlCollection.Find Method to find a control. The second parameter states to search all child controls as well as shown in the code snippet below.
Private Function CheckControl(ByVal ctrlName As String) As Boolean
Dim blnResult As Boolean = False If (Me.Controls.Find(ctrlName, True).Length > 0) Then blnResult = True End If Return blnResult
End Function
Fernando