开发者

Saving (Rich)TextBox's to memory and switching them off with a richtextbox in my form in C#

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

ric开发者_如何学PythonhTextBox1 = 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜