I have several textboxes for inputs and then several开发者_如何学Python more for results. Is there any shortcut to clear all the entries on form?
for example aTextBox.Clear();
If all the text boxes are direct children of your form, you can use LINQ:
yourForm.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());
You will need to loop on all form controls and check if type is textbox, then .Text = string.empty
var cntrlCollections = GetAll(this,typeof(TextBox));
foreach (Control ctrl in cntrlCollections)
{
if (ctrl is TextBox)
{
ctrl.Text = " ";
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl=>GetAll(ctrl,type)).Concat(controls).Where
(c=>c.GetType() ==type);
}
Call this function like above in a click event to clear all textboxes. Hope this will helpful for you.
Commenting Frederic - you need to use ActiveForm yourform.ActiveForm.Controls. ...
.
Beacuse yourform.Controls. ...
drops an error:
An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Controls.get
Apply to VS2012Express
精彩评论