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.
User avatar
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

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

Post by phhebert » Wed Sep 26, 2018 9:06 am

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?

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Sep 26, 2018 9:29 am

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
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

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

Post by phhebert » Wed Sep 26, 2018 10:11 am

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

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Sep 26, 2018 10:35 am

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.

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Sep 26, 2018 10:40 am

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
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

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

Post by phhebert » Fri Sep 28, 2018 5:22 am

I finally make it work with both KeyPress and TextChanged Event! Thank you for your precious help.

Locked