Page 1 of 1

"Enter" key on textbox

Posted: Thu Oct 03, 2013 9:06 am
by R1Johnny
Good afternoon,
I'd like to have the <Enter> key recognized after entering text into a textbox. I've set the "AcceptsReturn" to True but that doesn't seem to have the effect that I desire.
Can you please tell me how to have the textbox accept an <Enter> key after text is entered?

Thanks very much.
Regards,
John

Re: "Enter" key on textbox

Posted: Thu Oct 03, 2013 9:16 am
by davidc
If you want multiple lines of text, set the TextBox's Multiline property to True.

If you want <Enter> to trigger a script or button, then you use the control's KeyUp or KeyDown event and respond to the Enter key:
PowerShell Code
Double-click the code block to select all.
$textbox1_KeyUp=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	if($_.KeyCode -eq 'Enter')
	{
		&$button1_Click	
	}
}
For more info on the TextBox control refer to the follow article:

http://www.sapien.com/blog/2011/06/13/p ... x-control/

David

Re: "Enter" key on textbox

Posted: Thu Oct 03, 2013 9:18 am
by jvierra
You have to trap and handle the "keypress" event:
PowerShell Code
Double-click the code block to select all.
$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
	if($_.KeyChar -eq 13){
        [void][System.Windows.Forms.MessageBox]::Show('Enter key enterd'+$_.KeyChar)
    }
}

Re: "Enter" key on textbox

Posted: Thu Oct 03, 2013 9:19 am
by jvierra
Both methods - Daqvid's and mine - will work but have other uses too.

We both posted nearly simultaneously. :mrgreen:

Re: "Enter" key on textbox

Posted: Thu Oct 03, 2013 12:06 pm
by R1Johnny
Wow. That was tough to reverse-engineer. I'd never added/changed an event before so had to determine how to accomplish that first.

For anyone else in the same boat, create the textbox, then, from the designer screen, highlight the textbox, look for the textbox in the bottom left corner, highlight your textbox name, right-click and select "Add Events". From there the remainder of the suggestions should get you through.

Works great!!!

Thank you both very much.

Best regards,
John