简体   繁体   English

在powershell中的datagridview控件中右键单击select一行

[英]Right Click to select a row in datagridview control in powershell

Im working on a project in Powershell the uses WPF controls.我在 Powershell 中开展一个项目,使用 WPF 控件。

I have a datagridview where only one full row can be selected.我有一个数据网格视图,其中只能选择一整行。 There is also a contextmenustrip that is working fine in the datagridview as well.在 datagridview 中还有一个 contextmenustrip 也可以正常工作。

My problem is that I would like a right-click mouse event to select the row on which it was clicked and display the contextmenustrip.我的问题是我想要一个右键单击鼠标事件到 select 单击它的行并显示上下文菜单。 so there is no question for the user what they clicked.所以用户点击了什么是毫无疑问的。 Currently, the selected row doesnt change on right click.目前,右键单击时选定的行不会更改。

I've found many examples, but could use some guidance on converting them for use in powershell.我找到了很多示例,但可以使用一些指导将它们转换为在 powershell 中使用。

Once i get this down, i want to assign actions to each of the contextmenustrip selections Thanks!一旦我明白了这一点,我想为每个 contextmenustrip 选择分配动作谢谢!

The code in the linked answer , can be translated to PowerShell like as follows. 链接答案中的代码可以翻译为 PowerShell,如下所示。

Event handler registration事件处理程序注册

this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown); this.DeleteRow.Click += new System.EventHandler(this.DeleteRow_Click);

PowerShell doesn't support += for event handler registration, but you have two other options. PowerShell 不支持+=用于事件处理程序注册,但您有两个其他选项。 Either call the specially named methods that the C# ultimately converts += to - they will all have the form add_<EventName> :要么调用 C# 最终将+=转换为的特别命名的方法 - 它们都将具有add_<EventName>的形式:

$dataGridView = [System.Windows.Forms.DataGridView]::new()
# ...
$dataGridView.add_MouseDown($dgvMouseHandler)

Alternatively, use the Register-ObjectEvent cmdlet to let PowerShell handle the registration for you:或者,使用Register-ObjectEvent cmdlet 让 PowerShell 为您处理注册:

Register-ObjectEvent $dataGridView -EventName MouseDown -Action $dgvMouseHandler

Event arguments事件 arguments

 private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { var hti = MyDataGridView.HitTest(eX, eY); //...

In order to consume the event handler arguments you can either declare the parameters defined by the handler delegate in the script block:为了使用事件处理程序 arguments,您可以在脚本块中声明处理程序委托定义的参数:

$dgvMouseHandler = {
  param($sender,[System.Windows.Forms.MouseEventArgs]$e)

  # now you can dereference `$e.X` like in the C# example
}

Or take advantage of the $EventArgs automatic variable :或者利用$EventArgs自动变量

$dgvMouseHandler = {
  # `$EventArgs.X` will also do
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM