Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem. I need to modify the line spaceing in a richtextbox. I'm programing in C#.

Thanks in advance!
Posted
Updated 25-May-11 2:41am
v2
Comments
Manfred Rudolf Bihy 26-May-11 4:34am    
Any news on how you are coming along?

Here's some code you can experiment with. The form contains one RichTexBox control and six button plus one TextBox control. Momentarily the whole text is being selected so the spacing affects all lines, but you can change that yourself if you want. The parameters that are of interest here are:
- dyLineSpacing
- bLineSpacingRule
which live in struct PARAFORMAT. Please read the MSDN documentation for a thorough explanation on how these two play together to control linespacing: http://msdn.microsoft.com/en-us/library/bb787942(v=vs.85).aspx[^].

Here is the complete Form code (you need to construct the form yourself with the controls I mentioned at the beginning):

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace RichTextBoxLineSpacing
{
    public partial class Form1 : Form
    {
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT lParam);

        const int PFM_SPACEBEFORE = 0x00000040;
        const int PFM_SPACEAFTER  = 0x00000080;
        const int PFM_LINESPACING = 0x00000100;

        const int SCF_SELECTION = 1;
        const int EM_SETPARAFORMAT = 1095;

        public Form1()
        {
            InitializeComponent();
        }

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

        }

        private void button1_Click(object sender, EventArgs e)
        {
            setLineFormat(0, space);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            setLineFormat(1, space);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            setLineFormat(2, space);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            setLineFormat(3, space);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            setLineFormat(4, space);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            setLineFormat(5, space);
        }

        int space = 0;
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            String val = textBox1.Text;
            bool success = int.TryParse(val, out space);
            if (success)
            {
                textBox1.BackColor = Color.White;
            }
            else
            {
                textBox1.BackColor = Color.Red;
            }
        }
    }

    [StructLayout( LayoutKind.Sequential )]
    public struct PARAFORMAT
    {
        public int cbSize;
        public uint dwMask;
        public short wNumbering;
        public short wReserved;
        public int dxStartIndent;
        public int dxRightIndent;
        public int dxOffset;
        public short wAlignment;
        public short cTabCount;
        [MarshalAs( UnmanagedType.ByValArray, SizeConst = 32 )]
        public int[] rgxTabs;
        // PARAFORMAT2 from here onwards
        public int dySpaceBefore;
        public int dySpaceAfter;
        public int dyLineSpacing;
        public short sStyle;
        public byte bLineSpacingRule;
        public byte bOutlineLevel;
        public short wShadingWeight;
        public short wShadingStyle;
        public short wNumberingStart;
        public short wNumberingStyle;
        public short wNumberingTab;
        public short wBorderSpace;
        public short wBorderWidth;
        public short wBorders;
    }
}


The code was tested in VisualStudio 2010 in a Windows Forms project.

If you have any doubts please leave a comment to this solution.

Happy coding!

-MRB
 
Share this answer
 
setLineFormat
has some error
??
 
Share this answer
 
Comments
[no name] 13-Oct-12 9:40am    
How exactly is this an answer to this over year old question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900