• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 921
  • Last Modified:

get the type of control

how can I get the type of control ?
what I was trying to find so function that return the type of control so I can get all textbox controls in a form
note that I am using vs.net 2008
0
Ameerh24
Asked:
Ameerh24
  • 2
  • 2
  • 2
  • +3
4 Solutions
 
p_davisCommented:
foreach(Control ctrl in this.Controls)
{
       Type type = ctrl.GetType();
}
0
 
PaulHewsCommented:
For Each ctl As Control In Me.Controls
    If TypeOf ctl Is TextBox Then
        Dim txt As TextBox = DirectCast(ctl, TextBox)
        Debug.WriteLine(txt.Text)  'Now you can use the textbox properties.
    End If
Next
0
 
p_davisCommented:
for processing

if(ctrl.GetType() == typeof(TextBox))
{}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dampseyCommented:
you can also directly check if it is textbox or not by using typeof operator

            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                {
                }
            }

0
 
ee_rleeCommented:
you can also use this

        Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeOf ctl Is TextBox Then
            End If
        Next
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The solutions above will only find TextBoxes at the "first" level...those that are contained directly by the Form.  It would not find those contained by a Panel for instance.

To get ALL the TextBoxes you would need a RECURSIVE algorithm:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FindAllTextBoxes(Me)
    End Sub
 
    Private Sub FindAllTextBoxes(ByVal container As Control)
        For Each c As Control In container.Controls
            If TypeOf c Is TextBox Then
                Debug.Print(c.Name)
            ElseIf c.Controls.Count > 0 Then
                FindAllTextBoxes(c)
            End If
        Next
    End Sub

Open in new window

0
 
Ameerh24Author Commented:
Idle_Mind, why are u making recursion ?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As I said before:

    "The solutions above will only find TextBoxes at the "first" level...those that are contained directly by the Form.  It would not find those contained by a Panel for instance."

Specificly, the previous solutions are using:

    For Each ctl As Control In Me.Controls

Assuming the "Me" (or "this" for C#) refers to a Form, this would only iterate over the controls that are DIRECTLY contained by the Form.  If you placed a GroupBox, Panel, or some other container on the form, and then placed TextBoxes INSIDE that container, those TextBoxes would not be found by the For...Next looping structure.

Thus the need for recursion.  For each container control it encounters, the FindAllTextBoxes() method is called again so that the child controls for that container can also be inspected.  The recursion will "drill down" thru the control hierarchy...
0
 
Ameerh24Author Commented:
thanks Idle_Mind, that's really easy, clear and accurate
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now