Help with form controls

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

Hello,

I have multiple text box controls on a form, and they are named txt_Test1, txt_Test2, txt_Test3, etc.
I would like to have a loop that will populate all of these controls with the text: "ABC".
When I run the following:

for (int i = 1; i <= TESTNUM; i++)
{
((TextBox)this.Controls["txt_Test" + i.ToString()]).Text = "ABC";
}

I get a "System.NullReferenceException: Object reference not set to an instance of an object." error on the first item.
If I do:

txt_Test1.Text = "ABC";

then it works fine, but I have to do that for each one separately. I would like to use a loop instead. Can anyone help?

Cheers,
Max.
 
Hello,

I have multiple text box controls on a form, and they are named txt_Test1, txt_Test2, txt_Test3, etc.
I would like to have a loop that will populate all of these controls with the text: "ABC".
When I run the following:

for (int i = 1; i <= TESTNUM; i++)
{
((TextBox)this.Controls["txt_Test" + i.ToString()]).Text = "ABC";

}

I get a "System.NullReferenceException: Object reference not set to an instance of an object." error on the first item.
If I do:

txt_Test1.Text = "ABC";

then it works fine, but I have to do that for each one separately. I would like to use a loop instead. Can anyone help?

Cheers,
Max.

i dont see anything wrong with the code.infact i copied it over to my
winform n it worked just fine.

-
shashank kadge
 
Back
Top