Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size RichTextBox according to contents

Tags:

c#

winforms

This code automatically sizes a RichTextBox according to it's contents. I'm having issues, especially with tables. \t may be ignored. I tried a managed solution, now I'm trying platform invoke. Current Output:

enter image description here

    [DllImport("gdi32.dll")]
    static extern bool GetTextExtentPoint32(IntPtr hdc, string lpString, int cbString, out SIZE lpSize);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetDC(IntPtr hWnd);

    [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct SIZE
    {
        public int cx;
        public int cy;

        public SIZE(int cx, int cy)
        {
            this.cx = cx;
            this.cy = cy;
        }
    }

    public static void Main()
    {
        Form form = new Form();
        RichTextBox rtfBox = new RichTextBox();

        rtfBox.Rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil Arial;}}\viewkind4\uc1\trowd\trgaph100\cellx1000\cellx2000\pard\intbl\lang1033\f0\fs20  hi\cell  bye\cell\row\intbl  one\cell  two\cell\row\pard\par}";
        rtfBox.ScrollBars = RichTextBoxScrollBars.None;

        string sInput = "hi\t bye\t\n";// one\t two\t\n";
        SIZE CharSize;

        form.Controls.Add(rtfBox);

        IntPtr hdc = GetDC(IntPtr.Zero);//Context for entire screen
        GetTextExtentPoint32(hdc, sInput, sInput.Length, out CharSize);

        rtfBox.Width = CharSize.cx;
        rtfBox.Height = CharSize.cy;

        form.Visible = false;

        form.ShowDialog();
    }

(Note, for simplicity this is a console application with a reference to System.Windows.Forms.dll)

like image 292
P.Brian.Mackey Avatar asked Aug 10 '12 18:08

P.Brian.Mackey


People also ask

How to set the size of the RichTextBox?

You can set this property in two different ways: 1. Design-Time: It is the easiest way to set the size of the RichTextBox as shown in the following steps: Step 2: Next, drag and drop the RichTextBox control from the toolbox to the form as shown in the below image:

How to add RichTextBox to a form in Java?

Step 1: Create a RichTextBox using the RichTextBox () constructor is provided by the RichTextBox class. Step 2: After creating RichTextBox, set the Font property of the RichTextBox provided by the RichTextBox class. Step 3: And last add this RichTextBox control to the form using Add () method.

What is RichTextBox in Salesforce?

Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc. In RichTextBox, you are allowed to set the font of the content present in the RichTextBox control.

Does word wrap work if you turn off rich text box?

@HansPassant: does it work if you turn word wrap off on the rich text box? No. Not sure why you see something else. @HansPassant: it's a mystery to me as well!


1 Answers

Have you looked at the ContentsResized event? Add the following method to be called when the event fires:

private void richTextBox_ContentsResized(object sender, ContentsResizedEventArgs e)
{
    var richTextBox = (RichTextBox) sender;
    richTextBox.Width = e.NewRectangle.Width;
    richTextBox.Height = e.NewRectangle.Height;
}

When the RTF content is changed (using Rtf), the RichTextBox should be resized to match its contents. Make sure you also set the WordWrap property to false.


I've tried it with your table example and it does appear to work (albeit with a little offset, which you could possibly solve by adding a few pixels of width to the adjusted size - not sure why that happens):

P.Brian.Mackey EDIT
This answer worked for me. To clarify, here's the final code including border fix:

    public static void Main()
    {
        string sInput = "hi\t bye\t\n";// one\t two\t\n";
        SIZE CharSize;
        Form form = new Form();
        RichTextBox rtfBox = new RichTextBox();
        rtfBox.ContentsResized += (object sender, ContentsResizedEventArgs e) =>
        {
            var richTextBox = (RichTextBox)sender;
            richTextBox.Width = e.NewRectangle.Width;
            richTextBox.Height = e.NewRectangle.Height;
            rtfBox.Width += rtfBox.Margin.Horizontal + SystemInformation.HorizontalResizeBorderThickness;
        };

        rtfBox.WordWrap = false;
        rtfBox.ScrollBars = RichTextBoxScrollBars.None;

        rtfBox.Rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil Arial;}}\viewkind4\uc1\trowd\trgaph100\cellx1000\cellx2000\pard\intbl\lang1033\f0\fs20  hi\cell  bye\cell\row\intbl  one\cell  two\cell\row\pard\par}";

        form.Controls.Add(rtfBox);
        form.ShowDialog();
    }
like image 137
adrianbanks Avatar answered Oct 19 '22 00:10

adrianbanks