Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rich Text Box padding between text and border

Tags:

c#

winforms

Is it possible to add padding into a Rich Text Box control between the text and the border?

I tried docking a rich text box inside of a panel, with its padding for all four side set to 10 and that accomplished what I wanted. Except once the vertical scroll bar for the rich text box is needed that gets padded as well.

like image 410
Keith Maurino Avatar asked May 26 '10 14:05

Keith Maurino


2 Answers

The RichTextBox has no padding property.

Quick and dirty padding can be achieved by putting the RichTextBox in a Panel, which has the same BackColor property as the RichTextBox (usually Color.White).

Then, set the Dock property of the RichTextBox to Fill, and play with the Padding properties of the Panel control.

like image 117
Larry Avatar answered Sep 20 '22 18:09

Larry


There are EM_GETRECT and EM_SETRECT.

Combining those two together, you can make this:

enter image description here

…look like this:

enter image description here

I've written a small C# extension class to wrap this all up.

Usage example:

const int dist = 24; richTextBox1.SetInnerMargins(dist, dist, dist, 0); 

This sets the inner margins left, top and right to 24, leaving the bottom as zero.

Please note that when scrolling, the top margin stays as being set, giving something like this:

enter image description here

Personally, this looks "unnatural" to me. I would prefer that when scrolling the top margin becomes zero, too.

Maybe there is a workaround for that…

Full source code

As of request:

public static class RichTextBoxExtensions {     public static void SetInnerMargins(this TextBoxBase textBox, int left, int top, int right, int bottom)     {         var rect = textBox.GetFormattingRect();          var newRect = new Rectangle(left, top, rect.Width - left - right, rect.Height - top - bottom);         textBox.SetFormattingRect(newRect);     }      [StructLayout(LayoutKind.Sequential)]     private struct RECT     {         public readonly int Left;         public readonly int Top;         public readonly int Right;         public readonly int Bottom;          private RECT(int left, int top, int right, int bottom)         {             Left = left;             Top = top;             Right = right;             Bottom = bottom;         }          public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)         {         }     }      [DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]     private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect);      [DllImport(@"user32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)]     private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);      private const int EmGetrect = 0xB2;     private const int EmSetrect = 0xB3;      private static void SetFormattingRect(this TextBoxBase textbox, Rectangle rect)     {         var rc = new RECT(rect);         SendMessageRefRect(textbox.Handle, EmSetrect, 0, ref rc);     }      private static Rectangle GetFormattingRect(this TextBoxBase textbox)     {         var rect = new Rectangle();         SendMessage(textbox.Handle, EmGetrect, (IntPtr) 0, ref rect);         return rect;     } } 
like image 33
Uwe Keim Avatar answered Sep 16 '22 18:09

Uwe Keim