Results 1 to 3 of 3

Thread: [RESOLVED] Hard to find a control

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    17

    Resolved [RESOLVED] Hard to find a control

    I'm trying to find the values in textboxes, checkboxes and radiobuttons in a windows form so I'm trying to use this function
    VB Code:
    1. For Each control In Me.Controls
    2.     RichTextBox1.Text = RichTextBox1.Text & Chr(13) & control.Name
    3. Next

    But I have the following controls inside other controls and I can't find seem to be able to find them. For instance, the textbox (txtPtName) is inside a table layout panel (TableLayoutPanel1) and the TableLayoutPanel1 is inside a panel (Panel1)

    Panel1
    --- TableLayoutPanel1
    -------- txtPtName


    My question is: How can I search for every checkbox, radiobutton or a textbox without searching for them inside other controls? I really need this please.. thanks

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    17

    Re: Hard to find a control

    solved.. used this function

    Private Sub CheckControls(ByVal ctl As Control, controlnamestring As String)
    If ctl.Name = controlnameString then
    'do something
    Exit Sub
    End If
    If ctl.HasChildren Then
    For Each c As Control In ctl.Controls
    CheckControls(c, controlnamestring)
    Next
    End If
    End Sub

    Private Sub FindControl()

    CheckControls(Me, "TextBox1")

    End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Hard to find a control

    You don't need a recursive method. The GetNextControl method will follow the tab order, including nested controls:
    VB Code:
    1. Private Function GetControlByName(ByVal name As String) As Control
    2.         Dim ctl As Control = Me.GetNextControl(Me, True)
    3.  
    4.         While Not ctl Is Nothing
    5.             If ctl.Name = name Then
    6.                 Return ctl
    7.             End If
    8.  
    9.             ctl = Me.GetNextControl(ctl, True)
    10.         End While
    11.  
    12.         Return Nothing
    13.     End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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