标题: [问题求助] PowerShell怎样给列表项添加双击和中键事件? [打印本页]
作者: 5i365 时间: 2022-9-21 19:23 标题: 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
- }
复制代码
作者: flashercs 时间: 2022-9-23 16:12
- 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:selectedItems
复制代码
必须选择至少一个对象才能退出窗口;
作者: 5i365 时间: 2022-9-23 21:26
回复 2# flashercs
双击没有反应
作者: went 时间: 2022-9-23 23:27
- 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:selectedItems
复制代码
作者: 5i365 时间: 2022-9-24 05:40
回复 4# went
感谢大侠帮忙,没想到$_还能用在事件中!
列表框下边的空白无解吗?
作者: went 时间: 2022-9-24 08:52
回复 5# 5i365
listbox的高没必要和form相同,调大一点,260改成300
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |