Click here to Skip to main content
Click here to Skip to main content

Changing the line spacing in a RichTextBox control

, 3 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Using SendMessage and the PARAFORMAT structure, one can easily change the line spacing in a RichTextBox control
I cooked up a small code sample that demonstrates how to easily change the line spacing of text in a RichTextBox control. This example was written by me as a solution to a question in Q&A[^]. To use the below code, you have to make a WinForm project and add six buttons, a RichTextBox control and normal TextBox to it.
 
Inside the structure PARAFORMAT, there are two relevant fields that play closely together: bLineSpacingRule defines how dyLineSpacing is supposed to be interpreted.
 

0

Single spacing. The dyLineSpacing member is ignored.

1

One-and-a-half spacing. The dyLineSpacing member is ignored.

2

Double spacing. The dyLineSpacing member is ignored.

3

The dyLineSpacing member specifies the spacing from one line to the next, in twips. However, if dyLineSpacing specifies a value that is less than single spacing, the control displays single-spaced text.

4

The dyLineSpacing member specifies the spacing from one line to the next, in twips. The control uses the exact spacing specified, even if dyLineSpacing specifies a value that is less than single spacing.

5

The value of dyLineSpacing / 20 is the spacing, in lines, from one line to the next. Thus, setting dyLineSpacing to 20 produces single-spaced text, 40 is double spaced, 60 is triple spaced, and so on.


 
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;
    }
}

License

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

About the Author

Manfred R. Bihy
Software Developer Atos Origin
Germany Germany
While teaching myself BASIC and assembler on a Commodore 64 in the early 80's I got seriously infected by a tough kind of programming virus. The disease turned chronic and has since then been diagonsed as incurable. So I thought I'd put my affliction to good use and studied computer science which, after some trial and error, finally fetched me a degree.
 
This is some of the ground I've covered over the last 20 years:
  • Database design & programming
  • Application programming
  • Systems programming
  • Web applications
  • Multimedia/HTPC platform development
  • eCommerce software platform
  • Design & development of FAST based search solutions
I will not engage in listing all the languages, frameworks and tools I've worked with. The list would be too long, most probably mainly meaningless and judging by what has been accumulated just over the last 2 years surely incomplete too.
There are of course some languages that deserve to be mentioned as they played a vital role in me becoming what I am now.
  • BASIC
  • Assembler (6502)
  • C
  • Scheme (Lisp dialect)
  • Java
  • C#
The last four years were spent designing and programming FAST based search solutions using FAST Enterprise Search Platform 5.1 & 5.3. Since MS has already announced that FAST ESP will not be further developed and will die by 2017/2018, we are now looking into other technologies like Lucene/Solr, Sinequa and FAST Sharepoint Search.

Comments and Discussions

 
Questionobservation on using this with FrameWork 4.0 PinpremiumBillWoodruff7-Jul-14 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by Manfred R. Bihy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid