New Line textbox - c#

I have a comment box that users can fill and saving the text in a MySQL database:
When a users fills text in my textbox with a backslash (\n) it shows in the text box without the new line.
For example, the user enters:
Hi everyone!
Me
The Textbox displays:
Hi Everyone!Me
What am I doing wrong?

Use Environment.NewLine instead of "\n" (or at least "\r\n", since this is what Environment.NewLine contains on non-Unix platforms anyway).
Also, make sure your TextBox's MultiLine property is set to true.
myTextBox.MultiLine = true;
myTextBox.Text = "hi," + Environment.NewLine + "ALL";

Did you set the Multiline property?
textBox.Multiline = true;

try this : when extracting from database use this :
str_replace("\n",'<br />',$databasefield)
it`s working for me:)

Related

Adding Text to a TextBox multiple times from code behind?

I'm still stuck... I'm using ASP.NET Web Forms.
I have a TextBox:
<p><asp:TextBox runat="server" TextMode="Multiline" ReadOnly="true" ID="txtBoxAlgo" rows="50" width="878px"></asp:TextBox></p>
I can only add text to this textbox Once from the code behind:
txtBoxAlgo.Text = ("My first line of text" + Environment.NewLine);
Now my issue is if I try to add text again to the textbox nothing happens. For example:
txtBoxAlgo.Text = ("First try! It works!");
txtBoxAlgo.Text = ("Nothing, not working :(");
Now I'm moving over a simple program I have made for a C# console and C# Windows forms. I'm experimenting with ASP.NET Web Forms.
I used Console.WriteLine(""); throughout my console application. The only way I can do something similar for webforms is make Labels and TextBoxes+Multilines for each instance I wanto output text to the screen, which at this point is not worth doing all that effort for each instance of Console.WriteLine.
I tried Response.Write, but first off this shows in the top left corner of the webpage and its all unformatted.
I have ran out of ideas and I can't find anything while searching. Can you add or Append text to a textbox? Can we use Response.Write in some type of way to format everything so a user can read it?
Use += instead of = when appending extra text to your Textbox
txtBoxAlgo.Text = "My first line of text" + Environment.NewLine;
txtBoxAlgo.Text += "First try! It works!";
txtBoxAlgo.Text += "Should work.";
Every time you use = when assigning a value to your Textbox, you are just replacing the previous value which is why only the last string is visible.
Or use StringBuilder:
StringBuilder sb = new StringBuilder();
sb.AppendLine("First Line");
sb.AppendLine("Second Line");
txtBoxAlgo.Text = sb.ToString();

WebBrowser Control insert word in caret position

I have a WPF WebBrowser control where I need to insert some text (e.g. the selected item coming from a listbox). This way, if the editor contains "When " and the listbox contains "I, You, He" and I click on "You", the editor must contain "When you".
The code I'm using with the ActiveElement is working right now, but I'm finding some weird issues explained below.
HTMLDocument doc;
doc = webBrowser.Document as HTMLDocument;
doc.activeElement.innerHTML += " " + selectedItemFromListboxAsString + " ";
Then, the webbrowser gets appended the selectedItemFromListboxAsString properly, but when I continue typing after that, the new keystrokes go to the next line. In the sourcecode I can see that a </P> tag has been put after the selectedItemFromListboxAsString. No matter if I remove the </P> programmatically, it will insert the new typed content in a new line.
Is there any other way to achieve the same result instead of playing around with the activeElement?
Thanks in advance.

How do i remove "System.Windows.Forms.TextBox" from the output text?

text2 = string.Format("[B](No Change)[/B]");
The output comes out as:
System.Windows.Forms.TextBox, Text: (No Change)
How can I remove the System.Windows.Forms.TextBox, Text: from my output?
Well a wild guess, but I think what you are trying to print is not the Text property of the textbox, instead the whole textbox.
Suppose you have:
TextBox1.Text = string.Format("[B](No Change)[/B]");;
Console.Write(TextBox1);
Then you will get the output.
System.Windows.Forms.TextBox, Text: [B](No Change)[/B]
What you probably need to do is to use Text property.
Console.Write(TextBox1.Text);
This will give you the assigned text and will exclude. System.Windows.Forms.TextBox, Text: part
I'm making a guess here but are you trying to make "No change" appear as bold text? In that case, you can modify the textbox font itself rather than try and format your string.
text2.text = "No change";
text2.Font = new Font(text2.Font, FontStyle.Bold);
If that's not what you were trying to do then sorry!
Do not forget to add Text
text2.Text = string.Format("[B](No Change)[/B]");

How to make string bold in code behind

Is there a way to make a string bolder in code behind using C# .NET?
I tried:
string TypeofDay = "<span style='font-weight: bold'>Type Of Day</span> ";
txtbox.Text = TypeofDay + ": " + "Delivery Day"
I am concatenating TypeofDay(bold) and "Delivery Day" to display in a textbox.
You can't make some bits bold and some bits not bold, in an <asp:TextBox>.
You can't make text bolder by enclosing text value in tags. You must change attribute of a control that displays that text for example by setting its CSS class or changing code-behind property:
txbSendMessageBody.Font.Bold = true;
PS. I am concatenating few bold strings and some other strings to display in a textbox.
A HTML text box does not support this. ASP.NET (usually) generates HTML; if HTML does not support this, you cannot solve it from the server side.
A bit hacky alternative can be to use Unicode bold characters
http://qaz.wtf/u/convert.cgi?text=Type+Of+Day
txtbox.Text = "𝗧𝘆𝗽𝗲 𝗢𝗳 𝗗𝗮𝘆: 𝖣𝖾𝗅𝗂𝗏𝖾𝗋𝗒 𝖣𝖺𝗒"
You could layer a div on top of the textbox. Since the text formatting would change anyway once the user started typing, you can just hide the div once focus is given to the div, thus showing the text box. If nothing is entered and focus is lost, show the div again.
TextBox, no, however you could use a label.
myLabel.text = "<b>bold text</b> normal text <u>underlined text</u> <span style='font-size:Large; color:Red'>Big red text</span>";

show line breaks in output

when i enter text into a text area with linebreaks this is not being output later when i display the text. how do i let it show the line spaces?, instead all the text is cramped together at the moment.
How do you display it? HTML ignores whitespace, including line breaks. You can replace the linebreaks with the <br> element to show the enters in your browser too.
Mind that any subsequent spaces are also ignored. You can use the <pre> element too, to display the exact text, but I think that long lines won't be wrapped in that case.
If you are putting your text into HTML try this just before writing it out to the page:
string myText = "" // your text
myText = myText.Replace(Environment.NewLine, "<br/>");
this worked:
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))

Categories

Resources