KeyDown Event 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 6 years and 7 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
dank42
Posts: 61
Last visit: Tue Apr 26, 2022 7:49 am

KeyDown Event on TextBox

Post by dank42 »

Hi,

I have just followed this guide (https://www.sapien.com/blog/2015/08/17/ ... -a-button/) to invoke the click of a button by pressing 'ENTER'...everything is working fine, however, the text is removed from the textbox when pressing 'ENTER' and I need it to remain static as a second button reads the text from that box.

This is the code:

Code: Select all

$textbox_Username_KeyDown = [System.Windows.Forms.KeyEventHandler]{
	
	
	if ($_.KeyCode -eq 'Enter')
	{
		$buttonFind_Click.Invoke()
	}
	
	
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: KeyDown Event on TextBox

Post by jvierra »

Sorry but there is not enough information to understand your issue.
User avatar
dank42
Posts: 61
Last visit: Tue Apr 26, 2022 7:49 am

Re: KeyDown Event on TextBox

Post by dank42 »

I have a textbox called $textbox_username and the text is deleted when I use the ENTER button to invoke my button called $button_Find_Click but using the mouse to click $button_Find keeps the text in $textbox_username

The events for $textbox_username are as follows:

KeyDown textbox_Username_KeyDown

which is the KeyEventHandler:

$textbox_Username_KeyDown = [System.Windows.Forms.KeyEventHandler]{


if ($_.KeyCode -eq 'Enter')
{
$buttonFind_Click.Invoke()

}


}

Hope this is a bit clearer?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: KeyDown Event on TextBox

Post by jvierra »

You can use this to click a button:

Code: Select all

$textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	if ($_.KeyCode -eq 'Enter'){
		$button1.PerformClick()
    }
}
User avatar
dank42
Posts: 61
Last visit: Tue Apr 26, 2022 7:49 am

Re: KeyDown Event on TextBox

Post by dank42 »

I realized what it was...

The text box was set to "MultiLine" hence when I hit 'ENTER' it is adding a new line and pushing the text up :lol:
This topic is 6 years and 7 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