Creating a newline in rich text box - c#

I need help on creating a new line for my RichTextBox which I cant make work when using CheckBox.
It keeps overlapping instead of creating a newline of words.
Tried using the method of rtbdisplay.text = (display+envrionment.newline);
example from my code:
if (rbtnSmall.Checked == true)
{
rtbDisplay.Text = "displaytext".PadRight(20) + "size".PadRight(23) +
qty.ToString().PadRight(20) + StrongDummy;
}

Use the RichTextBox.Text property or the RichtTextBox.AppendText method to append a string with a newline.
myRichTextBox.Text += Environment.NewLine + "My new line.";
// Or
myRichTextBox.AppendText( Environment.NewLine + "My new line." );

You can use c# Environment.NewLine Property as described in http://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx. Or, the "old style" like #"\r\n".
Rgds,

Related

how to replace a selected text in a TextBox with a string?

I want to replace the selected text in a TextBox with another string in c# . I am using the following code but it replaces all the same texts in all the TextBox (not only my selected text). How may I solve this?
string selectedTxt = TextBox1.SelectedText;
TextBox1.Text = TextBox1.Text.Replace(selectedTxt, "<b>" + selectedTxt + "</b>");
No need to replace, just set the SelectedText property with new text. Explanation here
string selectedTxt = TextBox1.SelectedText;
TextBox1.SelectedText = "<b>" + selectedTxt + "</b>";

Using Windows forms c# message box

How will I get a messagebox "Or another way of doing this" to have multiple lines?
I've got a messageBox to appear when the user presses F1 and I need it to have a sort of list:
Product Name = Alphanumeric + Special Characters.
Quantity = Maximum 100.
Price = Must be Numeric.
Etc
Thanks.
You can use \r\n or Environment.NewLine at the end of each line to be shown or you can use the StringBuilder class:
var message = new StringBuilder();
message.AppendLine("Product Name = Alphanumeric + Special Characters.");
message.AppendLine("Quantity = Maximum 100.");
message.AppendLine("Price = Must be Numeric.");
MessageBox.Show(message.ToString());
Add/concatenate Environment.Newline to your string.
Put "\r\n" at the end of each line of the message.
so, for example:
var message = "Product Name = MyProduct.\r\n";
message += "Quantity = Maximum 100.\r\n";
message += "Price = Must be Numeric.\r\n";
MessageBox.Show(message);
Just use Environment.NewLine where you want a newline to appear. For example:
string message = "Line 1" + Environment.NewLine + "Line 2";
MessageBox.Show(message);
This will output:
Line 1
Line 2

adding space in texbox results when sending thru email

wassup guys im new to C# coding and i did a search but couldn't find exactly what im looking for. So i have a couple of text-boxes which holds string elements and integers
what i want to do is when these boxes are filled in i want to send a summary of the email to client/customer but the format is whats getting me.
(first, one) are strings equaling different text-boxes
my code is:
emailCompseTask.Body = first + one + Enviroment.NewLine +
second + two + Enviroment.NewLine
and so on problem is which i send thru email it shows something like this:
computer service25.00
instead of:
computer service 25.00
is there a way to add spacing to make this more presentable? or even a better way perhaps thanks in advance guys
try this :
emailCompseTask.Body = first + one + " "+ second + two ;
body takes as HTML input, check here for more spacing option.
I'm a bit confused, but you just want to add some spacing in the output? Just throw some spaces in there like you would another variable.
first + " " + one + Environment.NewLine
+ second + " " + two + Environment.NewLine;
You can use a table
string tableRow = #"<tr>
<td>{0}</td>
<td>{1}</td>
</tr>";
string htmlTable = #"<table>
{0}
</table>";
string rows = "";
// Can do this in a loop
rows += string.Format(tableRow, first, one);
rows += string.Format(tableRow, first, one);
emailComseTask.Body = string.Format(htmlTable, rows);

How to Show New Line command in text box?

Hi i am doing a small project in C#, and i need to know what commands are comming from input source so i can do my work accordingly..
here is example...
textBox1.Text = "First line \nSecond line";
richTextBox1.Text = "First line \nSecond line";
Richtextbox shows this output:
First line
Second line
Textbox show this output:
First line Second line
please tell me how to show new line "\n" or return "\r" or similar input as a character output in text or richtextbox. so i can know that newline command is coming from input data.
for example text or richtext box will show this output.
First line \nSecond line
thankx in advance.
Lets say that i have a string which have new line :
string st = "my name is DK" + Environment.NewLine + "Also that means it's my name";
Now that i want to show that there is new line in my text there you go :
textBox1.Text = st.Replace(Environment.NewLine, "%");
This will show the newline chat with % sign
For winforms application set
this.textBox1.Multiline = true;
and use "\r\n" as
textBox1.Text = "First line \r\nSecond line";
You want to either prefix your string with # or you can use a double slash before each n (\n). Both of these are ways of escaping the \ so that it displays instead of being treated as part of a new line.
#"This will show verbatim\n";
"This will show verbatim\\n";
You can utilize this by performing a Replace on your incoming text
richTextBox1.Text = richTextBox1.Text.Replace("\n", "\n\\n");
richTextBox1.Text = richTextBox1.Text.Replace("\r\n", "\r\n\\n");
In the replace, I left the original linebreak so that it will be there, just followed by the displaying version. You can take those out if you dont want that. :)
Use the combination "\r\n" (or Environment.NewLine constant which contains just that).
Change Your text box property from single line to Multiline then it will change to new line
TextBox1.TextMode = TextBoxMode.MultiLine;
TextBox1.Text = "First line \n New line ";
TextBoxt1.Text ="FirstLine \r\n SecondLine";

How can I format C# string output to contain newlines?

strEmail =
"Hi All,"
+ "<br>"
+ "The User "
+ lstDataSender[0].ReturnDataset.Tables[0].Rows[0][1].ToString()
+ " has been created on "
+ DateTime.Now.ToShortDateString()
+ "."
I am writing C# code to generate an email whenever a new user has been created. I need the body of the mail in a mail format like
hi,
The user "xxx" has been created on "todaysdate".
Thanks,
yyyy
so I need to insert linebreaks and bold for some characters. How might I achieve this?
If this is a plain text email (which it looks like it is), use \r\n for new lines.
strEmail = string.Concat("Hi All,\r\n\r\nThe User",
lstDataSender[0].ReturnDataset.Tables[0].Rows[0][1].ToString(),
"has been created on ",
DateTime.Now.ToShortDateString(),
".\r\n\r\nThanks, yyyy");
Strickly speaking this should be Environment.NewLine to support different platforms, but I doubt this is a concern.
Set
yourMessage.IsBodyHtml = true
Msdn-Reference can be found here.
In order to form, you can use like below:
Response.Write("Hi,<br/><br/>The user.......");

Categories

Resources