返回列表 发帖

[问题求助] PowerShell怎样给列表项添加双击和中键事件?

下面的代码能显示 可多选的列表框

存在两个问题:
1.不管用下面的哪种方法,窗体底部都有空白, 如何去除? 我想尽量使用第2种方法
$listBox.Size = '160,260'

$listBox.Dock = 'Fill'



2.我已经将窗体中的OK按钮删除了, 我想:
A.当选中了一个列表项时: 鼠标双击, 实现OK按钮的功能
B.当选中了多个列表项时: 按下鼠标中键, 实现OK按钮的功能


这样一搞, 感觉干净利索, 尤其是列表项很多时, 点OK按钮不太方便
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = '双击查询'
$Form.FormBorderStyle = "FixedToolWindow"
$form.StartPosition = 'CenterScreen'
$form.Font = New-Object System.Drawing.Font("微软雅黑", 10, [Drawing.FontStyle]::Bold)
$form.ClientSize = '160, 260'
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Size = '160,260'
$listBox.SelectionMode = 'MultiExtended'
$listBox.DataSource = '张三', '李四', '王五', '赵六'
$form.Controls.Add($listBox)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
        $selectedItems = $listBox.SelectedItems
}COPY
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

Add-Type -AssemblyName System.Windows.Forms
[system.Windows.forms.form]$form = New-Object System.Windows.Forms.Form
$form.Text = '双击查询'
$Form.FormBorderStyle = "FixedToolWindow"
$form.StartPosition = 'CenterScreen'
$form.Font = New-Object System.Drawing.Font("微软雅黑", 10, [Drawing.FontStyle]::Bold)
$form.ClientSize = '160, 260'
$listBox = New-Object System.Windows.Forms.Listbox
# $listBox.Size = '160,260'
$listBox.Dock = 'Fill'
$listBox.SelectionMode = 'MultiExtended'
$listBox.DataSource = '张三', '李四', '王五', '赵六'
$form.Controls.Add($listBox)
$form.add_FormClosing( {
    param($oSender, $oArgs)
    $oArgs.Cancel = $listBox.SelectedItems.Count -eq 0
    $Script:selectedItems = $listBox.SelectedItems
  })
$result = $form.ShowDialog()
$Script:selectedItemsCOPY
必须选择至少一个对象才能退出窗口;
微信:flashercs
QQ:49908356

TOP

回复 2# flashercs


双击没有反应
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = '双击查询'
$Form.FormBorderStyle = "FixedToolWindow"
$form.StartPosition = 'CenterScreen'
$form.Font = New-Object System.Drawing.Font("微软雅黑", 10, [Drawing.FontStyle]::Bold)
$form.ClientSize = '160, 260'
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Size = '160,260'
$listBox.SelectionMode = 'MultiExtended'
$listBox.DataSource = '张三', '李四', '王五', '赵六'
$form.Controls.Add($listBox)
$Script:selectedItems = $null
$listBox.add_DoubleClick({
    if($listBox.SelectedItems.Count -ne 1){ return }
    $Script:selectedItems = $listBox.SelectedItems
    $form.Close()
})
$listBox.add_MouseDown({
    if($_.Button -ne 'Middle'){ return }
    $Script:selectedItems = $listBox.SelectedItems
    $form.Close()
})
$result = $form.ShowDialog()
$Script:selectedItemsCOPY

TOP

回复 4# went

感谢大侠帮忙,没想到$_还能用在事件中!


列表框下边的空白无解吗?

本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

回复 5# 5i365


    listbox的高没必要和form相同,调大一点,260改成300

TOP

返回列表