Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
Posted
Updated 1-Mar-18 2:14am
v2

I´m afraid that what you want (show html text in a MessageBox), it´s not possible.
The MessageBox only takes plain text.
Sorry!!
 
Share this answer
 
Comments
Keerthana Hari 1-Mar-18 3:41am    
I tried to format string using HTML tags and then show in message box, which is not working .So asking is there way to format my content and show as popup?
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.
 
Share this answer
 
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.
C#
public class FormattedMessageBox : Form
{
   // Here you can implement a message box the way it suits you
}


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
<html>
<body>
<p>Hello how are you doing? <FINE/NOTWELL>
<p></br>
</body>
</html>

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900