user5349170
user5349170

Reputation: 163

How to close the form on mouse click?

I'm creating a Graphical Date Picker in PowerShell, based on this article. The following part of the code in the article helps to close the form after the date selection and pressing Enter key:

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
        {
            $dtmDate=$objCalendar.SelectionStart
            $objForm.Close()
        }
    })

I also want to add mouse event for the date selection and to close the form. So the question is, how do we close the form once the date has been selected and on MouseUp event? Thanks.

Upvotes: 0

Views: 267

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174515

Instead of registering an event handler for all Click/MouseDown/MouseUp events, you could use the DateSelected event instead. From the description:

Occurs when the user makes an explicit date selection using the mouse.

$objForm.Add_DateSelected({
        $dtmDate=$objCalendar.SelectionStart
        $objForm.Close()
    })

In PowerShell 3.0 or newer, you may have to change the scope of $dtmDate variable in order for it to work:

$script:dtmDate = $objCalendar.SelectionStart

or (-Scope 1 means "direct parent scope" or "1 step up the call stack")

Set-Variable -Scope 1 -Name dtmDate -Value $objCalendar.SelectionStart

Upvotes: 1

Related Questions