Share via


how to set top margin in rich text box?

Question

Sunday, November 6, 2016 4:53 AM

I am developing wordpad similar application. I need to set top margin in the rich text box. I wondered there is no such option for margin. I need to set top  margin in the rich text box. I could able to set the indent left and right. But stuck with top and bottom margin.

All replies (5)

Sunday, November 6, 2016 5:19 AM

I am developing wordpad similar application. I need to set top margin in the rich text box. I wondered there is no such option for margin. I need to set top  margin in the rich text box. I could able to set the indent left and right. But stuck with top and bottom margin.

There is no top margin in a Windows.Forms RichTextBox.

I've read where people place a RichTextBox onto a Panel with the Panels back color set to the RichTextBox's back color. And set the RichTextBox's border style to None. Then set the RichTextBox's size to whatever size it needs to be to appear to have left, right, top and bottom margins. However I suspect if any of the scrollbars display in the RichTextBox that it will not visually appear very attractive.

Trying to setup paging, if necessary so that separate pages are used for a specific number of lines, may be difficult also.

La vida loca


Sunday, November 6, 2016 7:23 AM

private void AdjustTextBoxRMargin()
{
    richTextBox1.RightMargin = richTextBox1.Size.Width - 35;
}This should work. Modify it according to your code

MD. ROKON-UZ-ZAMAN

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Sunday, November 6, 2016 8:11 AM

This code is setting right margin. I achieved right margin using code

richtextbox1.selectionRightMargin=30;

My requirement is to set a top margin.


Monday, November 7, 2016 4:52 AM

Please read Uwe Keim 's   answer from that URL. 

http://stackoverflow.com/questions/2914004/rich-text-box-padding-between-text-and-border   

You will get extension class code from http://pastebin.com/FnEGqWxX  

Hope that will solve your problem

MD. ROKON-UZ-ZAMAN

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, November 8, 2016 3:09 AM

Hi 444444Diya,

Thank you for posting here.

For your question, please try the following code.

In designer. I used button1 to set the margin and use button2 to get the margin.

Here is the code. 

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.Threading.Tasks;
using System.Windows.Forms;

namespace top_margin_of_richtextBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rect lParam);//

        private const int EM_GETRECT = 0x00b2;
        private const int EM_SETRECT = 0x00b3;

        public Rect RichTextBoxMargin
        {
            get
            {
                Rect rect = new Rect();
                SendMessage(richTextBox1.Handle, EM_GETRECT, IntPtr.Zero, ref rect);
                rect.Left += 1;
                rect.Top += 1;
                rect.Right = 1 + richTextBox1.ClientSize.Width - rect.Right;
                rect.Bottom = richTextBox1.ClientSize.Height - rect.Bottom;
                return rect;
            }
            set
            {
                Rect rect;
                rect.Left = richTextBox1.ClientRectangle.Left + value.Left;
                rect.Top = richTextBox1.ClientRectangle.Top + value.Top;
                rect.Right = richTextBox1.ClientRectangle.Right - value.Right;
                rect.Bottom = richTextBox1.ClientRectangle.Bottom - value.Bottom;

                SendMessage(richTextBox1.Handle, EM_SETRECT, IntPtr.Zero, ref rect);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Rect rect;
            rect.Left = 50;
            rect.Top = 50;
            rect.Right = 50;
            rect.Bottom = 50;

            RichTextBoxMargin = rect;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Rect rect;
            rect = RichTextBoxMargin;

            MessageBox.Show(string.Format("Left = {0} top={1} right={2} bottom={3}", rect.Left, rect.Top, rect.Right, rect.Bottom));
        }
    }
}

Here is the output.

1. input string

2. click button1 to set the margin

3. click button2 to get the margin.

I hope this would be helpful to you.

If you have something else, please feel free to contact us.

Best Regard,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].