填充打印选项控件

在本节中,将填充 Web 窗体或 Windows 窗体上的 DropDownList(或 ComboBox)控件。为此,需要在代码隐藏类中写入代码。

大多数控件都是很容易利用 CrystalDecisions.Shared 命名空间中打印机枚举的值进行填充的。但是,在这些控件中,有一个控件 (paperSourceList) 必须使用基于当前选定打印机的自定义纸张来源进行填充。所以,您的第一步是指定当前打印机,然后,创建一个帮助器方法以从该打印机生成自定义纸张来源的 ArrayList。

继续浏览“获取选定的纸张来源”

创建 GetPaperSources() 帮助器方法

  1. 打开 Web 窗体。

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

  3. 在类的顶部,为打印机的网络路径创建一个字符串常量。

<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>在本示例中,打印机路径是“\\NetworkPrintServer2\\Printer15”。</p></td>
</tr>
</tbody>
</table>

``` vb
Private Const CURRENT_PRINTER As String = "\\NetworkPrintServer2\Printer15"
```

``` csharp
private const string CURRENT_PRINTER = @"\\NetworPrinterServer2\Printer15";
```
  1. 在类的上方,添加一条 "Imports"[Visual Basic] 或 "using"[C#] 语句以引用 System.Collections 命名空间。
``` vb
Imports System.Collections
```

``` csharp
using System.Collections;
```
  1. 在类的底部,创建返回 ArrayList 的 GetPaperSources() 帮助器方法。
``` vb
Private Function GetPaperSources() As ArrayList
End Function
```

``` csharp
private ArrayList GetPaperSources()
{
}
```

这一步骤中的其余代码会进入 GetPaperSources() 方法。
  1. 在该方法中,声明并实例化 ArrayList。

    Dim myArrayList As ArrayList = New ArrayList()
    
    ArrayList arrayList = new ArrayList();
    
  2. 声明并实例化来自 System.Drawing.Printing 命名空间的 PrinterSettings 类。

<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>本教程中使用的几个类在 System.Drawing.Printing 命名空间和 CrystalDecisions.Shared 命名空间中具有重复性名称。当在本教程中使用具有重复性名称的类时,类名称的前面会带有完整的命名空间,以明确该类属于哪个命名空间。</p></td>
</tr>
</tbody>
</table>

``` vb
Dim myPrinterSettings As System.Drawing.Printing.PrinterSettings = New System.Drawing.Printing.PrinterSettings()
```

``` csharp
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
```
  1. 将 PrinterSettings 实例的 PrinterName 属性设置为字符串常量 CURRENT_PRINTER。

    myPrinterSettings.PrinterName = CURRENT_PRINTER
    
    printerSettings.PrinterName = CURRENT_PRINTER;
    
  2. 创建一个 foreach 循环,在 PaperSources 索引类实例的各个 PaperSource 实例间循环。

    Dim myPaperSource As System.Drawing.Printing.PaperSource
    For Each myPaperSource As System.Drawing.Printing.PaperSource In myPrinterSettings.PaperSources
    Next
    
    foreach (System.Drawing.Printing.PaperSource paperSource in
    printerSettings.PaperSources)
    {
    }
    
  3. 在 foreach 循环内,将 PaperSource 实例的 SourceName 属性添加到 ArrayList。

    myArrayList.Add(myPaperSource.SourceName.ToString())
    
    arrayList.Add(paperSource.SourceName.ToString());
    
  4. 在 foreach 循环外部,从方法中返回 ArrayList。

    Return myArrayList
    
    return arrayList;
    
  5. 从“文件”菜单中,单击“全部保存”。

填充 DropDownList 控件

现在,必须利用 CrystalDecisions.Shared 命名空间中的枚举填充前三个 DropDownList 控件。第四个 DropDownList 控件是通过刚创建的 GetPaperSources() 方法填充的。

  1. 打开 Web 或 Windows 窗体。

  2. 从“视图”菜单中,单击“设计器”。

  3. 双击窗体上的任意空白区域。

页面会切换到“代码”视图并生成 Page\_Load() 事件方法(对于 Web 窗体)或 Form1\_Load() 事件方法(对于 Windows 窗体)。
  1. 如果在开发网站,请在 Page_Load() 事件方法内创建一个 Not IsPostBack 条件块。

    If Not IsPostBack Then
    End If
    
    if (!IsPostBack)
    {
    }
    
    Note注意

    Not IsPostBack 条件块会封装仅在页面第一次加载时运行的代码。在 Not IsPostBack 条件块中,控件通常都绑定到数据值,这样它们的数据值(以及所有后续控件事件)才不会在重新加载页面时重置。

  2. Windows 项目和网站将以下代码行放置在不同的地方:

    • 在 Windows 项目中,应该将以下代码行放置在 Form_Load 事件处理程序中。
    • 在网站,应该将以下代码行嵌套在 Page_Load 事件处理程序的 Not IsPostBack 条件块中。
    1. 将 paperOrientationList 控件实例的 DataSource 属性设置为 PaperOrientation 枚举的值。

      paperOrientationList.DataSource = System.Enum.GetValues(GetType(PaperOrientation))
      
      paperOrientationList.DataSource = System.Enum.GetValues(typeof(PaperOrientation));
      
    2. 将 paperSizeList 控件实例的 DataSource 属性设置为 PaperSize 枚举的值。

      paperSizeList.DataSource = System.Enum.GetValues(GetType(PaperSize))
      
      paperSizeList.DataSource = System.Enum.GetValues(typeof(PaperSize));
      
    3. 将 printerDuplexList 控件实例的 DataSource 属性设置为 PrinterDuplex 枚举的值。

      printerDuplexList.DataSource = System.Enum.GetValues(GetType(PrinterDuplex))
      
      printerDuplexList.DataSource = System.Enum.GetValues(typeof(PrinterDuplex));
      
    4. 将 paperSourceList 控件实例的 DataSource 属性设置为在上一节中创建的 GetPaperSources() 帮助器方法。

      paperSourceList.DataSource = GetPaperSources()
      
      paperSourceList.DataSource = GetPaperSources();
      
    5. 最后,如果要构建一个网站,请输入对 DataBind() 方法的调用,以绑定第四个 DropDownList 控件的值。

      DataBind()
      
      DataBind();