Getting Enter key to execute a button from a textbox

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Wed Oct 31, 2012 7:04 am

I have a text box in a form and I'm trying to capture when the return is hit an execute a go button on the form I have not been successful in getting it capture the enter instead it just executes the go button every time a key is pressed here is the code that I have(I think I'm on the right track but could be way off):

$textboxpath_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
if ($_.KeyChar -eq [System.Windows.Forms.Keys]::Enter)
{
$_.Handled = $true
}
}

User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Wed Oct 31, 2012 8:53 am

Well i got it kind of figured out
$textboxpath_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
if ($_.KeyChar -eq [System.Windows.Forms.Keys]::Enter)
{
&$buttonGo_Click
}
}
Found out that if this $textboxpath.AutoCompleteMode = 'Suggest' is set then the enter will not take the enter. Any workaround?

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Enter key to execute a button from a textbox

Post by davidc » Wed Oct 31, 2012 9:07 am

You can set the button as the Form's AcceptButton and it will trigger the specified button whenever the user presses 'Enter'. The only side effect is that the form will also close.

Another alternative is to use the Form's KeyPreviewDown event. You will need to set the Form's KeyPreview property to True. I recommend searching the web for C# examples on how to do this.

David
David
SAPIEN Technologies, Inc.

User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Thu Nov 01, 2012 1:39 am

Thanks Davidc I got it working here is the code I used in case anyone else runs in to the problem

$textboxpath_Keydownpreview=[System.Windows.Forms.PreviewKeyDownEventHandler]{
if ($_.KeyCode -eq [System.Windows.Forms.Keys]::Enter)
{
&$buttonGo_Click
}
}

User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Thu Nov 01, 2012 2:52 am

Well that will capture the enter key but now it is capturing ctrl+v and not pasting in the box.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Enter key to execute a button from a textbox

Post by davidc » Thu Nov 01, 2012 4:44 am

I believe I have an easier way. Remove the other events and replace it with the KeyDown event:

$textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
#TODO: Place custom script here
if($_.KeyCode -eq 'Enter')
{
&$buttonGo_Click
}
}

It triggers even if you have AutoComplete enabled. You can also use KeyUp event as an alternative.

David
David
SAPIEN Technologies, Inc.

User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Thu Nov 01, 2012 6:46 am

Your code still does not allow for pasting into the text box

User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Thu Nov 01, 2012 7:02 am

I got it working with

$textboxpath_KeyUp=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if(($_.Control -eq $true) -and ($_.KeyCode -eq 'V'))
{
$textboxpath.Paste()
}

}

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Enter key to execute a button from a textbox

Post by davidc » Thu Nov 01, 2012 7:28 am

Not sure why the copy paste doesn't work for you. I can't reproduce it on my end.

David
David
SAPIEN Technologies, Inc.

User avatar
imigj1
Posts: 18
Joined: Wed Oct 31, 2012 7:04 am

Getting Enter key to execute a button from a textbox

Post by imigj1 » Thu Nov 01, 2012 7:32 am

I figured it out some how ShortcutsEnabled was set to false changed it to true and it is working now thanks for all your help!!!