|
-
Apr 7th, 2004, 11:16 AM
#1
Thread Starter
Junior Member
how to Find a control in a form.
Hi, I just tried to write a control search function tbut I'm not having much success.
I need it to be able to iterate through all the controls on a forum and all the container controls (group boxes, panels, etc) within and return a reference to the control I'm searching for
this is what I have so far. The problem is that Groupbox and Panel are not considered a ContainerControl so it just skips past that condition.
Public Function FindControl(ByRef Controls As System.Windows.Forms.Control.ControlCollection, ByVal strControlName As String) As Forms.Control
Dim objTemp As Forms.Control
Dim objReturnControl As Forms.Control
For Each objTemp In Controls
If TypeOf (objTemp) Is ContainerControl Then
objReturnControl = FindControl(objTemp.Controls, strControlName)
If Not objReturnControl Is Nothing Then
Return objReturnControl
End If
ElseIf objTemp.Name = strControlName Then
Return objTemp
End If
Next
Return Nothing
End Function
-
Apr 7th, 2004, 11:22 AM
#2
Frenzied Member
Here are some subs i wrote that get called from one another if needed. they clear text so you can go through the subs, and alter it to meet your needs.
basically, you just need seperate subs to scour the tabs and group boxes etc.
hope this helps!
VB Code:
#Region " Control Clearing Routines "
Public Sub ClearListBox(ByVal box As ListBox)
'Purpose : Clear a list box of all items
'Accepts : listbox control
'Notes :
box.Items.Clear()
End Sub
Public Sub ClearTabControl(ByVal tc As TabControl)
'Purpose : clears all tabpages on a tabpage control
'Accepts : control as tabcontrol
'Returns :
'Notes :
For Each ctrl As Control In tc.Controls
Try
If TypeOf ctrl Is TabPage Then
ClearTab(ctrl)
End If
Catch ex As Exception
End Try
Next
End Sub
Public Sub ClearTab(ByVal tab As TabPage)
'Purpose : Clears an entire tab control of data
'Accepts : tabpage control
'Notes : this sub calls on other subs to do the work
For Each ctrl As Control In tab.Controls
Try
'Clear all textboxes
If TypeOf ctrl Is TextBox Then
ClearTextBox(ctrl)
End If
'Next, ComboBoxes
If TypeOf ctrl Is ComboBox Then
ClearComboBox(ctrl)
End If
'Next, Groupboxes
If TypeOf ctrl Is GroupBox Then
ClearGroupBox(ctrl)
End If
'Next, other Tabs
If TypeOf ctrl Is TabPage Then
ClearTab(ctrl)
End If
'Next, Listboxes
If TypeOf ctrl Is ListBox Then
ClearListBox(ctrl)
End If
Catch
End Try
Next
End Sub
Public Sub ClearComboBox(ByVal Box As ComboBox)
'Purpose : Resets a combobox to the first item in the collection list
'Accepts : combox as argument
'Notes : This doesn't delete any info, it just resets the box
Try
Box.Text = Box.Items.Item(0)
Catch
End Try
End Sub
Public Sub ClearGroupBox(ByVal groupbox As GroupBox)
'Purpose : Clear all textboxes within a groupbox control
'Accepts : groupbox as argument
'Notes :
For Each ctrl As Control In groupbox.Controls
Try
If TypeOf ctrl Is TextBox Then
ClearTextBox(ctrl)
End If
If TypeOf ctrl Is ComboBox Then
ClearComboBox(ctrl)
End If
Catch
End Try
Next
End Sub
Public Sub ClearTextBox(ByVal box As TextBox)
box.Text = String.Empty
End Sub
#End Region
-
Apr 7th, 2004, 11:31 AM
#3
Thread Starter
Junior Member
sweet. thanks alot andy
actually I discovered that every control has a controls collection.
That means that if you want to write a recursive function you can simply check the count of the control's controls collection.
ie
For Each objTemp In Controls
If objTemp.controls.count > 0 Then
objReturnControl = FindControl(objTemp.Controls, strControlName)
....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|