Click here to Skip to main content
Licence 
First Posted 15 May 2006
Views 20,194
Bookmarked 15 times

Access all similar controls

By | 15 May 2006 | Article
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

About the Author

guptasulabh



India India

Member

The author is based in New Delhi. He is Microsoft Certified Professional(MCP) doing coding since 2002 in Microsoft platform/ Web applications. With the past experience of working in a GIS based company, currently he is working as a free lance coder and provides solutions for the real-time problems.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralLittle value Pinmemberjuggler0:59 16 May '06  
GeneralRe: Little value Pinmemberguptasulabh3:48 16 May '06  
GeneralRecursive PinmemberRobert Rohde20:47 15 May '06  
GeneralRe: Recursive Pinmemberguptasulabh2:57 16 May '06  
GeneralRe: Recursive PinmemberRobert Rohde5:52 16 May '06  
GeneralRe: Recursive Pinmembermicro_x21:53 6 Jul '06  
GeneralRe: Recursive PinmemberRobert Rohde7:23 7 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 16 May 2006
Article Copyright 2006 by guptasulabh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid