Results 1 to 3 of 3

Thread: how to Find a control in a form.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    22

    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

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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:
    1. #Region " Control Clearing Routines "
    2.  
    3.     Public Sub ClearListBox(ByVal box As ListBox)
    4.         'Purpose    :   Clear a list box of all items
    5.         'Accepts    :   listbox control
    6.         'Notes      :  
    7.         box.Items.Clear()
    8.     End Sub
    9.  
    10.     Public Sub ClearTabControl(ByVal tc As TabControl)
    11.         'Purpose    :   clears all tabpages on a tabpage control
    12.         'Accepts    :   control as tabcontrol
    13.         'Returns    :  
    14.         'Notes      :  
    15.  
    16.         For Each ctrl As Control In tc.Controls
    17.             Try
    18.                 If TypeOf ctrl Is TabPage Then
    19.                     ClearTab(ctrl)
    20.                 End If
    21.             Catch ex As Exception
    22.  
    23.             End Try
    24.         Next
    25.  
    26.     End Sub
    27.  
    28.     Public Sub ClearTab(ByVal tab As TabPage)
    29.         'Purpose    :   Clears an entire tab control of data
    30.         'Accepts    :   tabpage control
    31.         'Notes      :   this sub calls on other subs to do the work
    32.  
    33.         For Each ctrl As Control In tab.Controls
    34.  
    35.             Try
    36.                 'Clear all textboxes
    37.                 If TypeOf ctrl Is TextBox Then
    38.                     ClearTextBox(ctrl)
    39.                 End If
    40.  
    41.                 'Next, ComboBoxes
    42.                 If TypeOf ctrl Is ComboBox Then
    43.                     ClearComboBox(ctrl)
    44.                 End If
    45.  
    46.                 'Next, Groupboxes
    47.                 If TypeOf ctrl Is GroupBox Then
    48.                     ClearGroupBox(ctrl)
    49.                 End If
    50.  
    51.                 'Next, other Tabs
    52.                 If TypeOf ctrl Is TabPage Then
    53.                     ClearTab(ctrl)
    54.                 End If
    55.  
    56.                 'Next, Listboxes
    57.                 If TypeOf ctrl Is ListBox Then
    58.                     ClearListBox(ctrl)
    59.                 End If
    60.             Catch
    61.             End Try
    62.         Next
    63.  
    64.     End Sub
    65.  
    66.     Public Sub ClearComboBox(ByVal Box As ComboBox)
    67.         'Purpose    :   Resets a combobox to the first item in the collection list
    68.         'Accepts    :   combox as argument
    69.         'Notes      :   This doesn't delete any info, it just resets the box
    70.  
    71.         Try
    72.             Box.Text = Box.Items.Item(0)
    73.  
    74.         Catch
    75.  
    76.         End Try
    77.  
    78.     End Sub
    79.  
    80.     Public Sub ClearGroupBox(ByVal groupbox As GroupBox)
    81.         'Purpose    :   Clear all textboxes within a groupbox control
    82.         'Accepts    :   groupbox as argument
    83.         'Notes      :  
    84.  
    85.  
    86.         For Each ctrl As Control In groupbox.Controls
    87.  
    88.             Try
    89.                 If TypeOf ctrl Is TextBox Then
    90.  
    91.                     ClearTextBox(ctrl)
    92.  
    93.                 End If
    94.  
    95.                 If TypeOf ctrl Is ComboBox Then
    96.  
    97.                     ClearComboBox(ctrl)
    98.  
    99.                 End If
    100.  
    101.             Catch
    102.  
    103.             End Try
    104.  
    105.         Next
    106.  
    107.  
    108.     End Sub
    109.  
    110.     Public Sub ClearTextBox(ByVal box As TextBox)
    111.         box.Text = String.Empty
    112.     End Sub
    113.  
    114.  
    115. #End Region

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    22
    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
  •  



Click Here to Expand Forum to Full Width