2011-03-05 55 views
3

我正在創建此程序,需要重新計算RichTextBox控件的寬度。問題是它不斷重新計算錯誤。我使用正確的公式將點轉換爲英寸,然後英寸轉換爲像素,然後使用像素乘以字符數來獲得寬度。以下代碼如下:有問題重新計算RichTextBox控件的寬度

 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); 


      } 
     } 

回答

1

您是否嘗試過查看Graphics.MeasureString方法?

+0

謝謝,我不知道子程序存在。它使我的生活更加簡單。 – 2011-03-05 03:02:55

+0

很高興能有所幫助 – 2011-03-05 03:05:53