Display Code From A Database with C# - c#

I have a database with almost 2000 code examples that I've collected over the last 10 years that I want to be able to access with a textarea field so I can copy and/or update easily. I'm trying to migrate from PHP to C# on a new domain, but I'm having problems getting it to display correctly.
This is how it looks currently: http://nunyabiz.freeiz.com/csharp/index.html
This is how it's supposed to look: http://nunyabiz.freeiz.com/csharp/index2.html
This is the code that I'm using to display it:
Code.Text = rs["Code"].ToString().Replace("\r\n", "\n");
The "\r\n" characters aren't getting recognized, so I'm guessing that I have to convert, encode or decode it, but I haven't had any luck trying to find something that will work. online.

you can try Environment.NewLine for Linebreaks Environment.Newline
strYourCode = string.Format("Name: {0} LineBreak {1}", yournameVariable, Environment.NewLine);

Try to figure out WHAT line breaks you have in your code samples - there are many variants including html tag br, and then change it to what you need (i think it is Environment.Newline)
And check that your Code control is Multiline

Give this a try .Replace("\r", "").Replace("\n", "<br/>");

Related

How to achieve new line display in asp.net 7 C#

In my previous WebAPI project before this one, I always achieved multiline display of texts by using the newline.
\n
within the string as follows and it worked, displaying the different options on different lines when I return the string as response.
Message= "What do you want to do? \n" +
"1 Option 1 \n" +
"2 Option 2 \n" +
"3 Option 3"
But the last I used it was in a dotnet 6 application.
Currently, I am working on a dotnet 7 project and when I do the same thing as above, the options appear in one line in the method response.
I have also tried to use Environment.Newline to achieve the newline behaviour but to no avail still.
I have also tried to use StringBuilder as follows:
sb.AppendLine("What do you want to do?");
sb.AppendLine("1 Option 1 ");
sb.AppendLine("2 Option 2");
sb.AppendLine("3 Option 3");
Message = sb.ToString();
Please what am I doing wrong here?
Make sure you've selected the right option:
and for example ,in html
it makes a new line with the css setting white-space:pre-wrap,if you set like:white-space:nowrap,it would display in a line
Some clients would make a new line with "\r\n" instead of "\n",so it also depend on your client
Have you tried $- string interpolation along with Environment.NewLine?
Check the same in console or different browser as sometimes browser might not see it.

New line character from c# to cshtml

I am making a webpage... the controller is written in C# and the webpage is in cshtml. In the c# file, I am constructing strings to be put into the table on the html page. When I have my strings, I need line breaks within them, so I have things like stringpart1 + "\n" + stringpart2 in hope of the webpage displaying the string as two separate lines as I fill my table cells. However, everything seems to print out on the same line. I have also tried "\r\n" and System.Environment.NewLine instead. Any ideas as to a potential fix?
Here is a fix which worked for me. In your .cshtml, use this:
#Html.Raw(System.Web.HttpUtility.HtmlEncode([your content containing \n]))
Line breaks in HTML are represented by <br> tag. Change \n and/or \r to <br>.
Example:
part1 + "<br>" + part2;

How to put multiple lines of text into a textbox

I have something like this:
and i would like to have something like this:
You should use Environment.NewLine as suggested by Jonesopolis.
See the documentation here, the caracters for the new line depend on the system you are targeting. Let the .NET framework know what a new line is.
You can use it like that
string str = String.Format("this text{0}is on three{0}lines", Environment.NewLine);
Or, if you're using the last version of C# (can be less readable if you have a lot of new lines) :
string newLine = Environment.NewLine;
string str = $"this text is{newLine}on three{newLine}lines";
Insert "\r\n" where ever you want to add line
"It's Felix Birthday\r\nIt's DesBirthday\r\nIt's Fffffffs Birthday!"
As stated before\r\n is a good use to insert new lines. Alternatively (and my personal choice) is to use Environment.NewLine which effectively does the same thing. However this is based on the Enviroment and therefore should be cross environment compatible (all-be-it no other WPF environment exists).
Something like
string message = string.Format("It's Felix Birthday{0}It's DesBirthday{0}It's Fffffffs Birthday!", Environment.NewLine);
Really this will just produce the same result as entering in the \r\n but also makes it a bit more readable.
MessageBox.Show("It's Felix Birthday\nIt's DesBirthday\nIt's Fffffffs Birthday!");
Just put "\n" where you want to new line
By using TextWrapping, you will be able to use multiline textbox.
TextWrapping="Wrap" MaxLines="2" Width="150"

Get index of String between unique chars, then remove those chars and apply font to string in C#

so I hope someone here may help me.
My Situation:
I'm using NPOI in C# to export some things to excel. For that, I am also using HSSFRichTextStringto have multiple fonts in one cell.
So, the "cell in need" should look like this:
My Problem here lies in the formating of the "little Numbers" after each article, e.g. 3,20.
To have an entrypoint for using string-comparison, I tried to add ### before the values which worked fine, but I can't figure out how to apply the correct font to the correct substrings (all of them in menuCellContent.
Here's my code so far (I tried a lot of things with stringfunctions, but nothing worked so I think I'll go for the startingpoint so far..), I think it'll make things a little bit clearer (sorry, german here):
var richString = new HSSFRichTextString(menuCellcontent + "\n" + nutrientCellContent);
richString.ApplyFont(0, menuCellcontent.split('###')[0], menuFont);
richString.ApplyFont(menuCellcontent.split('###')[0] + 1, (menuCellcontent + "\n" + nutrientCellContent).Length, nutrientFont);
menuCell.SetCellValue(richString);
but this results in the following output:
which is also not what I want to achieve. I tried for the last few hours to find a string method to generate the desired output, but somehow I always failed. Would be grat if someone here can help me! I think I just have a real knot in my brain..
Best regards,
goide

What's with the line break variations in C# and ASP.NET? (\r\n vs.\n)

I've been writing code for ASP.NET since the start, and today I encountered something I've never seen before. Typically I've looked for line breaks in C# (when posted in ASP.NET from a textarea, for example) by expecting "\r\n". Now I'm using the MVC framework, and the text coming over the wire from a textarea simply has "\n" for line breaks.
Is there something going on in old school TextBox controls that normalizes the line breaks? Since MVC just uses the form data as-is, is this normal? Would love to get some insight.
I have made observation that the actual line break sequence differs from browser to browser.
If you have a multiline textarea on a page and the page is submitted then:
IE returns "\r\n" to indicate newlines. FF returns "\n" in this case.
I tested it somewhere along the end of 2006, so it may be different now.
I do not believe it could have anything to do with WebForms vs. MVC. Both just process submitted input and return it to you as it is.
If you wish to somehow process and replace these characters it would make sense doing it in the long-to-short order:
string userText = form["Text"];
userText = userText.Replace ("\r\n", "<br/>").Replace ("\r", "<br/>");
\n is the line ending variant on *nix-style systems. \r\n is Windows-specific line-ending behaviour.
If you're checking for line-endings and expose your interface to non-Windows environments, be sure to check not only for \r\n but also for \n alone.
Fore more background, check out the Newline article at Wikipedia.
I am using Environment.NewLine :
string userText = form["Text"];
userText = userText.Replace (Environment.NewLine, "<br />")
Also take a look at #Haacked's post about some newline textarea quirks.

Categories