How to display label text with line breaks in c#

This is very basic stuff in .net , while developing the web/windows application  in many scenarios we may required to use a multi line label text. I have seen that many of the developers are using just another label and they were struggling to place it in proper alignment .This will impact your performance and coding skills. let us see here How to display label text with line breaks in c#
we can achieve this in two different ways. one is using HTML<br/> tag and another using string builder.
1) You may append HTML <br /> in between your lines. Something like:

MyLabel.Text = "Dotnetstock" + "<br />" + "Dotnetstock.com";

2) With StringBuilder :

StringBuilder sb = new StringBuilder();
sb.AppendLine("DotnetStock.com");
sb.AppendLine("Dotnetstock.com");
MyLabel.Text = sb.ToString().Replace(Environment.NewLine, "<br />");

Leave a Reply

Your email address will not be published. Required fields are marked *