将(Rich)TextBox保存到内存中,并使用我的C#表单中的richtextbox将其关闭
我的目标是在表单中有一个主要的富文本框,然后在后台有几个不同的富文本框,背景文本框一直在幕后添加、删除和编辑......我需要一种方法来交换我的富文本框后台带有 Richtextbox 的表单。我原本拥有的是一本字典
Dictionary<string, RichTextBox> RichTextBoxs = new Dictionary<string, RichTextBox>();
,只要走
string Data = "string";
RichTextBox box = new RichTextBox();
box.Text = "Session for \""+Data+"\" started!";
box.Tag = (string)Data;
RichTextBoxs.Add(Data, box);
就可以保存得很好,但是当我尝试类似的事情时
richTextBox1 = RichTextBoxs[(string)Data];
什么也没有发生!我可以复制属性,就像
richTextBox1.Text = RichTextBoxs[(string)Data].Text;
工作正常一样,但我需要复制所有属性,以及文本框中的高级颜色格式。我不知道为什么它不起作用,因为据我所知它应该起作用!
摘要:我需要能够用存储的文本框交换表单文本框
〜代码示例表示赞赏!提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要解决您的问题,您只需要使用
Rtf
属性,而不是Text
属性:但我同意 David Hay 的观点,您应该将 Rtf 字符串直接存储在字典,不在另一个隐藏的 RichTextBox 中。
To solve your problem, you just need to use the
Rtf
property, not theText
property:But I agree with David Hay that you should be storing the Rtf string directly in the Dictionary, not in another hidden RichTextBox.
为什么不将每个文本框的数据存储在字典中,然后将其交换到相同的实际控件中?据我了解,RTF 的所有格式等都在原始文本数据中(可能与文本属性不同)。翻转并显示/隐藏所有这些控件可能会给您带来很多麻烦,在我看来,替代解决方案会更好。
要添加更多内容:
它在您的示例中不起作用的原因是您缺少与 Windows 窗体控件一起使用的大量设计器生成的代码;您正在替换对原始 RTF 框的类引用,但新的 RTF 框没有初始化任何相同的内容。
编辑2:
如果您确实需要使其按照描述的方式工作,则必须在切换它们的代码中投入大量工作。获取托管控件(可能是表单本身)并删除当前显示的 RTF 框。然后,您必须初始化替换框的所有属性,如大小、位置、锚定样式等。最后,您需要将新控件实际添加到保存先前 RTF 框的托管元素。我现在不记得了,但我认为还需要采取另一个步骤来防止内存泄漏。
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.