"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.
This topic is 10 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
R1Johnny
Posts: 37
Last visit: Mon Dec 12, 2022 7:15 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
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Mon Dec 12, 2022 7:15 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
This topic is 10 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked