I am creating this program that requires recalculating the width of a RichTextBox control. The problem is that he continues to count incorrectly. I used the correct formulas to convert a point to inches, then inches to pixels, and then pixels to multiply the 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);
}
}
source
share