March 1st, 2011, 06:16 PM
[Q] Issue with (RichTextBox)this.Controls[...];
As part of a larger project, on one form I have several rich text box controls. I am trying to access them all in the following for loop.
for (int i = 0; i < 7; i++)
RichTextBox txtbx = (RichTextBox)this.Controls["txtbx" + ((lanterntypes)i).ToString()];
string lines = new string;
lines = "goodbye";
for (int i2 = 1; i2 <= 3; i2++)
lines[i2] = "hello";
txtbx.Lines = lines;
However, txtbx returns as null for every iteration.
lanterntypes is an enum, of which the first is ParCan, and I have a rich text box called txtbxParCan so I don't think there is a problem with the names in fact being different.
I've used this method before in this same project succesfully with labels, so any help as to what I'm doing wrong now would be gratefully received.
Cheers to anyone who can help!
March 2nd, 2011, 12:16 AM
Re: [Q] Issue with (RichTextBox)this.Controls[...];
Oh, yeesh. There is rarely a good reason to access controls by their name. Make your life easier and keep a reference to the controls. If they're added in the designer you already have that. If you are creating these at runtime then add them to a collection of RickTextBox controls.
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!