我有一个小应用程序,用于跟踪用户所做的副本。
然后,我希望能够在RTF文本框上输出复制历史记录。
copyDisplay.Clear();
string toDisplay = myEntries.getAllEntriesFormated();
copyDisplay.Rtf = toDisplay;
这些是我的电话
getAllentriesFormated()
循环添加用户复制内容的列表
因此,您最终可以使用以下格式的字符串
“RTF文本
普通文字“
问题是它将显示RTF文本,但它会抛出异常,因为我给它一个格式化为RTF文本和普通文本的字符串。
所以,我的问题是,如何让两者同时显示?我可以使用
copyDisplay.Text = toDisplay
但它会显示用于编码RTF文本的文本。
提前致谢!
答案 0 :(得分:1)
不是使用纯文本和RTF创建连接字符串,而是尝试弄清楚哪些部分是哪些,为什么不返回字符串列表,然后将每个部分追加到RichTextBox
,测试是否每个字符串是RTF还是纯文本?如果您在添加文本时反复关注富文本框闪烁和重绘,则可以调整How do you prevent a RichTextBox from refreshing its display?的解决方案以防止:
public static class RichTextBoxExtensions
{
public static void SetTextOrRtf(this RichTextBox richTextBox, IEnumerable<string> texts)
{
if (richTextBox == null || texts == null)
throw new ArgumentNullException();
using (richTextBox.BeginUpdate())
{
bool first = true;
richTextBox.Clear();
foreach (var text in texts)
{
if (text == null)
continue;
if (first)
first = false;
else
richTextBox.AppendText("\n");
richTextBox.Select(richTextBox.TextLength, 0);
if (text.IsRtf())
{
richTextBox.SelectedRtf = text;
}
else
{
richTextBox.SelectedText = text;
}
}
richTextBox.SelectionStart = richTextBox.Text.Length;
richTextBox.ScrollToCaret();
}
}
public static bool IsRtf(this string text)
{
// Adapted from http://stackoverflow.com/questions/22502924/how-to-determine-text-format-in-c-sharp
if (text == null)
return false;
const string rtfPrefix = @"{\rtf1";
int start = 0;
for (; start < text.Length && Char.IsWhiteSpace(text[start]); start++)
;
return string.Compare(text, start, rtfPrefix, 0, rtfPrefix.Length, StringComparison.Ordinal) == 0;
}
private const int WM_USER = 0x0400;
private const int EM_SETEVENTMASK = (WM_USER + 69);
private const int WM_SETREDRAW = 0x0b;
class ResetEventMask : IDisposable
{
readonly IntPtr oldEventMask;
RichTextBox richTextBox;
public ResetEventMask(RichTextBox richTextBox, IntPtr oldEventMask)
{
this.richTextBox = richTextBox;
this.oldEventMask = oldEventMask;
}
#region IDisposable Members
public void Dispose()
{
var richTextBox = Interlocked.Exchange(ref this.richTextBox, null);
if (richTextBox != null && !richTextBox.IsDisposed)
{
SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
SendMessage(richTextBox.Handle, EM_SETEVENTMASK, IntPtr.Zero, oldEventMask);
}
}
#endregion
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public static IDisposable BeginUpdate(this RichTextBox richTextBox)
{
if (richTextBox == null)
throw new ArgumentNullException();
// Adapted from http://stackoverflow.com/questions/192413/how-do-you-prevent-a-richtextbox-from-refreshing-its-display
SendMessage(richTextBox.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
var oldEventMask = (IntPtr)SendMessage(richTextBox.Handle, EM_SETEVENTMASK, IntPtr.Zero, IntPtr.Zero);
return new ResetEventMask(richTextBox, oldEventMask);
}
}
然后称之为:
IEnumerable<string> toDisplay = myEntries.getAllEntriesInList();
copyDisplay.SetTextOrRtf(toDisplay);