How do you use . as Tab

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 8 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
TheMoist
Posts: 9
Last visit: Tue Nov 03, 2020 5:45 am

How do you use . as Tab

Post by TheMoist »

I am creating a form that people will be entering IP addresses into. To make it more simple for them - and harder for me, apparently - I need to give the form the ability to tab to the next box when a person presses the "." key on the numpad. Is this possible?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How do you use . as Tab

Post by jvierra »

To do this you have to handle all keyboard characters in the controls of interest. Pass all characters and react when you receive a dot.
PowerShell Code
Double-click the code block to select all.
$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
	#TODO: Place custom script here
	if($_.KeyChar -eq '.'){
        #[System.Windows.Forms.MessageBox]::Show('trapped')
        $textbox2.Focus()
	}
}
User avatar
TheMoist
Posts: 9
Last visit: Tue Nov 03, 2020 5:45 am

Re: How do you use . as Tab

Post by TheMoist »

So, using the code you have there, if I have the first box called $Octet1 and the next in line is called $Octet2, would it look like this?

Code: Select all

$Octet1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
    #TODO: Place custom script here
    if($_.KeyChar -eq '.'){
        #[System.Windows.Forms.MessageBox]::Show('trapped')
        $Octet2.Focus()
    }
}
I assume that I would add this code to the main function section of the script and then add some sort of event handler to the $Octet1 section of the form, correct?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How do you use . as Tab

Post by jvierra »

Just crate the event handler and paste the code into it. The event handler names will follow the object.

I created the $textbox1 event handler in the designer. I then added the reference to the $textbox2 object.

It seems pretty obvious to me.
User avatar
TheMoist
Posts: 9
Last visit: Tue Nov 03, 2020 5:45 am

Re: How do you use . as Tab

Post by TheMoist »

Okay, you rock. It's working, but I have one small issue. When you press the . key, it actually types the . before it tabs to the next section. How do I tell it to remove the actual .?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: How do you use . as Tab

Post by davidc »

Set the following in the event when you detect a period:
PowerShell Code
Double-click the code block to select all.
$_.Handled = $true
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: How do you use . as Tab

Post by jvierra »

Thanks David - forgot to do that.
User avatar
TheMoist
Posts: 9
Last visit: Tue Nov 03, 2020 5:45 am

Re: How do you use . as Tab

Post by TheMoist »

Thank you both so much for your help. It's working great! I am new here, so forgive me for asking stupid questions, please!!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How do you use . as Tab

Post by jvierra »

Not a stupid question.

Certain techniques like key handling are not obvious. Unfortunately there is no PowerShell book on Windows Forms development.

Try using MSDN and search for the control class.
This topic is 10 years and 8 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