I typically reference a function when using eventing, this keeps the event handlers on large forms pretty clean and allows for the action you're requesting.
As an example of why I do this, have a look at these event handlers.
<#===================================
---- Windows Form Event Handlers ----
===================================#>
$btn_Config_Done.Add_Click({$Form_Config.DialogResult = [System.Windows.Forms.DialogResult]::OK})
$cb_Domain.Add_SelectionChangeCommitted({Event_Credential_TextChanged $this $_})
$Form_Main.Add_Load({Event_Form_Load $this $_})
$Form_Main.Add_FormClosing({Event_Form_FormClosing $this $_})
$Form_Config.Add_Activated({Event_Config_Activated})
$Form_Config.Add_Deactivate({Event_Config_Deactivate $this $_})
$tb_UserName.Add_KeyUp({Event_Credential_TextChanged $this $_})
$mtb_Password.Add_KeyDown({Event_Password_KeyDown $this $_})
$mtb_Password.Add_KeyPress({Event_Password_KeyPress $this $_})
$tsmi_Configure.Add_Click({$Form_Config.ShowDialog()})
$tsmi_Import.Add_Click({Event_Import_Click})
$tsmi_Export.Add_Click({Event_Export_Click})
$tsmi_Reset.Add_Click({Event_Reset_Click})
$tsmi_Exit.Add_Click({$Form_Main.Close()})
<#=========================================
---- Windows Form Event Handlers (End) ----
=========================================#>
Nice and neat so that I can easily see what objects have event handlers defined.
Here are a few implementations along the lines of what you entered.
#Example of how I do it:
Function Event_Button1_Click{
param([object]$Sender, [System.EventArgs]$e)
if (some condition is not met){
return #The function now ends, as does the event handler.
}
else{
#Run this event code.
}
}
#Create an event handler on your object.
$button1.Add_Click({Event_Button1_Click $this $_}) #$This is the sender object, $_ are the event arguments.
#Example using your method:
$button1.Add_Click({
if(3 -ne 2){
Write-Host "Event Cancelled"
return
}
else{
#Run this event code.
}
})