当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 多选列表框

作者:精品下载站 日期:2024-12-14 03:01:43 浏览:13 分类:玩电脑

多选列表框


该示例仅适用于Windows平台。

使用 Windows PowerShell 3.0 及更高版本在自定义 Windows 窗体中创建多选列表框控件。

创建允许多项选择的列表框控件

将以下内容复制并粘贴到 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 类的实例后,为该类的三个属性赋值。

  • 文本。这将成为窗口的标题。
  • 大小。这是表单的大小(以像素为单位)。前面的脚本创建了一个宽 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 窗体类型来确定按钮行为。

$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
}

参见

  • 周末脚本编写者:修复 PowerShell GUI 示例
  • GitHub:Dave Wyatt 的 WinFormsExampleUpdates
  • Windows PowerShell 本周提示:多选列表框 - 以及更多!)

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯