Works like a charm. Thanks Jacob. -----Original Message----- From: The DOTNET list will be retired 7/1/02 [mailto:[EMAIL PROTECTED]] On Behalf Of Jacob Grass Sent: Wednesday, June 05, 2002 4:50 PM To: [EMAIL PROTECTED] Subject: Re: [DOTNET] Locate WinForm control recursively
Dan- This works for me: Public Function FindControl(ByVal Parent As Control, ByVal Target As String) As Control Dim FoundIt As Control = Nothing Dim i As Integer = 0 Do Until Not FoundIt Is Nothing Or i > Parent.Controls.Count - 1 If Parent.Controls(i).Name = Target Then FoundIt = Parent.Controls(i) Else If Parent.Controls(i).Controls.Count > 0 Then FoundIt = FindControl(Parent.Controls(i), Target) End If i += 1 End If Loop Return FoundIt End Function Jacob A. Grass -----Original Message----- From: Dan Souk [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 05, 2002 4:35 PM To: [EMAIL PROTECTED] Subject: [DOTNET] Locate WinForm control recursively Hi folks, I need to locate a control within a WinForm that may be located a few layers deep into the container hierarchy. The answer seems to be recursion, since there is no FindControl method on the standard Windows Form class. Problem I'm having is that this code can't retrieve a control past the second layer. If the control is buried at level three, this code can find it but the return value is Nothing. Would really appreciate any help. Thanks. Function FindControl(ByVal Parent As System.Windows.Forms.Control, ByVal strCtlName As String) As System.Windows.Forms.Control Dim tmp As System.Windows.Forms.Control Dim blnFound As Boolean Try For Each tmp In Parent.Controls blnFound = (tmp.Name = strCtlName) If blnFound Then Exit For Else tmp = FindControl(tmp, strCtlName) End If Next Return tmp Catch ex As System.Exception DisplayExceptionInfo(ex) End Try End Function Form1 TabControl TabPage Grid1 -- FindControl gets to this, but can't pass it back ------------------------ Dan Souk Finna Technologies, Inc. 2410 Lindsay Ct West Chicago, IL 60185 +1-630-762-8257 phone +1-630-762-8258 fax http://www.finnatech.com You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.