在stringbuilder中添加HTML标记不起作用 [英] Appending HTML tags in stringbuilder not working

查看:72
本文介绍了在stringbuilder中添加HTML标记不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现字符串格式样式,例如粗体,带下划线的文本,斜体等几个单词。

我尝试过的示例使用字符串构建器不起作用。



我尝试使用stringBuilder Append(),AppendFormat()和AppendLine(),但都没有奏效。我不想将内容拆分为子字符串并通过C#代码应用字体样式。



有没有办法在C#中为字符串类型应用样式。请建议任何更好的方法来实现它。



我尝试过:



I am trying to achieving a string format styling such as few words in bold , underlined text ,italic as well.
The sample I have tried does not work as mentioned below using string builder.

I tried using stringBuilder Append(),AppendFormat() and AppendLine(), none of it worked. I don't want to split the content as substring and apply font styles through C# code.

Is there any way to apply styling for string type in C#. please do suggest any better approaches to achieve it.

What I have tried:

var textWithBrakeLine = string.Format(text, Environment.NewLine);
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<html>");
            builder.AppendLine("<body>");
            builder.AppendLine("<p>Hello how are you doing? <FINE/NOTWELL>");
            builder.AppendLine("<p></br>");
            builder.AppendLine("</body>");
            builder.AppendLine("</html>");          

            MessageBox.Show(builder.ToString(), "sample text", MessageBoxButtons.OK);

推荐答案

我害怕你想要的东西(在一个显示html文本) MessageBox),这是不可能的。

MessageBox只接受纯文本。

抱歉!!
I´m afraid that what you want (show html text in a MessageBox), it´s not possible.
The MessageBox only takes plain text.
Sorry!!


我认为你有一个Windows窗体应用程序是正确的吗?



在这种情况下,您可以创建自己的窗体并添加一个RichTextBox,然后您可以使用它来添加格式化文本。

您可以将该表格用作自定义MessageBox。

例如,检查 MessageBox 发布了一些解决方案。
I presume you have a Windows Forms application, is that correct?

In that case, you could create your own Form and add a RichTextBox to it, which you could then use to add a formatted text.
You could use that Form as your custom MessageBox.
For instance, check this post, people have posted some solutions for an extended MessageBox.


假设您使用的是 System.Windows.Forms。 MessageBox [ ^ ](你没有说明哪个项目类型):

无法格式化<$ c的文本$ C>的MessageBox 。你不能有粗体,斜体等格式说明符。并且它也不会识别HTML格式。



有解决方案,但这意味着



- 要么你找到一篇文章,其中某人已经完成了这项工作;例如,CP上有一个: FlexibleMessageBox - .NET的灵活替代品MessageBox [ ^ ]

但也可能有其他人。例如,您可以在Google上搜索c#winforms格式化邮箱。



- 或者通过继承表单来实现自己的消息框 class。

Assuming you are using a System.Windows.Forms.MessageBox[^] (you did not stated which project type):
It is not possible to format the text of a MessageBox. You cannot have bold, italic, etc... format specifiers. And it will not recognize HTML format, either.

There are solutions, but that implies that

- either you find an article in which someone already did the job; for example there is one here on CP: FlexibleMessageBox – A flexible replacement for the .NET MessageBox[^]
There can be others, though. You can have a Google search for "c# winforms formatted messagebox" for example.

- or you implement your own message box by subclassing the Form class.
public class FormattedMessageBox : Form
{
   // Here you can implement a message box the way it suits you
}





而且,我想说,即使它是可以使用一些HTML代码格式化Windows窗体MessageBox,正如您尝试的那样,您生成的结果将不是有效的HTML代码。以下是您正在构建的字符串将采用的内容:



Moreover, I would like to say that, even if it was possible to format a Windows Forms MessageBox with some HTML code, as you tried to do, the result which you would have produced would not have been valid html code. Here is what the string you are building would resort to:

<html>
<body>
<p>Hello how are you doing? <FINE/NOTWELL>
<p></br>
</body>
</html>



第3行:你不能写< > 字符,因为HTML解析器使用这些字符来标识文档的标记。因此,< FINE / NOTWELL> 将被解释为HTML标记;一个无效的。要在HTML文档中编写< > ,您必须使用&分别是& gt;

也许这可以帮助您理解HTML实体:

HTML实体 - w3schools [ ^ ]



第3行:开头< p> 标签需要关闭< / p> 对应。



第4行:与上述相同。


Line 3: you cannot write a < or > character in a HTML string, because these characters are used by HTML parsers to identy the tags of the document. Thus, <FINE/NOTWELL> would be interpreted as a HTML tag; an invalid one. To write < and > in a HTML document, you have to use &lt; and &gt;, respectively.
Maybe that could help you understand HTML entities:
HTML Entities - w3schools[^]

Line 3: an opening <p> tag need its closing </p> counterpart.

Line 4: same remark as above.


这篇关于在stringbuilder中添加HTML标记不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆