Hold richTextBox1.Text and so on in an array?

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
I have 12 richTextBoxes and 12 lables that I wish to flip through easily in my program with a for loop but I don't know if or how I can put richTextBox1.Text
richTextBox2.Text and so on in an array.
Here is one example:

string[]data=new string[CommunicationLog.textBoxCount];
data[0]+=this.richTextBox1.Text;
data[1]+=this.richTextBox2.Text;
return data;
 
Why don't you use the parent's Controls collection to step through?
Then you can do something like:
C#:
foreach(Control c in Parent.Controls)
{
    RichTextBox myTextBox = c as RichTextBox;
    if(myTextBox != null)
    {
       //do stuff..
    }
}

Hope its what you're looking for...
 
Nope. That would be easiest but it won't work in my case because I need to go in a special order and that order could change. Can't I store ref's to
this.TextBox1.Text
in an array?
 
There has got to be some kind of orderly way that I can refer to the textbox text. I tried storing
this.richTextBox1(2,3,4,5,6,7,...).Text in an object array but it was like a variable instead of the actual text box text. Here is an example of what I am trying to avoid:

holdIt=SR.ReadToEnd(); SR.Close(); ClearTBs(); this.richTextBox1.Text=holdIt[0]; this.richTextBox2.Text=holdIt[1]; this.richTextBox3.Text=holdIt[2]; this.richTextBox4.Text=holdIt[3];

I would like to put it in a for loop instead and just write:
?Write in textbox?=holdIt;
 
I'm still not entirely clear on what you're trying to do here. If you're trying to store a reference to the text in a richtextbox, i.e. when you change your reference, the text in the richtextbox changes too, you can't. The String class is immutable.
 
I am trying to store references to the properties Text in each text box. So that when I reference t in a for loop it will be just like writing textBox2.Text.
 
Actually it absolutely CAN be done... unless I am misunderstanding what divil is talking about... but you don't get intellisense showing anything else except GetType when you try to use the array as the object. The object properties are there you just can't see them in the intellisense box and will have to know them and type them manually. Not sure why this is so. I will attach a project in a minute that demonstrates this. I already did a small test to make sure it worked first but need to clean it up a bit and add a few things to give a good demonstration.

In your original post you were talking about something like:

myarray(1).text = "Hello there"

At least this is what I thought you were talking about... that is what my demo will be based on. After rereading though I realized that one of your ideas divil is right on as far as I can tell... you can't reference the text property of the control directly to the array... but you can accomplish the same thing by using the array as the object itself.
 
Last edited:
I haven't looked at the program attached, but assuming I am understanding
correctly, you should always do it this way:
Visual Basic:
DirectCast(myArray(1), TextBox).Text = "Hello there"
Not only will you get intellisense, it will keep Option Strict happy
(which you should always have enabled).
 
The demo was VB so I could not open it. I don't think I am doing this right:

object[]t={this.richTextBox1.Text,this.richTextBox2.Text,this.richTextBox3.Text};
(TextBox)t[0]="HI";
 
I'm afraid you've been led slightly astray - this is the closest you'll get to what you were originally trying to do:

C#:
TextBox[] t = { textBox1, textBox2, textBox3 };

t[0].Text = "test 1";
t[1].Text = "test 2";
t[2].Text = "test 3";
 
Perhaps I am just not understanding what is being done here. :-\

[edit]Oh, I see (just reread the original post)[/edit]
 
Back
Top