Tab Key = Button Click

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 8 years and 2 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
aeremis74
Posts: 18
Last visit: Tue Sep 17, 2019 4:22 am

Tab Key = Button Click

Post by aeremis74 »

I have written an app, and its all good. I did get a request though. I have a text box. When you are in the text box and you hit the enter key it activates a button I created. That works great. The request I have is that if I am in a text box and I hit the "tab" key, it would be nice if that also activated the same enter key I have defined. I tried a few different things but I'm not sure if I'm doing it. It's not working. This is what I've done....

In the events of the text box I changed the "KeyDown" event to PCText_KeyDown.

Then I added this:

$PCText_KeyDown = [System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if ($_.KeyCode -eq 'Tab')
{
&$EnterButton_Click
}
}

It doesn't seem to be working though. What else am I missing?




Product, version and build: PowerShell Studio 2015 x64
32 or 64 bit version of product: 64
Operating system: Windows 7 Professional
32 or 64 bit OS: 64 bit
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Tab Key = Button Click

Post by DevinL »

There are a couple settings that need to be changed in order to do this but it is possible. First, select your TextBox and make sure that Multiline and AcceptsTab are both set to true.

Lastly, be sure to add $_.Handled = $true at the end of the event handler to indicate it's been handled.
Tab_Button.png
Tab_Button.png (17.15 KiB) Viewed 5808 times
DevinL
SAPIEN Technologies, Inc.
User avatar
aeremis74
Posts: 18
Last visit: Tue Sep 17, 2019 4:22 am

Re: Tab Key = Button Click

Post by aeremis74 »

Like this?

The problem I have now is that it just tabs in the field and doesn't execute the button. Thoughts?

$PCText_KeyUp=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if ($_.KeyCode -eq 'Enter')
{
&$EnterButton_Click
}
$_.Handled = $true
}

$PCText_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if ($_.KeyCode -eq 'TAB')
{
&$EnterButton_Click
}
$_.Handled = $true
}
User avatar
aeremis74
Posts: 18
Last visit: Tue Sep 17, 2019 4:22 am

Re: Tab Key = Button Click

Post by aeremis74 »

[img]
Capture.JPG
Capture.JPG (19.12 KiB) Viewed 5762 times
[/img]

Do I need to change these settings?
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Tab Key = Button Click

Post by DevinL »

[POST MOVED TO POWERSHELL GUI FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Tab Key = Button Click

Post by DevinL »

Let me find out for sure and get back to you. In the meantime I've moved the thread to the PowerShell GUI's Forum in case anyone else can provide some help.
DevinL
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: Tab Key = Button Click

Post by jvierra »

You can make the button the next tab in the sequence.
You can use the validating event to control the results.
You can use the "lost focus" event to force the button to be next.

If a control accepts tabs then the tab action will take precedence over other events.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Tab Key = Button Click

Post by DevinL »

In my tests I couldn't get it to avoid the tab action just as Jvierra said above.

As for it running $Enter_ButtonClick, make sure 'Tab' isn't in all caps.
DevinL
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: Tab Key = Button Click

Post by jvierra »

Well - it can be done but we have to use a different approach.

Here is a demo. Type some text an tab or enter and watch the button.
Reselecting the textbox resets the button until you type tab or enter again.
Attachments
Demo-TrapTab.psf
(26.29 KiB) Downloaded 326 times
This topic is 8 years and 2 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