gpt4 book ai didi

reporting-services - 在 SSRS 中用省略号截断文本框内容

转载 作者:行者123 更新时间:2023-12-04 14:12:54 25 4
gpt4 key购买 nike

默认情况下,当 SSRS 中文本框的内容超出文本框的宽度时,文本框将垂直增长以适应其内容。通过将文本框的“CanGrow”属性设置为“False”,可以关闭此功能。

但是,这突然切断了内容,这并不理想。

我正在寻找一种方法来清楚地向用户显示文本太宽而无法容纳文本框。过去,当文本字符串的长度高于某个固定数字时,我一直使用一个简单的表达式来添加省略号“...”:

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

但是,当客户名称包含大写字母、小写字母、标点符号和其他使单个字符像素宽度变化很大的内容时,这并不适用。

理想情况下,文本框控件的某些属性将允许报表开发人员在文本不适合文本框时添加省略号。

有没有人对更优雅的方法有任何建议?

最佳答案

我想出的另一个解决方案是使用 VB.NET,特别是 TextRenderer.MeasureText() 函数。

为了完成这项工作,我在报告中添加了以下代码:

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 像素,并且将添加后缀参数“...”以防任何字符被截断。

关于reporting-services - 在 SSRS 中用省略号截断文本框内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24306841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com