control help

  • Thread starter Thread starter Dave Petersen
  • Start date Start date
D

Dave Petersen

I have 10 textboxes on my page, textbox1 - textbox10. Rather than modify
them individually, I would like to modify them with a variable something
like this:

for (int i = 0; i <= 10; i++)
{
textbox.text = "some text";
}

Is this possible? Would I need to create some kind of control array?

Thanks,
Dave
 
You could either not use the form designer to create them, by creating them
in your constructor like this:

public Form() {
InitializeComponent();
TextBox[] txts = new TextBox[10];
for (int i = 0; i < 10; i++) {
TextBox t = new TextBox();
t.Location =
t.Size =
t.Text =
txts.Add(t);
Controls.Add(t);
}
}

or you could assign them afterwards manually

TextBox[] txts = new TextBox[10];
txts[0] = textBox1; txts[1] = textBox2; etc;

Chris
 
Chris,
thanks for the info. I ended up doing this:

TextBox[] txts = new TextBox[10];
txts[0] = TextBox1;
txts[1] = TextBox2;
....

for (int i = 0; i <= 9; i++)
{
txts.Text = "Control " + Convert.ToString(i + 1);
}

It worked great!
Dave

Chris Capel said:
You could either not use the form designer to create them, by creating them
in your constructor like this:

public Form() {
InitializeComponent();
TextBox[] txts = new TextBox[10];
for (int i = 0; i < 10; i++) {
TextBox t = new TextBox();
t.Location =
t.Size =
t.Text =
txts.Add(t);
Controls.Add(t);
}
}

or you could assign them afterwards manually

TextBox[] txts = new TextBox[10];
txts[0] = textBox1; txts[1] = textBox2; etc;

Chris

Dave Petersen said:
I have 10 textboxes on my page, textbox1 - textbox10. Rather than modify
them individually, I would like to modify them with a variable something
like this:

for (int i = 0; i <= 10; i++)
{
textbox.text = "some text";
}

Is this possible? Would I need to create some kind of control array?

Thanks,
Dave

 
Back
Top