disable a key press in a 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 5 years and 11 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
mhenders
Posts: 5
Last visit: Thu Dec 01, 2022 5:29 am

disable a key press in a textbox

Post by mhenders »

I want to disable the use of the period key within a textbox. How would I go about doing that?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: disable a key press in a textbox

Post by jvierra »

To filter keys on input add the following event:

Code: Select all

$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
    if($_.KeyChar -eq '.'){
        Write-Host 'Cancel keystroke'
        $_.Handled = $true
    }
}
This behavior is not clearly documented except in the MSDN documents and examples.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: disable a key press in a textbox

Post by mxtrinidad »

As the code above was provided, PowerShell Studio need first the register the event before adding the above code inside the event. (see image below)

:)
AddKeyPress_event_01_2018-04-03_11-54-02.jpg
AddKeyPress_event_01_2018-04-03_11-54-02.jpg (136.69 KiB) Viewed 2134 times
This topic is 5 years and 11 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