By default, when the contents of a textbox in SSRS overflows the width of the textbox, the textbox will grow vertically to accommodate its content. This feature can be turned off, by setting the "CanGrow" property of the textbox to "False".
However, this abruptly cuts off the content, which is not ideal.
I am looking for a way to clearly show the user that the text is too wide to fit the textbox. In the past, I've been using a simple expression to add an 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 does not work well when customer names contain a mixture of capital letters, lowercase letters, punctuation and other stuff that makes the individual character pixel widths vary wildly.
Ideally, some property for the textbox control would allow the report developer to add an ellipsis whenever text would not fit in a textbox.
Does anyone have any suggestions for a more elegant approach to this?
Another solution I've come up with, is to use VB.NET, specifically the TextRenderer.MeasureText() function.
To make this work, I've added the following code to the report:
Remember to add references to the assemblies System.Drawing (2.0.0.0) and System.Windows.Forms (2.0.0.0). The
TextWidth
function will calculate the width of a string of text, using the Tahoma font, size 8. This could easily be made dynamic by adding the font name and the font size as additional parameters to both functions.When calling the
TextCap
function from an SSRS expression like this:the text will automatically be truncated at 150 pixels, and the suffix argument "..." will be added in case any characters were truncated.