交叉线程 rtf 格式
cross thread rtf format
我使用此函数在跨线程情况下更新 RichTextBox
public void AddRtf(string text)
{
// cross thread allowed
if (rtb.InvokeRequired)
{
rtb.Invoke((MethodInvoker)delegate()
{
AddRtf(text);
});
}
else
{
rtb.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}"; // this works
rtb.Rtf = @"{\rtf1\ansi This "+text+"is in \b bold\b0.}"; // this not
}
}
但是,这不起作用,我在传递 "text" 参数时看不到 RTF 格式。
会有什么问题?
事实上,我需要一个简单的解决方案来更新文本中带有颜色、粗体、下划线和一些 URL 的 RichTextBox。我为此编写了一些函数,例如 rtb.AddLink() .AddBold() 等等,包括用于添加 URL 的一个很好的扩展,但似乎更合乎逻辑地传递 RTF 格式并让控件更新格式。但这将迫使我在需要粗体字或其他内容的每个点中断文本。
我认为 HTML 会更方便,但我需要一个简单的解析器,至少比 HTMLAgilitypack 更简单。
一行就这么简单:
log.write("<font color="red">This is error</font> and this is the link... etc")
有人对此有简单的解决方案吗?
您需要转义字符串第二部分的\
:
@"{\rtf1\ansi This "+text+"is in \b bold\b0.}"
^^ ^^
或再次使用 @
@"{\rtf1\ansi This "+text+@"is in \b bold\b0.}"
^
我使用此函数在跨线程情况下更新 RichTextBox
public void AddRtf(string text)
{
// cross thread allowed
if (rtb.InvokeRequired)
{
rtb.Invoke((MethodInvoker)delegate()
{
AddRtf(text);
});
}
else
{
rtb.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}"; // this works
rtb.Rtf = @"{\rtf1\ansi This "+text+"is in \b bold\b0.}"; // this not
}
}
但是,这不起作用,我在传递 "text" 参数时看不到 RTF 格式。
会有什么问题?
事实上,我需要一个简单的解决方案来更新文本中带有颜色、粗体、下划线和一些 URL 的 RichTextBox。我为此编写了一些函数,例如 rtb.AddLink() .AddBold() 等等,包括用于添加 URL 的一个很好的扩展,但似乎更合乎逻辑地传递 RTF 格式并让控件更新格式。但这将迫使我在需要粗体字或其他内容的每个点中断文本。
我认为 HTML 会更方便,但我需要一个简单的解析器,至少比 HTMLAgilitypack 更简单。
一行就这么简单:
log.write("<font color="red">This is error</font> and this is the link... etc")
有人对此有简单的解决方案吗?
您需要转义字符串第二部分的\
:
@"{\rtf1\ansi This "+text+"is in \b bold\b0.}"
^^ ^^
或再次使用 @
@"{\rtf1\ansi This "+text+@"is in \b bold\b0.}"
^