本节说明如何为 Windows 项目配置 ListBox 控件。如果所构建的是网站,请参见“为网站配置 ListBox 控件”。
现在,可以为 Button 控件创建单击事件处理程序,然后将代码添加到此事件处理程序中。此事件处理程序根据用户在 ListBox 控件中所做的选择,为 CrystalReportViewer 类的工具栏属性设置不同的布尔值。
在创建此事件处理程序之前,需要创建两个枚举: CeWinCRVReportOptions 和 CeWinCRVToolbarOptions。
这两个枚举提供可选择的报表元素和工具栏元素的列表。
创建 CeWinCRVReportOptions 枚举
在“解决方案资源管理器”中,右击用粗体显示的项目名,指向“添加”,然后单击“类”。
在“添加新项”对话框的“名称”字段中,键入“CeWinCRVReportOptions”,然后单击“添加”。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>在 Visual Studio 中,可能会要求将此类放置在 App_Code 目录中。单击“是”按钮。</p></td>
</tr>
</tbody>
</table>
在类签名中,将单词 class 改为“enum”,以将该类转换为枚举。
注意在 Visual Basic 中,要记住把类的开始和结束签名都更改为 enum。
因为枚举没有构造函数,所以要删除代码的 C# 版本中提供的默认构造函数方法。
在枚举内部,输入值:
Toolbar Group_Tree Status_BarToolbar, Group_Tree, Status_Bar从“文件”菜单中,单击“全部保存”。
创建 CeWinCRVToolbarOptions 枚举
在“解决方案资源管理器”中,右击用粗体显示的项目名,指向“添加”,然后单击“类”。
在“添加新项”对话框的“名称”字段中,键入“CeWinCRVReportOptions”,然后单击“添加”。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>可能会要求将此类放置在 App_Code 目录中。单击“是”按钮。</p></td>
</tr>
</tbody>
</table>
- 在类签名中,将单词 class 改为“enum”,以将该类转换为枚举。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>在 Visual Basic 中,要记住把类的开始和结束签名都更改为 enum。</p></td>
</tr>
</tbody>
</table>
因为枚举没有构造函数,所以要删除代码的 C# 版本中提供的默认构造函数方法。
在枚举内部,输入值:
Page_Navigation_Button Go_to_Page_Button Close_View_Button Print_Button Refresh_Button Export_Button Group_Tree_Button Zoom_Button Search_ButtonPage_Navigation_Button, Go_to_Page_Button, Close_View_Button, Print_Button, Refresh_Button, Export_Button, Group_Tree_Button, Zoom_Button, Search_Button从“文件”菜单中,单击“全部保存”。
从枚举填充 ListBox 控件
现在将使用枚举值对 ListBox 控件进行填充,这些值代表可用于 CrystalReportViewer 工具栏的属性。
打开 Windows 窗体。
从“视图”菜单中,单击“代码”。
在 ConfigureCrystalReports() 方法中,将 listCRVReportListBox 控件的 DataSource 属性设置为 CeWinCRVReportOptions 枚举的值。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>本教程开始时,在ConfigureCrystalReports()过程中创建了 <a href="ms227453(v=vs.90).md">“项目设置”</a> 方法。要正确完成本教程,必须首先执行<a href="ms227453(v=vs.90).md">“项目设置”</a>。</p></td>
</tr>
</tbody>
</table>
``` vb
listCRVReport.DataSource =
System.Enum.GetValues(GetType(CeWinCRVReportOptions))
```
``` csharp
listCRVReport.DataSource =
System.Enum.GetValues(typeof(CeWinCRVReportOptions));
```
将 listCRVToolbarListBox 控件的 DataSource 属性设置为 CeWinCRVToolbarOptions 枚举的值。
listCRVToolbar.DataSource = System.Enum.GetValues(GetType(CeWinCRVToolbarOptions))listCRVToolbar.DataSource = System.Enum.GetValues(typeof(CeWinCRVToolbarOptions));将该 Chart.rpt 文件绑定到 CrystalReportViewercontrol. 的 ReportSource 属性。
有关示例报表的信息,请参见“示例报表目录”。
myCrystalReportViewer.ReportSource = "C:\Program Files\Microsoft Visual Studio 8\Crystal Reports\Samples\chs\Reports\Feature Examples\Chart.rpt"crystalReportViewer.ReportSource = "C:\\Program Files\\Microsoft Visual Studio 8\\Crystal Reports\\Samples\\chs\\Reports\\Feature Examples\\Chart.rpt";
为 Windows 项目的重新显示 Button 控件编写代码
现在您可以向 Button 控件的单击事件添加代码。单击方法必须为 CrystalReportViewer 类的报表和工具栏元素设置布尔值。如果选择了某个元素,则布尔值设置为 True,并且将显示该报表或工具栏元素。如果属性未被选定,则布尔值设置为 False,并且该报表或工具栏元素将不会显示。
打开 Windows 窗体。
从“视图”菜单中,单击“设计器”。
双击重新显示 Button 控件。
即会出现报表的代码隐藏类,表示已经自动生成 redisplay\_Click() 事件方法。
- 在 redisplay_Click() 事件方法中,调用 GetSelected() 方法,并传递来自 ListBox 的每个项。
GetSelected() 方法返回布尔值以设置 CrystalReportViewer 报表或工具栏属性。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>CrystalReportViewer 报表和工具栏元素被设置为它们在 CeWinCRVReportOptions 和 CeWinCRVToolbarOptions 枚举中的相应值。</p></td>
</tr>
</tbody>
</table>
``` vb
myCrystalReportViewer.ShowPageNavigateButtons =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Page_Navigation_Button)
myCrystalReportViewer.ShowGotoPageButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Go_to_Page_Button)
myCrystalReportViewer.ShowCloseButton = listCRVToolbar.
GetSelected(CeWinCRVToolbarOptions.Close_View_Button)
myCrystalReportViewer.ShowPrintButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Print_Button)
myCrystalReportViewer.ShowRefreshButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Refresh_Button)
myCrystalReportViewer.ShowExportButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Export_Button)
myCrystalReportViewer.ShowGroupTreeButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Group_Tree_Button)
myCrystalReportViewer.ShowZoomButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Zoom_Button)
myCrystalReportViewer.ShowTextSearchButton =
listCRVToolbar.GetSelected(CeWinCRVToolbarOptions.Search_Button)
myCrystalReportViewer.DisplayToolbar =
listCRVReport.GetSelected(CeWinCRVReportOptions.Toolbar)
myCrystalReportViewer.DisplayGroupTree =
listCRVReport.GetSelected(CeWinCRVReportOptions.Group_Tree)
myCrystalReportViewer.DisplayStatusBar =
listCRVReport.GetSelected(CeWinCRVReportOptions.Status_Bar)
```
``` csharp
crystalReportViewer.ShowPageNavigateButtons =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Page_Navigation_Button));
crystalReportViewer.ShowGotoPageButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Go_to_Page_Button));
crystalReportViewer.ShowCloseButton = listCRVToolbar.
GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Close_View_Button));
crystalReportViewer.ShowPrintButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Print_Button));
crystalReportViewer.ShowRefreshButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Refresh_Button));
crystalReportViewer.ShowExportButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Export_Button));
crystalReportViewer.ShowGroupTreeButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Group_Tree_Button));
crystalReportViewer.ShowZoomButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Zoom_Button));
crystalReportViewer.ShowTextSearchButton =
listCRVToolbar.GetSelected(Convert.ToInt32(CeWinCRVToolbarOptions.Search_Button));
crystalReportViewer.DisplayToolbar =
listCRVReport.GetSelected(Convert.ToInt32(CeWinCRVReportOptions.Toolbar));
crystalReportViewer.DisplayGroupTree =
listCRVReport.GetSelected(Convert.ToInt32(CeWinCRVReportOptions.Group_Tree));
crystalReportViewer.DisplayStatusBar =
listCRVReport.GetSelected(Convert.ToInt32(CeWinCRVReportOptions.Status_Bar));
```
测试重新显示 Button 控件
现在即可生成并运行项目,以自定义 CrystalReportViewer 工具栏。
从“生成”菜单中,单击“生成解决方案”。
如果生成过程中出错,请立即纠正。
从“调试”菜单中,单击“开始”。
listCRVReport 和 listCRVToolbarListBox 控件将显示 CrystalReportViewer 报表和工具栏选项的完整列表。
在工具栏选项列表框中,选择“Page_Navigation_Button”、“Print_Button”和“Export_Button”。
在报表选项列表框中,选择“Toolbar”、“Group_Tree”和“Main_Page”。
单击“重新显示报表”。
页面将重新加载,显示带有一个可见的工具栏、组树和主页面的 CrystalReportViewer 控件。在工具栏中,只能看见“页面导航”、“打印”和“导出”按钮。
返回到 Visual Studio,然后单击“停止”从调试模式中退出。