FastReport中国社区FastReport联系电话 联系电话:023-68661681

如何实现报表中高亮显示文本功能

来源:   发布时间:2017-12-29   浏览:3292次

在浏览电子报表时,你常常会想要高亮显示一些文本,就像你在阅读纸质文档时用荧光笔勾画重点那样。我要告诉你,这是可以实现的。

在报表预览模式下,你可以通过单击鼠标来选择所需的文本字段。也就是说,你单击文本框,它就会改变颜色。如果再次按下,突出显示效果消失。我会告诉你两种方法来做到这一点,他们都涉及使用报表脚本。

方法1:该方法的本质是使用一个Sender(即引发事件的对象)来分配一个新的颜色给OnCklick事件处理句柄。在这种情况下,你需要更新缓存中的报表页面,因为 因为预览模式会显示其报表。

那么,让我们用产品清单创建一个最简单的报表:

如何实现报表中高亮显示文本功能

假设我们想通过点击来突出显示其中的一个字段。而当我们再次点击时,我们想删除高亮显示。为文本对象创建事件“点击”,以显示Products.ProductName字段。

private void Text1_Click(object sender, EventArgs e)
 { 
 if (sender is TextObject)
 {
 //Define sender as TextObject
 TextObject obj = sender as TextObject;
 //Method of highlighting an object
 SwitchColor(obj);
 if(Report.Preview != null)
 {
 // Refresh the current page of the report in the cache, if the report is viewed on a desktop
 Report.PreparedPages.ModifyPage(Report.Preview.PageNo - 1, obj.Page as ReportPage);
 //Refresh preview
 Report.Preview.Refresh();
 } 
 }
 }
 
 private void SwitchColor(TextObject obj)
 {
 // Check whether the object is filled with yellow
 if (obj.Fill is SolidFill && (obj.Fill as SolidFill).Color != Color.Yellow)
 //Fill with yellow
 obj.Fill = new SolidFill(Color.Yellow);
 // Clear the fill
 else
 obj.Fill = new SolidFill(Color.Transparent); 
 }

如你所见,我们从点击事件中提取Sender对象,将其定义为文本对象并更改其填充。然后,重新绘制缓存中的报表页面。如果我们有一个web报表,我们只需更改对象的填充,而无需重新绘制报表页面。

我们再考虑下另一种替代方法。

方法2:这种方法的本质是确定对象的坐标,我们将用颜色填充。然后我们更新缓存中的报表页面,就像第一个方法一样。

private void Text1_Click(object sender, EventArgs e)
 { 
 if (sender is TextObject)
 {
 // Get the current page number
 int pageNo = Report.Preview.PageNo - 1;
 // Get the page by number
 ReportPage page = Report.PreparedPages.GetPage(pageNo);
 // Define sender as TextObject - this phantom object, we also need the original object from the preview page
 TextObject obj = sender as TextObject;
 // Looking the original object on the preview page
 foreach(ReportComponentBase b in page.AllObjects)
 // It is necessary to identify the object by name and coordinates
 if (b.Name == obj.Name && b.AbsTop == obj.AbsTop && b.AbsLeft == obj.AbsLeft)
 {
 // Get the original object
 obj = b as TextObject;
 break;
 } 
 // Defining the object's fill
 if (obj.Fill is SolidFill && (obj.Fill as SolidFill).Color != Color.Yellow)
 obj.Fill = new SolidFill(Color.Yellow);
 else
 obj.Fill = new SolidFill(Color.Transparent);
 
 // Update the report page in the cache
 Report.PreparedPages.ModifyPage(pageNo, page);
 // Refresh prewiew
 Report.Preview.Refresh();
 }
 }

我们来演示一下这个代码的操作:

如何实现报表中高亮显示文本功能

这种方法客观上比较复杂,只适用于预览模式。但是,它有它的优点。假设你不仅要突出显示你单击的文本对象,还要突出显示表中的整个行。然后添加另一个文本对象,使其与我们将单击的对象重叠。我们拉伸它的整个宽度。文本对象的左边界与文本对象的左边界重合,为此我们创建了一个点击事件,这一点很重要。右键单击,并从菜单中选择:

如何实现报表中高亮显示文本功能

就这样,我们将这个对象移动到背景,以便它不与其他文本字段重叠。

如何实现报表中高亮显示文本功能

修改一下我们以前的代码:

if (b.AbsTop == obj.AbsTop && b.AbsLeft == obj.AbsLeft)
 {
 // Get the original object
 obj = b as TextObject;
 break;
 } 

我从条件中删除了名称的比较,只留下了字段开始坐标的比较。现在运行报表并点击产品名称:

如何实现报表中高亮显示文本功能

如上所示,这两种方法都是可行的。第一种方法比较简单,但是只允许你选择Sender中的特定对象。第二个更复杂,但它可以让你突出Sender之外的的对象。你只需要指定他们的坐标。在web报表中,你只能使用第一种方法。

当然,你不仅可以更改背景颜色,还可以更改文本本身的颜色、样式或字体。另外,当以任何支持的格式(例如PDF)导出报表时,所有这些修改都会被保留。


产品介绍 | 下载试用 | 优惠活动 | 在线客服 | 联系Elyn

 

推荐阅读
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/1958.html

联系我们
  • 重庆总部 023-68661681
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
微信
QQ 电话
023-68661681
返回
顶部