"Enter" key on textbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
R1Johnny
Posts: 37
Joined: Thu Aug 18, 2011 12:22 am

"Enter" key on textbox

Post 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

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

Re: "Enter" key on textbox

Post 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
David
SAPIEN Technologies, Inc.

jvierra
Posts: 14578
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: "Enter" key on textbox

Post 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)
    }
}

jvierra
Posts: 14578
Joined: Tue May 22, 2007 9:57 am
Answers: 1
Been upvoted: 1 time
Contact:

Re: "Enter" key on textbox

Post by jvierra »

Both methods - Daqvid's and mine - will work but have other uses too.

We both posted nearly simultaneously. :mrgreen:

User avatar
R1Johnny
Posts: 37
Joined: Thu Aug 18, 2011 12:22 am

Re: "Enter" key on textbox

Post 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

Locked