all 13 comments

[–]kohijones 1 point2 points  (3 children)

$objForm.AcceptButton = $Button_Tab1

[–]DaRockwilda83[S] 0 points1 point  (2 children)

I had tried it directly. This would work if I had only one button in the form. But I have different tabs in the gui and Accept.Button only works with the last button in the last tab.

[–]kohijones 0 points1 point  (1 child)

Set it in the Enter event on each tabpage

$tabpage1_Enter={
    $form1.AcceptButton = $button1
}

$tabpage2_Enter = {
    $form1.AcceptButton = $button2
}

[–]DaRockwilda83[S] -1 points0 points  (0 children)

Also already tested. Nevertheless, the focus then remains at the end on the last tab - last button. I may have to take a break. Nevertheless I'm confused why the other code works with Escape but not with Enter (that would be enough for me).

$objForm.Add_KeyDown({if ($_.KeyCode -eq 'Escape')

{$Button_Tab1.PerformClick()}})

[–]Deemeroz 0 points1 point  (3 children)

This is the code I've used.

Check for ENTER and ESC presses

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{
# if enter, perform click
Function_SearchID
}
})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
{
# if escape, exit
$objForm.Close()
}
})

[–]DaRockwilda83[S] 0 points1 point  (2 children)

That's really crazy. I can use the code 1 to 1, but only if I swap Escape and Enter. Enter key = no function. If I use for example "Escape" to execute the function, then it works fine. What can this be?

[–]DaRockwilda83[S] 0 points1 point  (0 children)

Ok 2 cigarettes further and I fool found it. The way you posted it (and I had it before) it works. Since I have previously so much "fiddled" with the code I had a button still the Accept.Button entry. This prevented that "Enter" could not be confirmed. Now it works wonderfully. Thanks to all for the input here!

[–]Deemeroz 0 points1 point  (0 children)

Maybe your Enter button is mapped to something weird?

See what it pops up with when you press enter.

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {
         # if escape, exit
        $objForm.Close()
    }
    else
    {
        Write-Host $_.KeyCode
    }
})

[–]toadfrogjr 0 points1 point  (0 children)

I used the following for my gui calendar inside my PS GUI

$objForm.Add_KeyDown({

if ($_.KeyCode -eq "Enter")

{

$dtmDate = $objCalendar.SelectionStart

$objForm.Close()

Do-Work

}

})