Welcome to Dream.In.Code
Getting C# Help is Easy!

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!



Double Spacing

2 Pages V  1 2 >  
Reply to this topicStart new topic

Double Spacing, Help with double spacing in richTextBox

gbertoli3
post 6 Jul, 2008 - 06:41 PM
Post #1


D.I.C Regular

Group Icon
Joined: 23 Jun, 2008
Posts: 455



Thanked 4 times

Dream Kudos: 625
My Contributions


The only code I can think of for double spacing in a richTextBox control would be...

CODE

txtBox.Lines = txtBox.Lines + 1;


I know that is not even close to what it would be, but that is as close as I can get.

Please Help!

**Please Show Code and Explain** **Thanks**
User is offlineProfile CardPM

Go to the top of the page


modi123_1
post 7 Jul, 2008 - 11:18 AM
Post #2


D.I.C Regular

Group Icon
Joined: 12 Jun, 2008
Posts: 267



Thanked 5 times

Dream Kudos: 100
My Contributions


QUOTE(gbertoli3 @ 6 Jul, 2008 - 08:41 PM) *

The only code I can think of for double spacing in a richTextBox control would be...

CODE

txtBox.Lines = txtBox.Lines + 1;


I know that is not even close to what it would be, but that is as close as I can get.

Please Help!

**Please Show Code and Explain** **Thanks**


Simply put, you don't. Without extending the text box and some updates to the drawing you are stuck with single spaced lines.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 7 Jul, 2008 - 11:31 AM
Post #3


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 370



Thanked 4 times

Dream Kudos: 175
My Contributions


this will move your string to the next line while leaving an empty line
csharp

txtBox.Text = "Hi World \n\nSpaces line added";
User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 7 Jul, 2008 - 05:45 PM
Post #4


D.I.C Regular

Group Icon
Joined: 23 Jun, 2008
Posts: 455



Thanked 4 times

Dream Kudos: 625
My Contributions


QUOTE(zakary @ 7 Jul, 2008 - 11:31 AM) *

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?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 7 Jul, 2008 - 05:51 PM
Post #5


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,135



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


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.

Hope that helps smile.gif
User is online!Profile CardPM

Go to the top of the page

OliveOyl3471
post 7 Jul, 2008 - 08:23 PM
Post #6


D.I.C Addict

****
Joined: 11 Jul, 2007
Posts: 562



Thanked 3 times
My Contributions


QUOTE(gbertoli3 @ 7 Jul, 2008 - 07:45 PM) *

QUOTE(zakary @ 7 Jul, 2008 - 11:31 AM) *

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();

}

private void button1_Click(object sender, EventArgs e)
{

richTextBox1.Text += textBox1.Text + "\n\n";
}
}
}


This post has been edited by OliveOyl3471: 7 Jul, 2008 - 08:24 PM
User is online!Profile CardPM

Go to the top of the page

baavgai
post 8 Jul, 2008 - 08:13 AM
Post #7


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,524



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


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.

CODE

richTextBox1.LoadFile("foo.rtf");
richTextBox1.SaveFile("dump.rtf", RichTextBoxStreamType.RichText);


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.
User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 8 Jul, 2008 - 12:19 PM
Post #8


D.I.C Regular

Group Icon
Joined: 23 Jun, 2008
Posts: 455



Thanked 4 times

Dream Kudos: 625
My Contributions


QUOTE(baavgai @ 8 Jul, 2008 - 08:13 AM) *

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.

CODE

richTextBox1.LoadFile("foo.rtf");
richTextBox1.SaveFile("dump.rtf", RichTextBoxStreamType.RichText);


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.
CODE

private void txtBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtBox.SelectedText = "\n";
            }
        }


Now what I am trying to do is figure out when the user has reached the end of the line.
Something like...
CODE

if (txtBox.Text.Length == txtBox.Lines.Length)
            {
                MessageBox.Show("You have reached the end of the line!");
            }


...but that doesn't work, so can anyone help with this?
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 8 Jul, 2008 - 01:16 PM
Post #9


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,524



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(gbertoli3 @ 8 Jul, 2008 - 03:19 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();
}

private void Form1_Load(object sender, EventArgs e) {
this.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" + @"}";
}
}


That's it, you're done, the control now supports double space. Run the form, type in the control to test.

User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 8 Jul, 2008 - 02:39 PM
Post #10


D.I.C Addict

****
Joined: 11 Jul, 2007
Posts: 562



Thanked 3 times
My Contributions


baavgai, you are so smart. smile.gif

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
User is online!Profile CardPM

Go to the top of the page

gbertoli3
post 8 Jul, 2008 - 03:43 PM
Post #11


D.I.C Regular

Group Icon
Joined: 23 Jun, 2008
Posts: 455



Thanked 4 times

Dream Kudos: 625
My Contributions


QUOTE(baavgai @ 8 Jul, 2008 - 01:16 PM) *

QUOTE(gbertoli3 @ 8 Jul, 2008 - 03:19 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();
}

private void Form1_Load(object sender, EventArgs e) {
this.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" + @"}";
}
}


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
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 9 Jul, 2008 - 04:28 AM
Post #12


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,524



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(gbertoli3 @ 8 Jul, 2008 - 06:43 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. tongue.gif 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.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 8/28/08 04:12PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month