aewarnick Posted February 13, 2003 Posted February 13, 2003 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; Quote C#
Cywizz Posted February 13, 2003 Posted February 13, 2003 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... Quote Howzit??
aewarnick Posted February 14, 2003 Author Posted February 14, 2003 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? Quote C#
aewarnick Posted February 15, 2003 Author Posted February 15, 2003 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; Quote C#
*Gurus* divil Posted February 15, 2003 *Gurus* Posted February 15, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted February 15, 2003 Author Posted February 15, 2003 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. Quote C#
*Gurus* divil Posted February 15, 2003 *Gurus* Posted February 15, 2003 Sorry, you can't do that. Closest you'll get is having an array of textboxes instead. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted February 15, 2003 Author Posted February 15, 2003 At least I tried. Thanks divil. Quote C#
mooman_fl Posted February 16, 2003 Posted February 16, 2003 (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 February 16, 2003 by mooman_fl Quote "Programmers are tools for converting caffeine into code." Madcow Inventions -- Software for the Sanity Challenged.
mooman_fl Posted February 16, 2003 Posted February 16, 2003 Ok... back again with a sample program. Hopefully you can get something useful out of this.windowsapplication9.zip Quote "Programmers are tools for converting caffeine into code." Madcow Inventions -- Software for the Sanity Challenged.
*Experts* Volte Posted February 16, 2003 *Experts* Posted February 16, 2003 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). Quote
*Gurus* divil Posted February 17, 2003 *Gurus* Posted February 17, 2003 That is exactly what I suggested in my last post, having an array of textboxes. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted February 17, 2003 Author Posted February 17, 2003 I don't think that is C#. Quote C#
*Experts* Volte Posted February 17, 2003 *Experts* Posted February 17, 2003 Oh right.. you just need to cast it using(TextBox)myArray(1) Quote
aewarnick Posted February 17, 2003 Author Posted February 17, 2003 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"; Quote C#
*Gurus* divil Posted February 17, 2003 *Gurus* Posted February 17, 2003 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"; Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted February 17, 2003 *Experts* Posted February 17, 2003 Perhaps I am just not understanding what is being done here. :-\ [edit]Oh, I see (just reread the original post)[/edit] Quote
aewarnick Posted February 17, 2003 Author Posted February 17, 2003 That was exactly what I wanted! How much room in memory will that array take up? Quote C#
*Gurus* divil Posted February 17, 2003 *Gurus* Posted February 17, 2003 Virtually nothing; it is holding references to the textbox objects, not the objects themselves. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted February 17, 2003 Author Posted February 17, 2003 Even Better!!!! Thank you very much for all your help!!! Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.