views:

115

answers:

2

My goal is to have one main richtextbox in the form, and then several different richtextbox's in the backround, the backround textbox's get added, removed, and edited behind the scences all the time... and i need a way to swap my richtextbox in the form with the richtextbox's in the backround. what i origannally had was a Dictionary

Dictionary<string, RichTextBox> RichTextBoxs = new Dictionary<string, RichTextBox>();

and would just go

string Data = "string";
RichTextBox box = new RichTextBox();
box.Text = "Session for \""+Data+"\" started!";
box.Tag = (string)Data;
RichTextBoxs.Add(Data, box);

and it saves fine, but the moment i try something like

richTextBox1 = RichTextBoxs[(string)Data];

Nothing happens! i can copy over attributes like

richTextBox1.Text = RichTextBoxs[(string)Data].Text;

works fine, but i need to copy ALL the properties, along with my advanced color formatting in the textbox's. i dont know why it isnt working, because as far as i know it should!

Summary: I need to be able to swap out form textbox's with stored textbox's

~code exampled appreciated! and thanks in advance!

+1  A: 

Why not just store the data for each textbox in a dictionary, and swap it in and out of the same actual control? As I understand it all of the formatting etc. of RTF is right in the raw text data (which may be different than the text property). Flipping around and showing/hiding all these controls may cause you a ton of headaches, and an alternative solution would be preferable in my opinion.

To add more stuff: The reason it doesn't work in your sample, is because you are missing the giant pile of designer generated code that goes along with windows forms controls; stuff like position, size, visibility, etc, etc. You are replacing the class reference to the original RTF box, but the new one doesn't have any of the same stuff initialized.

EDIT 2: If you really do need to make it work as described, you are going to have to put a lot of work into the code that switches them. Take the hosting control (probably the form itself) and remove the currently displayed RTF box. Then, you will have to initialize all of the properties of the replacement box like size, position, anchoring style, etc. Finally you will need to actually add the new control to the hosting element that held the previous RTF box. I don't recall right now, but I think there is even another step required in there to prevent memory leaks.

David Hay
Agreed. Extra form controls = more graphics card drawing, more memory and processor resources, and likely more code in the long run.
Jrud
+1  A: 

To solve your problem, you just need to use the Rtf property, not the Text property:

richTextBox1.Rtf = RichTextBoxs[(string)Data].Rtf;

But I agree with David Hay that you should be storing the Rtf string directly in the Dictionary, not in another hidden RichTextBox.

Mark Byers
Ahh, the Rtf property is what I was thinking of when I was talking about "raw text data" in my answer. This would work, but like you said with the code above there is no reason to keep those extra RTF boxes around, since all they are doing is storing string data.
David Hay
If you took a closer look at my question, i did state that the "TextBox's" are edited in the backround too! without them being shown. which means i either need the textbox's, or ill need to edit the RTF directly
Xavier
OK, well I won't argue with you about what you should be doing. You know your project better than I do. :) I'm glad that I could help you!
Mark Byers
yeah thanks for the help!
Xavier