views:

47

answers:

2

Hi Stack-O

I have a RichTextBox created programmatically with the following code:

RichTextBox RT = new RichTextBox();
RT.Name = "asdf";
RT.Text = "blah";

TableLayoutPanel.Controls.Add(RT,0,0);

Now let's say I want to modify the text of RT, and it's name is "asdf", Visual Studio won't allow me to write asdf.Text = "haha" because asdf doesn't exist yet.

How can I grab "asdf" specifically and set its text? Because this RichTextBox is in a specific cell, can I grab it based on its cell coordinates?

+1  A: 

Well... you did instantiate the RichTextBox and have a reference that you can use; it's called "RT" in your example.

Now, likely you've done this in a method so it was locally scoped and is no longer available when you want it. So you save that reference somehow by assigning it to some member you can access. If you have a lot of them and want to differentiate by name somehow, you might stick it into a Dictionary<string, RichTextBox>, for example. Or you could put it in some static variable; there are numerous options, each with their own pros and cons.

The one thing you probably don't want to do is walk the control tree looking for the control with the name you specified. But you could also do that, if you really wanted to.

Randolpho
Randolpho, that was very helpful, I will try to implement what you have described here. Thanks!
Soo
@Randopho ~ I would think that looking for Controls.Find('name',false) is EXACTLY the reason for that particular function existing. Why wouldn't you use it for that purpose? If you're building thousands of controls dynamically it might take some time to find it, but I don't imagine most forms will contain more than a few hundred elements, so walking the tree (yes, I see that reflector does show a `for(i;i<count;i++){...}` treewalk) shouldn't take that long.
drachenstern
@drachenstern: Let me put it to you another way: if you were building a database table with a sizable number of records and knew that you were going to search on a field that you knew was going to be unique, wouldn't you put an index on that field so that you could search faster? Consider my `Dictionary<string, RichTextBox>` to be such an index.
Randolpho
@Randolpho ~ Define `sizable`. I think for less than [some large number of] controls the `.Find()` method is sufficient for the work at hand. I'm not saying it's not a valid design, but the guy got confused between `RT` and `asdf`. Complexity and elegance are the tools of the experienced. I'm not saying that the Dictionary wouldn't be faster. I'm just asking "what's fast enough"?
drachenstern
@drachenstern: good point.
Randolpho
@Randolpho ~ Thanks, and good design points on your end ya know. I would probably do what you were talking about if I were designing a forms app myself where I had to do this ;) ... just was thinking like him.
drachenstern
+2  A: 

You should be able to get a reference to it via the TableLayoutPanel.Controls property, which returns a TableLayoutControlCollection. That class provides two ways to locate a control by name: the Item property and the Find method. The Item property returns a control with the specified name, whereas the Find method returns a collection of controls. In both cases you would need to cast from a Control to a RichTextBox.

var rt = (RichTextBox)myTableLayoutPanel.Controls.Item["asdf"];

// or

var rts = myTableLayoutPanel.Controls.Find("asdf", false);
foreach (var rt in rts)
    // (RichTextBox)rt ...

EDIT: be sure to check that the result is not null before using it in case the control is not found.

Ahmad Mageed