Trim textbox contents with ellipsis in SSRS

By default, when the content of a text field in SSRS overflows the width of the text field, the text field will grow vertically to accommodate its contents. This feature can be disabled by setting the CanGrow property of the False text box.

However, this abruptly cuts off the contents, which is not ideal.

I am looking for a way to clearly show the user that the text is too wide to fit the text field. I used to use a simple expression to add the ellipsis "..." when the length of the text string was above some fixed number:

=Iif(Len(Fields!CustomerName.Value) > 25, 
     Left(Fields!CustomerName.Value,23) + "...", 
     Fields!CustomerName.Value)

But this will not work when customer names contain a mixture of uppercase letters, lowercase letters, punctuation marks and other things that make individual pixel pixel widths of characters highly variable.

Ideally, some property for the text field control will allow the report designer to add an ellipsis if the text does not fit into the text field.

Does anyone have any suggestions for a more elegant approach to this?

+4
source share
1 answer

Another solution I came up with is using VB.NET, in particular the TextRenderer.MeasureText () function.

To make this work, I added the following code to the report:

Public Function TextWidth(str As String) AS Double
    'Returns the width, in pixels, of a string, assuming Tahoma size 8.
    Dim size As System.Drawing.SizeF
    Dim font As New system.Drawing.Font("Tahoma", 8)
    size = System.Windows.Forms.TextRenderer.MeasureText(str, font)
    TextWidth = size.Width
End Function

Public Function TextCap(str As String, maxWidth As Integer, Optional suffix As String = "") As String
    'Truncates a string to fit within maxWidth pixels, optionally adding a suffix string if
    'any characters were truncated.

    Dim w As Integer, l As Integer
    l = Len(str)
    w = TextWidth(str)
    For i As Integer = 1 To 10
        If (w > maxWidth) Then
            l = (l * maxWidth / w)
            If (l < 0) Then
                l = 0
                Exit For
            End If
            w = TextWidth(Left(str, l) + suffix)
        Else
            Exit For
        End If
    Next i

    If l < Len(str) Then
        TextCap = Left(str, l) + suffix
    Else
        TextCap = Left(str, l)
    End If
End Function

System.Drawing(2.0.0.0) System.Windows.Forms(2.0.0.0). TextWidth , Tahoma, 8. , .

TextCap SSRS, :

=Code.TextCap(Fields!CustomerName.Value, 150, "...")

150 , "..." .

+6

Source: https://habr.com/ru/post/1545235/


All Articles