header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Exiting an event in a .Net For
Last Post 25 Jan 2012 10:23 AM by Geoff Guynn. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
KevinUser is Offline
New Member
New Member
Posts:1
Avatar

--
19 Jun 2009 11:13 AM
    Hi All,

    Trying to figure out a clean way to end an event on a winform

    $handler_button1_OnClick=
    {

    If(some condition is not met)
       {
          $Handler_Button1_OnClick Exits without excuting rest of button code 
       }

    }


    TIA
    Geoff GuynnUser is Offline
    Basic Member
    Basic Member
    Posts:140
    Avatar

    --
    25 Jan 2012 10:22 AM
    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.
        }
    })
    Geoff GuynnUser is Offline
    Basic Member
    Basic Member
    Posts:140
    Avatar

    --
    25 Jan 2012 10:23 AM
    Just noticed how old that was, my apologies for the necro post.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer