C# 将文本直接添加到控件文本会破坏RichTextBox中的内容
我正在制作一个小的文本编辑器,作为更大应用程序的一部分。因此,我使用了一个C# 将文本直接添加到控件文本会破坏RichTextBox中的内容,c#,C#,我正在制作一个小的文本编辑器,作为更大应用程序的一部分。因此,我使用了一个RichTextBox,并添加了一个带有一些按钮的工具条。实际上,编辑部分一切都很好。每次单击按钮时,我的应用程序都需要在文本框中添加DateTime戳记。当我这样做时,整个标记都消失了 所以我要做的是: private void button_click(object sender, EventArgs e) { richtextbox1.text += DateTime.Now.toString(); }
RichTextBox
,并添加了一个带有一些按钮的工具条。实际上,编辑部分一切都很好。每次单击按钮时,我的应用程序都需要在文本框中添加DateTime
戳记。当我这样做时,整个标记都消失了
所以我要做的是:
private void button_click(object sender, EventArgs e)
{
richtextbox1.text += DateTime.Now.toString();
}
:
要读取或设置多行文本框的文本,请使用“行”特性文本属性不返回任何有关应用于RichTextBox内容的格式信息。要获取RTF代码,请使用RTF属性
我可以帮你
本文展示了如何扩展RichTextBox以允许将字符串附加到其RTF属性
解决方案如下:
public void InsertTextAsRtf(string _text, Font _font,
RtfColor _textColor, RtfColor _backColor) {
StringBuilder _rtf = new StringBuilder();
// Append the RTF header
_rtf.Append(RTF_HEADER);
// Create the font table from the font passed in and append it to the
// RTF string
_rtf.Append(GetFontTable(_font));
// Create the color table from the colors passed in and append it to the
// RTF string
_rtf.Append(GetColorTable(_textColor, _backColor));
// Create the document area from the text to be added as RTF and append
// it to the RTF string.
_rtf.Append(GetDocumentArea(_text, _font));
this.SelectedRtf = _rtf.ToString();
}
及
在其他任何你正在使用richtextbox1.text的地方?你能看看更新你的原始帖子来清理一些拼写和语法吗。
public void AppendTextAsRtf(string _text, Font _font,
RtfColor _textColor, RtfColor _backColor) {
// Move carret to the end of the text
this.Select(this.TextLength, 0);
InsertTextAsRtf(_text, _font, _textColor, _backColor);
}