Join 107,398 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,198 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
this will move your string to the next line while leaving an empty line
csharp
txtBox.Text = "Hi World \n\nSpaces line added";
Ok that helps but how would I use that for user input. What I mean is when a user types a line of text then the next line will be double spaced. How would I do that?
The only way I can think of off the top of my head would be to create a custom control that inherited from the RichTextBox (or extends it's functionality), something along these lines.
this will move your string to the next line while leaving an empty line
csharp
txtBox.Text = "Hi World \n\nSpaces line added";
Ok that helps but how would I use that for user input. What I mean is when a user types a line of text then the next line will be double spaced. How would I do that?
The following code works. When the user types something in the textbox and then clicks a button, whatever they typed in the textbox is added to the richTextBox, and it comes out double spaced. They must click the button at the end of each line of text in order for this to work though. You could probably modify it so when they press enter on the keyboard it double spaces instead of them having to click the button.
csharp
namespace RichTextBox { public partial class Form1 : Form { public Form1() { InitializeComponent();
It's called richtext for a reason. There should be a way to take advantage of the native format! And, indeed, there is.
Try this before you do anything.
CODE
richTextBox1.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" + "\n" + @"\viewkind4\uc1\pard\sl480\slmult1\f0\fs24 \par" + "\n" + @"}";
This sets up text to be double spaced. The flag for this is "\sl480", however, writing rtf by hand is error prone. To get the prior line I loaded up MS Word, chose the formatting I wanted, saved it in rtf. I then loaded it into the control, it worked, and saved it out again, e.g.
I then opened up the results in notepad and there you have it. If you do richTextBox1.Text = ""; You'll wipe out your formatting. Here's an example of usage.
CODE
richTextBox1.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" + "\n" + @"\viewkind4\uc1\pard\sl480\slmult1\f0\fs24 \par" + "\n" + @"}"; richTextBox1.SelectedText = "There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here,"; richTextBox1.SelectedText = " it will instantly disappear and be replaced by something even more bizarre and inexplicable."; richTextBox1.SelectedText = " There is another theory which states that this has already happened.";
It's called richtext for a reason. There should be a way to take advantage of the native format! And, indeed, there is.
Try this before you do anything.
CODE
richTextBox1.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" + "\n" + @"\viewkind4\uc1\pard\sl480\slmult1\f0\fs24 \par" + "\n" + @"}";
This sets up text to be double spaced. The flag for this is "\sl480", however, writing rtf by hand is error prone. To get the prior line I loaded up MS Word, chose the formatting I wanted, saved it in rtf. I then loaded it into the control, it worked, and saved it out again, e.g.
I then opened up the results in notepad and there you have it. If you do richTextBox1.Text = ""; You'll wipe out your formatting. Here's an example of usage.
CODE
richTextBox1.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" + "\n" + @"\viewkind4\uc1\pard\sl480\slmult1\f0\fs24 \par" + "\n" + @"}"; richTextBox1.SelectedText = "There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here,"; richTextBox1.SelectedText = " it will instantly disappear and be replaced by something even more bizarre and inexplicable."; richTextBox1.SelectedText = " There is another theory which states that this has already happened.";
Hope this helps.
I don't understand what you are telling me. Please explain better if you can.
Ok I have figured out how to double space when the user presses the enter key. Here is my code.
I don't understand what you are telling me. Please explain better if you can.
I'm not sure how to make it more simple, but I'll give it one more go.
You're using a RichTextBox control, yes? The kind that supports formatting, things like bold and different font colors? Well, in the guts of that control, it actually supports more complex formatting options and you can advantage of that to make the control do double space without any other code.
Here's how to do it from scratch.
1. Create a C# windows project. 2. Into the form drag and drop a RichTextBox control. By default it will be named richTextBox1. 3. Double click on the title of the form. This will cause a Form Load event to be created for you. 4. In the form load event, place the code previously offered. In a new project, your source should look like this:
csharp
public partial class Form1 : Form { public Form1() { InitializeComponent(); }
Your way works perfectly. How in the world do you remember all that?
In case anyone's interested, if you want to let the user type into a different textbox and add that text to a richTextBox, this code will do it, and double space it. Whatever they type in the first textBox will be added to the richTextBox whenver they press enter. Baavgai's way is much better, though.
csharp
private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter)//do this when they click enter { if (textBox1.Text != "")//if there's text in textbox, add it and 2 new lines { richTextBox1.SelectedText += textBox1.Text + "\n\n"; } else//if no text in textbox, just add 2 new lines when they press enter { richTextBox1.SelectedText += "\n\n"; } //either way erase the current text in textbox so they can type another line textBox1.Text = "";
} }
This post has been edited by OliveOyl3471: 8 Jul, 2008 - 05:05 PM
I don't understand what you are telling me. Please explain better if you can.
I'm not sure how to make it more simple, but I'll give it one more go.
You're using a RichTextBox control, yes? The kind that supports formatting, things like bold and different font colors? Well, in the guts of that control, it actually supports more complex formatting options and you can advantage of that to make the control do double space without any other code.
Here's how to do it from scratch.
1. Create a C# windows project. 2. Into the form drag and drop a RichTextBox control. By default it will be named richTextBox1. 3. Double click on the title of the form. This will cause a Form Load event to be created for you. 4. In the form load event, place the code previously offered. In a new project, your source should look like this:
csharp
public partial class Form1 : Form { public Form1() { InitializeComponent(); }
That's it, you're done, the control now supports double space. Run the form, type in the control to test.
Thank you so much it works! Can you help me with one more thing? If so...
How can you make some margins like in Word. I have tried using the margins(margins:10,10,10,10) it doesn't do anything, I do not see any margins. Can you help?
This post has been edited by gbertoli3: 8 Jul, 2008 - 04:01 PM
How can you make some margins like in Word. I have tried using the margins(margins:10,10,10,10) it doesn't do anything, I do not see any margins. Can you help?
I'm afraid the control implements as much, or as little, of rtf as it feels like. Also, I'm not entirely sure margins have any meaning on a form. When you went to print, sure...
If you want it to look like it has margins in your form, just place the richTextControl inside a panel with the same background. Center the control in the panel and define the panel's padding to give you a "margin". For printing, you'll have to mess with some other stuff that should allow you to set margins there.