您的位置:首页 > 其它

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

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

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

方法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)导出报表时,所有这些修改都会被保留。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐