填充在 Windows 项目中的 ComboBox 控件

在本节中,将编写代码以使用非嵌入式报表的列表来填充 ComboBox 控件。将使用 getReports() 帮助器方法通过 Crystal Reports Web 服务来生成报表列表,然后将此列表绑定到 ComboBox 控件。

对于网站,请参见填充 DropDownList 控件

填充在 Windows 项目中的 ComboBox 控件

在此过程中,将使用 Crystal Reports Web 服务来填充报表的排序列表。

  1. 打开 Windows 窗体。

  2. 从“视图”菜单中,单击“代码”。

  3. 在类签名上,在类顶部为 System.IO, System.Collections 和 System.Web.Services 命名空间添加 "Imports"[Visual Basic]"using"[C#] 声明。 [Visual Basic]

    Imports System.IO
    Imports System.Collections
    Imports System.Web.Services

    [C#]

    using System.IO;
    using System.Collections;
    using System.Web.Services

  4. Form1_Load 事件处理程序内部,使用 getReports() 帮助器方法从 Web 服务中检索报表列表。将结果分配给 reports 数组。 [Visual Basic]

    Dim myReports As ArrayList = getReports()

    [C#]

    ArrayList reports = getReports();

  5. 在下一行上,实例化一个 ArrayList 对象。 [Visual Basic]

    Dim myArrayList As ArrayList = New ArrayList()

    [C#]

    ArrayList arrayList = new ArrayList();

  6. 下一步,创建 For Each 循环,此循环在reports 的每个元素间循环。 [Visual Basic]

    For Each myPath As String In myReports
    Next

    [C#]

    foreach (string path in reports)
    {
    }

  7. 下一个代码块使用字符串处理方法,从每个报表的名称中删除文件路径,并使用字符串字面值替换转义符。将这些行插入到 For Each 循环内。 [Visual Basic]

    Dim myReportNamePrefix As Integer = myPath.LastIndexOf("/") + 1
    Dim myReportNameSufix As Integer = myPath.LastIndexOf("?")
    Dim myReportNameLength As Integer = myReportNameSufix - myReportNamePrefix
    Dim myReportName As String = myPath.Substring(myReportNamePrefix, myReportNameLength)
    myReportName = myReportName.Replace("%20", " ")

    [C#]

    int reportNamePrefix = path.LastIndexOf(@"/") + 1;
    int reportNameSufix = path.LastIndexOf(@"?");
    int reportNameLength = reportNameSufix - reportNamePrefix;
    string reportName = path.Substring(reportNamePrefix, reportNameLength);
    reportName = reportName.Replace("%20", " ");

  8. 仍然在 For Each 循环内部,添加一行代码,将截断的报表名称添加到 arrayList 中。 [Visual Basic]

    myArrayList.Add(myReportName)

    [C#]

    arrayList.Add(reportName);

  9. ? For Each ????,? ArrayList ????? DropDownList ??? dataSource ?????? ArrayList ?????? ComboBox ??? [Visual Basic]

    reportsList.DataSource = mysortedList

    [C#]

    reportsList.DataSource = sortedList;

  10. 从“文件”菜单中选择“全部保存”。

  11. 从“生成”菜单中选择“生成解决方案”。

  12. 如果生成过程中出错,请立即纠正。

在下一节中,将添加一个 click 事件,根据 reportsList ComboBox 控件中选择的内容来绑定报表。

继续到添加 Click 事件以绑定报表.

请参见

其它教程 | 教程和示例代码 | 教程的示例代码目录

将反馈意见发送给 Business Objects

© 2005 Business Objects SA. All Rights Reserved

Business Objects
http://www.china.businessobjects.com/
や穿狝叭
http://www.china.businessobjects.com/BOindex/support/