1

There are "N" number of Textboxes on a Form,

I want to get the values in the textboxes in an Array.

using "For" loop,

Can any help me out with this.

1 Answer 1

2

You can get all the controls on a form by calling this.Controls and loop through those comparing the control to a TextBox, when it is a TextBox you add the value to the array you are mentioning.

I'd use something like this:

List<string> values = new List<string>();
foreach(Control c in this.Controls)
{
    if(c is TextBox)
    {
        /*I didnt need to cast in my intellisense, but just in case!*/
        TextBox tb = (TextBox)c;
        values.Add(tb.Text);
    }
 }
 string[] array = values.ToArray();
Sign up to request clarification or add additional context in comments.

7 Comments

Control doesn't have a Text property, I think you need to use as instead of is and check for != null.
Well, i typed it in VS2005 real quick and intellisense said it had .Text property... i was suprised aswell to be honest. I'll edit in the line needed to fix it!
thanks i will workk on as you said, and i tried over with this code but didnt worked out.....
What exactly didn't work? I edited in a cast as Graham commented. Please try with this cast.
Thanks Kate, It really worked out...my textboxes are placed into Panel1>Panel2>TableLayoutpanel....<br>Thanks Man
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.