我使用此函数在跨线程情况下更新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
}
}但是,不工作,我不能看到RTF格式时,传递“文本”参数。
会有什么问题?
事实上,我需要一个简单的解决方案来更新RichTextBox,包括颜色、粗体、下划线和文本中的一些URL。我为此编写了一些函数,如rtb.AddLink()、.AddBold()等,包括一个用于添加URL的很好的扩展,但传递RTF格式并让控件更新格式似乎更符合逻辑。但这将迫使我在需要粗体或其他东西的每一点上打破文本。
我认为HTML会更方便,但我需要一个简单的解析器,至少比HTMLAgilitypack更简单。
所以简单地用一行来写:
log.write("<font color="red">This is error</font> and this is the link... etc")有人对此有简单的解决方案吗?
发布于 2017-04-21 13:25:05
您需要在字符串的第二部分转义\:
@"{\rtf1\ansi This "+text+"is in \\b bold\\b0.}"
^^ ^^或者再次使用@
@"{\rtf1\ansi This "+text+@"is in \b bold\b0.}"
^https://stackoverflow.com/questions/43543854
复制相似问题