Daniel Lopez
Daniel Lopez

Reputation: 1798

Having Trouble Recalculate Width Of RichTextBox Control

I am creating this program that requires the recalculation of a RichTextBox control's width. The problem is that it keeps recalculating wrong. I used the correct formulas to convert point to inches, then inches to pixels and then pixels times number of characters to get the width. Here is the code below:

        foreach (RichTextBox r in panel1.Controls)
        {


            if (r.DisplayRectangle.IntersectsWith(contextMenuStrip1.DisplayRectangle))
            {

                Graphics gfx = this.CreateGraphics();
                float dpix = gfx.DpiX;
                float dpiy = gfx.DpiY;
                int numofchars =  r.TextLength;
                float point = r.Font.SizeInPoints;
                float inches = point / 72;
                float pixels = dpiy * inches;
                float width = (pixels * numofchars);
                r.Width = Convert.ToInt32(width);


            }
        }

Upvotes: 3

Views: 411

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

Have you tried looking into the Graphics.MeasureString Method?

Upvotes: 1

Related Questions