Social Icons

Tuesday, 9 July 2013

how to find particular type or all children controls in a container control or in a WPF Window from code behind file

Finding Children Controls on a container contorl from code behind file:


In my previous articles i explained various Oops Concepts like partial classes,sealed classes,abstract classes and static classes and also in WPF and Asp.Net.

               Now we will see how to identify particular type of children controls on a container control either it is in WPF or Windows Forms or Asp.Net.


      Sometimes while programming we have situation like our form contains tens of textboxes or textblocks which need to be cleared for some button click event.For this situation manually writing for code i.e clearing each textbox value by specifying TextBox1.Clear() this means we have to write lot of code for this process.

         we can automate this process by identifying children controls of type and then clearing them. For this thing i am explaining using windows forms and wpf examples. We will use GetType() method of Container Controls in this process.

Finding TextBoxes in a Panel Control and Clearing them Automatically in Windows Forms with Insurance Form Example:

  Take a windows form and place a panel container control in it. Add labels, TextBoxes and Three buttons which looks like below.
Insurance Form
Insurance Form
So our function here is to Clear all textboxes when clicking Clear button. For clearing we will loop through all the children controls of Panel container Control and Checks each control whether it is textbox or not.
If it is TextBox then Convert it to textbox type and Clear that textbox using Clear() method.

            In this process we will use GetType() method of each Child Control. So in Clear Button Click event write below code

In the above code i am looping through all the controls on the panel using foreach loop and checking type using GetType() method if find a match then clearing that textbox.

     This procedure same for even in WPF Window or Asp.Net WebForm. This type of Procedure help full to you not only for clearing but also changing color control for doing validation and some sort of visibility changing setting (Hiding controls without mentioning their name). Now see how we can done this in WPF Window .

Finding TextBoxes in Grid Container Control in WPF Window and Changing Background Color of TextBox using Insurance Form Example:

Take a Window in WPF Application By default Grid will be there and name to Grid1 then add labels, textboxes and buttons which looks like below.

XAML Code for Above Window:

  So our requirement here is to change the textboxes background color when we click the clear button.In clear button Click event write below code.

If You observer the above code with windows forms example code you will find difference that in WPF Container Controls have Children property, where as forms Containers have Controls Property. In WPF every Control is a Container Control.

   This type of finding children control of particular type will help us in various situation like visibility settings, WPF Animations, moving controls, Applying Drag and Drop features and in Validating situations.


I hope this example on how to find children of type controls in Container controls in WPF or Windows Forms will help to you. If you like this post why can't you share it for your friends or else you have any other requirement write it below.Thank You.

No comments:

Post a Comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.