Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a windows application in c#.net

I have a richtextbox in which I display rtf files;

But it displays near border of rich textbox

I cannot give padding between text and border

So please help me
Posted
Updated 28-Sep-20 4:54am
v3

You have stumbled upon the surprising limitations of the MS supplied RichTextBox (RTB) Control: even though the RTF standard supports embedded margin codes, you have no way to easily set them. You'll have to hack the RTB one way or another to effectively set variable document margins. And, as you found out, Padding is not implemented for the RTB.

However, there are two available Properties of the RTB that do have the effect of (kind of) giving you margins:

1. RightMargin (integer) property determines the line length. It's set in pixels. If your RTB is 500 pixels wide, and you set RightMargin to 480, you should get a 20 pixel wide right margin.

2. The ShowSelectionMargin (boolean) property if set to 'true will expose a small left margin in the RTB. Clicking in this area will select the entire line. You have no control over how wide this margin is.

A frequent Win Forms ui hack to simulate RTB margins is:

1. embed the RTB Control in a Panel, or other ContainerControl. set the RTB DockStyle to 'DockStyle.Fill

2. set the Padding parameters of the Panel to simulate margins.

3. set the BackColor of both Panel and RTB to the same color.

4. set the BorderStyle of the RTB to 'None.
 
Share this answer
 
Comments
Karthik_Mahalingam 13-Dec-13 7:39am    
I tried from the existing properties of RTB, no way to achieve it..
excellent answer Bill.
nice trick to cheat the UI issues :-)
my 5 for you..
El problema es si tenes barra de desplazamiento, como solucinaria eso?
 
Share this answer
 
Comments
Kats2512 26-Mar-19 2:59am    
Por favor habla en ingles
Richard Deeming 26-Mar-19 15:58pm    
No publique su pregunta como una solución a la pregunta de otra persona.

ASK A QUESTION[^]. In English. With a lot more detail than you have posted here.
Here is a simple solution for setting the left and right margins of a RichTextBox that worked well for me in a C# Windows app. It sets both the right and left margins to 4 pixels.

C#
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam);

// both left and right margins set to 4 pixels with the last parm: 0x00040004
SendMessage(richtextbox1.Handle, 0xd3, (UIntPtr)0x3, (IntPtr)0x00040004);

I tried to make the SendMessage more proper below:

C#
const int EM_SETMARGINS = 0xd3;
const int EC_LEFTMARGIN = 0x1;
const int EC_RIGHTMARGIN = 0x2;

SendMessage(richtextbox1.Handle, EM_SETMARGINS,
 (UIntPtr)(EC_LEFTMARGIN | EC_RIGHTMARGIN), (IntPtr)0x00040004);

I figure this solution was not possible when the question was originally posted.
 
Share this answer
 
v2

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