KeyPress event handling not working on MaskedTextBox

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 2 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
loganstanford
Posts: 2
Last visit: Tue Nov 29, 2022 7:12 am

KeyPress event handling not working on MaskedTextBox

Post by loganstanford »

PowerShell Studio 2021 Version 5.8.190
Windows 10 x64 Build 18363.1679

I am attempting to validate the key presses in a masked textbox to fit the characters for a MAC address. I only want to allow the characters A-F and 0-9 to be typed in the field. Here is the code for the KeyPress event.

  1. $maskedtextboxMAC_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
  3.  
  4.     $_.KeyChar = $_.KeyChar.ToString().ToUpper().ToCharArray()[0]
  5.     if ($_.KeyChar -notmatch "[A-F0-9]")
  6.     {
  7.         $_.Handled = $true
  8.     }
  9. }

This code works perfectly fine on a standard text box but it does not seem to work on a MaskedTextBox. I copy and pasted it directly to another TextBox event handler and it worked as expected. I have used the debugger to verify that the event is running on a keypress, the regex is working as expected, and that the Handled property is getting set to $true but unmatched characters are still entered into the MaskedTextBox. Any ideas what may be causing this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: KeyPress event handling not working on MaskedTextBox

Post by jvierra »

This shuold help you understand why you might be having problems.
  1. $maskedtextbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
  3.         if ($_.KeyChar -match '[A-F0-9]'){
  4.             #Matches template
  5.         }else{
  6.             # Doesn't match template
  7.             $_.Handled = $true
  8.         }
  9. }
loganstanford
Posts: 2
Last visit: Tue Nov 29, 2022 7:12 am

Re: KeyPress event handling not working on MaskedTextBox

Post by loganstanford »

This doesn't work either. Again, I've debugged step by step to make sure that the logic is working as intended and that the Handled property is being set to $true. It just seems that $_.Handled -eq $true is not keeping to key press from showing up in the maskedtextbox the way that it would in a normal textbox
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: KeyPress event handling not working on MaskedTextBox

Post by Alexander Riedel »

https://stackoverflow.com/questions/202 ... vent-input

Seems you are not alone. I assume it is a problem in the .NET framework. Try to use the other event as indicated in the stackoverflow post.
Alexander Riedel
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: KeyPress event handling not working on MaskedTextBox

Post by jvierra »

loganstanford wrote: Thu Aug 12, 2021 10:18 am This doesn't work either. Again, I've debugged step by step to make sure that the logic is working as intended and that the Handled property is being set to $true. It just seems that $_.Handled -eq $true is not keeping to key press from showing up in the maskedtextbox the way that it would in a normal textbox
It works without issue when I use the code posted above. Perhaps you should start by building a new form and just try that simple code.

Remember the match is positive for the template so the characters in the template will be excepted and all otheres will be rejected.

Also the Net Framework may need updating although I have never seen such a simple issue like this be dependent on Net issues.
This topic is 2 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