获取选定的纸张来源

添加到 Web 窗体或 Windows 窗体的 paperSourceList 控件会显示基于当前选定打印机的自定义纸张来源列表。当最终用户在运行时从 paperSourceList 控件选择纸张来源时,这一选定的纸张来源必须应用到报表的 CustomPaperSource 属性。

但是,从 paperSourceList 控件只能获得两种可能的值类型:

  • 选定项目的字符串型值。
  • 选定项目的整数索引。

两种类型(字符串或整数)与 CustomPaperSource 属性都不兼容。只能将类型 System.Drawing.Printing.PaperSource 赋给该属性

因此,在本节中,要根据 paperSourceList 控件的选定索引创建帮助器方法 GetSelectedPaperSource(),以确定正确的 PaperSource 实例然后返回该实例。

为此,该方法会在当前选定打印机的 PaperSources 集合中循环,然后将 PaperSource 实例的 SourceName 字符串属性与选定项目的字符串值进行比较。当发现匹配的 PaperSource 时,将会从该方法返回此 PaperSource 实例。

创建 GetSelectedPaperSource() 方法

  1. 在类的底部,创建返回 PaperSource 实例的 GetSelectedPaperSource() 帮助器方法。
``` vb
Private Function GetSelectedPaperSource() As System.Drawing.Printing.PaperSource
End Function
```

``` csharp
private System.Drawing.Printing.PaperSource GetSelectedPaperSource()
{
}
```

此步骤的其余代码会进入到 GetSelectedPaperSource() 方法。
  1. 在该方法内,声明并实例化来自 System.Drawing.Printing 命名空间的 PaperSource 类。

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

    Dim myPrinterSettings As System.Drawing.Printing.PrinterSettings = New System.Drawing.Printing.PrinterSettings()
    
    System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
    
  3. 将字符串常量 CURRENT_PRINTER 赋给 PrinterSettings 实例的 PrinterName 属性。

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

    For Each myPaperSource As System.Drawing.Printing.PaperSource In myPrinterSettings.PaperSources
    Next
    
    foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
    {
    }
    
  5. 在 foreach 循环内,添加一个条件块,以测试 PaperSource 实例的 SourceName 属性是否与 paperSourceList 控件中的选定项相匹配。

<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>对于网站中的 DropDownList 控件和 Windows 项目中的 ComboBox 控件而言,选定项的名称是不同的。请完成下面列出的相应步骤。</p></td>
</tr>
</tbody>
</table>

1.  在 Windows 项目中,创建条件块,该条件块将 paperSourceList 控件中选定项的名称指定为 SelectedText 属性。
    
    ``` vb
    If myPaperSource.SourceName = paperSourceList.SelectedText Then
    End If
    ```
    
    ``` csharp
    if (paperSource.SourceName == paperSourceList.SelectedText)
    {
    }
    ```

2.  或者,在网站中,创建条件块,该条件块将 paperSourceList 控件中选定项的名称指定为 SelectedItem.Text 属性。
    
    ``` vb
    If myPaperSource.SourceName = paperSourceList.SelectedItem.Text Then
    End If
    ```
    
    ``` csharp
    if (paperSource.SourceName == paperSourceList.SelectedItem.Text)
    {
    }
    ```
  1. 在条件块内,将当前 foreach 循环迭代的 PaperSource 实例赋给在该方法顶部声明的 selectedPaperSource 实例。

    selectedPaperSource = myPaperSource
    
    selectedPaperSource = paperSource;
    
  2. 在条件块外以及 foreach 循环外,从该方法返回 selectedPaperSource 实例。

    Return selectedPaperSource
    
    return selectedPaperSource;