如果在确认消息框中按“否”,如何停止powershell中的事件

如果在确认消息框中按“否”,如何停止powershell中的事件,powershell,datagridview,Powershell,Datagridview,我有一个DataGridView,其中充满了行,能够使用delete键选择和删除行。 按下Delete键时会弹出一个确认消息框,询问是或否继续删除 Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $F

我有一个DataGridView,其中充满了行,能够使用delete键选择和删除行。 按下Delete键时会弹出一个确认消息框,询问是或否继续删除

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '800,800'
$Form.text                       = "Form"
$Form.TopMost                    = $false

$DataGridView1                   = New-Object system.Windows.Forms.DataGridView
$DataGridView1.BackColor         = "#f7f7f7"
$DataGridView1.width             = 771
$DataGridView1.height            = 716
$DataGridView1.Anchor            = 'top,right,bottom,left'
$DataGridView1.location          = New-Object System.Drawing.Point(15,68)

$import                          = New-Object system.Windows.Forms.Button
$import.text                     = "import"
$import.width                    = 60
$import.height                   = 30
$import.location                 = New-Object System.Drawing.Point(25,22)
$import.Font                     = 'Microsoft Sans Serif,10'

$save                            = New-Object system.Windows.Forms.Button
$save.text                       = "save"
$save.width                      = 60
$save.height                     = 30
$save.location                   = New-Object System.Drawing.Point(125,22)
$save.Font                       = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($DataGridView1,$import,$save))


$import.Add_Click({ importXML })
$save.Add_Click({ saveXML })
$DataGridView1.Add_UserDeletingRow({ message })

$DataGridView1.AutoSizeColumnsMode = 16
Function importXML(){
    $xml_input = Get-FileName
    $ds = New-Object System.Data.Dataset
    $ds.ReadXml($xml_input)
    $DataGridView1.DataSource = $ds.Tables[0]
}
Function message(){
    $msgBoxInput = [System.Windows.Forms.MessageBox]::Show("Proceed with the deletion?","Delete confirmation","YesNo","Question")
        if ($msgBoxInput -eq "YES" ) 
        {
            [System.Windows.Forms.MessageBox]::Show("The selected row will be deleted")
        }
        else
        {
            #stop the deletion
        }
}
Function saveXML(){
    $xml_output = Save-FileName
    $DataGridView1.DataSource.writexml($xml_output)
}

[void]$Form.ShowDialog()
除了别的以外,一切都很顺利。我不知道如何中止删除事件


有什么建议吗?

在MSDN示例中,我们看到他们在c中这样做,将e设置为当前事件的引用,然后将e.Cancel设置为true,这允许我们使用如下语法:

private void DataGridView1_UserDeletingRow(object sender,
DataGridViewRowCancelEventArgs e){
  e.Cancel = true; //Cancel the event
}
在PowerShell中,如果我们尝试以这种方式添加事件处理程序,就会出现错误,因为事件处理程序方法通常只允许我们指定一个重载,即在事件上运行的脚本块

幸运的是,引用当前事件似乎很容易!要取消删除,只需将其添加到add_UserDeletingRow脚本块

else
        {
            #stop the deletion
            $PSItem.Cancel=$true
        }
您还可以使用$current item语法,如下所示

else
        {
            #stop the deletion
            $_.Cancel=$true
        }

每当您添加事件处理程序并且需要使用PowerShell引用事件本身时,都会有很多这样的事件,仅针对DataGridView!您将使用$或$PSItem。因此,在MSDN中的这些示例中,如果您看到它们用e或类似的东西引用当前事件,只需替换$或$PSItem即可。

可能不是最佳做法,但您是否尝试过退出/中断?设置$GLOBAL:变量值,并在删除代码中检查该变量的值。@Paxz它不起作用。我收到一条错误消息,您的应用程序中出现未处理的异常…@EBGreen我没有删除代码。所有相关的代码都是我原来帖子中的代码。其他一切都只与GUI相关。我不明白你关于$GLOBAL变量的建议。你能解释一下吗?还有什么{return}太棒了!它工作完美无瑕。。。在MSDN上很难找到有关Powershell的信息。那里的一切都只针对C、C++和VB,但从来没有用于PuthS壳。令人沮丧的。无论如何,我期待着发布一些更难的问题:-谢谢。