Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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;

C#
Posted

Why don't you use the parent's Controls collection to step through?

Then you can do something like:

foreach(Control c in Parent.Controls)
{
   RichTextBox myTextBox = c as RichTextBox;
   if(myTextBox != null)
   {
      //do stuff..
   }
}

 

Hope its what you're looking for...

Howzit??
Posted

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?

C#
Posted

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;

C#
  • *Gurus*
Posted
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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted (edited)

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.

Edited by mooman_fl

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

  • *Experts*
Posted

I haven't looked at the program attached, but assuming I am understanding

correctly, you should always do it this way:

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).

Posted

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";

C#
  • *Gurus*
Posted

I'm afraid you've been led slightly astray - this is the closest you'll get to what you were originally trying to do:

 

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

t[0].Text = "test 1";
t[1].Text = "test 2";
t[2].Text = "test 3";

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...