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
Ameerh24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.