Suppose I don’t want to use
if (string.IsNullOrEmpty(textbox1.Text))
{
textbox1.Text = null;
}
for every textbox controls in form, is there a easier way to do it ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can iterate through the ControlCollection of the given form, e.g. frmMain.Controls
Now this will be the basic Control object, so you would need a test to see if it is of type TextBox.
.NET 2.0 – you’ll have to check this manually
.NET 3.0+ – use the
.OfType<TextBox>extension method to give you only a list ofIEnumerable<TextBox>Note that iterating through this from the form will only give you text boxes on that form. If you bind text boxes to a container it won’t show up there.
Safest bet would be to write a recursive function that walks through all the control collections and passes the reference to your test function to perform your test and update.