Question

Hi well my problem is this I have a RichTextBox but i wanna add a "pretty" space after the paragraph, i found on the internet many examples but all examples change all the lines and not only the paragraph.

private void FormatRTB(byte rule, int space, int x)
    {
        PARAFORMAT fmt = new PARAFORMAT();
        fmt.cbSize = Marshal.SizeOf(fmt);
        fmt.dwMask = PFM_LINESPACING;
        fmt.dyLineSpacing = space;
        fmt.bLineSpacingRule = rule;
        richTextBox1.Select(x, 2);
        SendMessage(new HandleRef(richTextBox1, richTextBox1.Handle),
                     EM_SETPARAFORMAT,
                     SCF_SELECTION,
                     ref fmt
                   );
    }

Well i add this code and select ony the \n because after of "\n" start the paragraph and dosent works i dont if my logic is bad or i need to add more code

while (richTextBox1.Text.IndexOf("\n", k) > 0)
        {
            k = richTextBox1.Text.IndexOf("\n", k);
            setLineFormat(2, 0, k);
            k++;
        }

.

Was it helpful?

Solution

I know that there is already an accepted answer, but maybe this will help other people.

If you really want to add spacing before or after a paragraph in RichTextBox, there is a very simple and "native" (ie. no hacking) solution using PFM_SPACEBEFORE or PFM_SPACEAFTER. The code is quite similar to the first one you present.

The complete solution with a custom control is posted on http://dominicweb.eu/en/blog/various/winforms-richtextbox-with-paragraph-spacing-csharp/

OTHER TIPS

If you are sure that all the occurrences of "\n" are indeed a different paragraph, you can simply add spaces after it. You could use a simple loop as:

for (int i = 0; i < richTextBox1.Text.Length; i++)
{
    if (richTextBox1.Text[i] == '\n')
        richTextBox1.Text.Insert(i + 1, "    ");
}

Often though paragraphs are marked with both '\n' and '\r' so you may look for the \r instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top