共用方式為


多重選取清單框

此範例僅適用於 Windows 平臺。

使用 Windows PowerShell 3.0 和更新版本,在自定義 Windows Form 中建立多重選取清單框控制件。

建立允許多個選取項目的清單框控制件

複製下列內容,然後將下列內容貼到 Windows PowerShell ISE 中,然後將它儲存為 PowerShell 腳本 (.ps1) 檔案。

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please make a selection from the list below:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)

$listBox.SelectionMode = 'MultiExtended'

[void] $listBox.Items.Add('Item 1')
[void] $listBox.Items.Add('Item 2')
[void] $listBox.Items.Add('Item 3')
[void] $listBox.Items.Add('Item 4')
[void] $listBox.Items.Add('Item 5')

$listBox.Height = 70
$form.Controls.Add($listBox)
$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox.SelectedItems
    $x
}

腳本一開始會載入兩個 .NET Framework 類別: System.DrawingSystem.Windows.Forms。 然後,您會啟動 .NET Framework 類別 System.Windows.Forms.Form 的新實例。 這會提供您可以開始新增控制件的空白表單或視窗。

$form = New-Object System.Windows.Forms.Form

建立 Form 類別的實體之後,請將值指派給這個類別的三個屬性。

  • Text (文字)。 這會成為視窗的標題。
  • 大小。 這是表單的大小,以像素為單位。 上述腳本會建立寬度為 300 像素的表單,高度為 200 像素。
  • StartingPosition。 在上述腳本中,這個選擇性屬性會設定為 CenterScreen 。 如果您未新增此屬性,Windows 會在開啟表單時選取位置。 藉由將 StartingPosition 設定為 CenterScreen,您就會在每次載入時自動在畫面中間顯示表單。
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

接下來,建立表單的 [確定] 按鈕。 指定 [確定] 按鈕的大小和行為。 在此範例中,按鈕位置距離窗體的上邊緣為120圖元,而左邊緣則為75圖元。 按鈕高度為 23 像素,而按鈕長度為 75 像素。 腳本會使用預先定義的 Windows Forms 類型來判斷按鈕行為。

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

同樣地,您會建立 [取消] 按鈕。 [ 取消 ] 按鈕距離頂端為 120 像素,但距離視窗左邊緣 150 像素。

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

接下來,在您的視窗上提供標籤文字,描述您希望使用者提供的資訊。

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please make a selection from the list below:'
$form.Controls.Add($label)

新增控制件 (在此案例中為清單框),讓使用者提供標籤文字中所描述的資訊。 除了文字框之外,您還可以套用許多其他控制項:如需更多控件,請參閱 System.Windows.Forms 命名空間

$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)

以下是您想要允許使用者從清單中選取多個值的方式。

$listBox.SelectionMode = 'MultiExtended'

在下一節中,您會指定清單框要向用戶顯示的值。

[void] $listBox.Items.Add('Item 1')
[void] $listBox.Items.Add('Item 2')
[void] $listBox.Items.Add('Item 3')
[void] $listBox.Items.Add('Item 4')
[void] $listBox.Items.Add('Item 5')

指定清單框控制件的最大高度。

$listBox.Height = 70

將清單框控件新增至窗體,並指示 Windows 在開啟其他視窗和對話框時開啟表單。

$form.Controls.Add($listBox)
$form.Topmost = $true

新增下列程式代碼行,以在 Windows 中顯示表單。

$result = $form.ShowDialog()

最後,區塊內的 if 程式代碼會指示 Windows 在使用者從清單框中選取一或多個選項之後,對窗體執行什麼動作,然後按兩下 [確定 ] 按鈕或按 Enter 鍵。

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox.SelectedItems
    $x
}

另請參閱