Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Access all similar controls

0.00/5 (No votes)
15 May 2006 1  
This article helps in accessing all controls. For example clearing all textbox in the form

 

Introduction

This article helps in accessing all the controls on the form (both windows based as well as web based). Bellow code can be used to clear all textbox or reset all the dropdown list or this code can be modified as per the requirement.

Using the code

This article provides the idea of accessing all the controls on the form. This simple code snippet can be modified as per the requirement to maximize its utilization. In the for loop it will pick all the controls on the form. It will compare if the control is of textbox type then it till clear it, if the control if dropdownlist, it will reset it. Similarly any control can be matched any corresponding step can be taken.

 

Dim __textBoxType As Type = TextBox1.GetType
Dim __DropDown As Type= DropDownList1.GetType

For Each __control As Control In Me.Controls
   If __control.GetType() Is __textBoxType Then
       CType(__control, TextBox).Clear()
   End If 
   If __control.GetType() Is __DropDown Then 
        CType(__control, TextBox).SelectedItem=0 
   End If
Next

 

Above I have given two options. Above code will clear all of the textboxes as well as it will make selected index of all dropdownlist as 0. This code can be used to reset the data entry form where you may suppose have 15 textbox to clear. These are just small implementation. You can use this code depending upon your requirement. For example if you are creating few controls in runtime and you need to clear them. So instead of writing code for each and every control you can just loop it and do you work. In the for loop, it will pick each and every control on the form control array. In a case if we want to modify only one set of control in a panel, we can run the above loop for all controls in the panels only.

Points of Interest

By writing just few lines we can set any similar property for a similar kind of control. If will surly decrease the lines of code and also most importantly it will be optimized code

Conclusion

Hence we can say that above code can be used when you want do similar function for all similar kind of controls.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here