Textbox cursor position is reset after programmatically changed text property

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 5 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
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

Re: Textbox cursor position is reset after programmatically changed text property

Post by aartogba »

Hi,

Thank you all for your answers. I've used KeyPress event but now I'm facing another problem. When KeyPress event is triggered, it fires a validation function that looks for characters in the textbox that is updated, but the Text property is now empty. I think it relates to the fact that the Text property of the Control is updated after the event has occured?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Textbox cursor position is reset after programmatically changed text property

Post by jvierra »

Code: Select all

$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
    
    # code to do what you want.
    
    # to not send the character to the textbox set "Handled" to true.
    $_.Handled = $true
}
User avatar
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

Re: Textbox cursor position is reset after programmatically changed text property

Post by aartogba »

Well, I still want to send the chars to the textbox. Problem is that the Text property of the textbox isn't updated when I look at it in the KeyPress EventHandler. I just want to look at the first char in the textbox. If it's lower case then replacing it by the corresponding capital letter. The same is true for the rest of the string but lower it if it's upper case.

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Textbox cursor position is reset after programmatically changed text property

Post by jvierra »

The character does not get put into the textbox until after the event ends.

The character is available in the event object. Type "$_." to see the properties.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Textbox cursor position is reset after programmatically changed text property

Post by jvierra »

Code: Select all

$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
    
    # Allow only capital A-Z to be entered.
    if($_.KeyChar -cmatch '[A-Z]'){
        Write-Host $_.KeyChar
    }else{
        [System.Console]::Beep()
        $_.Handled =  $true
    }
    # to not send the character to the textbox set "Handled" to true.
    #$_.Handled = $true
}

User avatar
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

Re: Textbox cursor position is reset after programmatically changed text property

Post by aartogba »

I finally make it work with both KeyPress and TextChanged Event! Thank you for your precious help.
This topic is 5 years and 5 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