Batch update of controls on Winforms
private void clearTextBoxes()
{
//Declare type
Type type = typeof(TextBox);
//Loop into controls on form
foreach (Control ctrl in this.Controls)
{
//check if type of control is TextBox
if (ctrl.GetType() == type)
{
ctrl.Text = "";
}
}
}
The method above loops into all controls of the form and sets the Text-properties of all TextBoxes to an empty string.
Pff, no big deal really ... but this can be quite powerful (and handy!) if you are able to organize your control-types in some kind of categories so they are subject for a batch update! You can check on the type of control, the name (or substring of the name) of a control, ...
Another example where databinding for textboxes is done in batch may give you more an idea of what I mean :
private void setDataBindingForTextBoxes()
{
//Declare type
Type type = typeof(TextBox);
//Loop into controls on form
foreach (Control ctrl in this.Controls)
{
//check if type of control is TextBox
if (ctrl.GetType() == type)
{
//Add DataBinding assuming that name of TextBox is the same as ColumName
ctrl.DataBindings.Add("Text", this.myDataSet.Names, ctrl.Name);
}
}
}
These simple procedures also do a lot to the readability of your code and are easy to maintain. I'm sure that this "method" can be used in many more straightforward situations.
0 Comments:
Post a Comment
<< Home