2017-07-19 308 views
0

如何在報告的文本框控件中動態設置字體大小,以便文本框的內容符合其維度? 我的想法是基於未來功能的價值在循環減小字體大小:如何在c#local report(rdlc - WinForms)中動態設置字體大小以適應文本框的尺寸?

Private Function IsContentsBiggerThanTextBox(ByVal textBox As TextBox) As Boolean 
    Dim size As Size = TextRenderer.MeasureText(textBox.Text, textBox.Font) 
    Return ((size.Width > textBox.Width) OR (size.Height > textBox.Height)) 
End Function 

的問題是我不知道怎麼打發報告文本框的參考作用。

回答

0

經過多次嘗試查找可在本地rdlc報告中工作的代碼,我最終設法編寫了可以動態調整文本框控件中字體大小的函數,具體取決於文本大小。我們的想法是用固定尺寸來測量文本框控件中的文本大小,如果我們發現文本高度高於文本框控件的高度,我們會減小字體的大小,直到文本適合控制。

首先,您必須在「報表屬性」中添加對System.Drawing的引用。

rdlc報告中的所有代碼必須寫入vb.net。在代碼報告屬性的字段中添加這些功能:

'1cm = 37.79527559055 pixels 
Const cmToPx as Single = 37.79527559055F 

Public Function setMaxFont(Text as string, boxWidth as Single, boxHeight as 
Single, FontMax as Integer, FontMin as Integer) as String 
    Dim i as Integer 
    For i = FontMax to FontMin Step -1 
     If IsTextSmaller(Text, i, boxWidth, boxHeight) Then 
      Exit For 
     End If 
    Next 
    return i & "pt" 
End Function 


Private Function IsTextSmaller(Text as String, fontValue as Integer, boxWidth as Single, boxHeight as Single) as Boolean 
    Dim stringFont As New System.Drawing.Font("Arial", fontValue) 
    Dim stringSize As New System.Drawing.SizeF 
    Dim boxSize as New System.Drawing.SizeF(boxWidth * cmToPx, boxHeight * cmToPx * 10) 'we set box height bigger than textbox that we check 
    Dim bitmap as System.Drawing.Bitmap = New System.Drawing.Bitmap(1, 1) 
    Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap) 
    g.PageUnit = System.Drawing.GraphicsUnit.Pixel 
    stringSize = g.MeasureString(Text, stringFont, boxSize) 
    bitmap = Nothing 
    return stringSize.Height < (boxHeight * cmToPx) 
End Function 

選擇文本框控件要動態,並在下面字號屬性附加代碼更改其字體大小:的文本框有

= Code.SetMaxFont(TextBox.Value, WidthOfTextBox, HeightOfTextBox, MaxFontSize, MinFontSize) 

寬度和高度以釐米輸入。 CanGrow和CanShrink屬性的文本框必須設置爲False。

相關問題