CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011

    [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[4];
    lines[0] = "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!


  2. #2
    Join Date
    Jun 2008

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.