For Example
txtUnitTotalQty.Text = "";
txtPrice.Text = "";
txtUnitPrice.Text = "";
lblTotalvalue.Text = "";
To something like
(txtUnitTotalQty, txtPrice, txtUnitPrice, lblTotalvalue).Text = "";
You can do it like this:
txtUnitTotalQty.Text = txtPrice.Text = txtUnitPrice.Text = lblTotalvalue.Text = string.Empty;
Or you could write a method for it:
public void SetText(params TextBox[] controls, string text)
{
foreach(var ctrl in controls)
{
ctrl.Text = text;
}
}
Usage of this would be:
SetText(txtUnitTotalQty, txtPrice, txtUnitPrice, lblTotalvalue, string.Empty);
As .Text is a property of the common base class Control, you can iterate over a list:
new List<Control> { txtUnitTotalQty, txtPrice, txtUnitPrice, lblTotalvalue }.ForEach(c => c.Text = "");
You can do something like this:
foreach (var txt in new[] { txtUnitTotalQty, txtPrice, txtUnitPrice, lblTotalValue} )
{
txt.Text = "";
}
Another possible way to write the same thing with a smaller function is
void ClearAllText(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With