Page 2 of 2

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

Posted: Wed Sep 26, 2018 9:06 am
by phhebert
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?

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

Posted: Wed Sep 26, 2018 9:29 am
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
}

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

Posted: Wed Sep 26, 2018 10:11 am
by phhebert
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

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

Posted: Wed Sep 26, 2018 10:35 am
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.

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

Posted: Wed Sep 26, 2018 10:40 am
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
}


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

Posted: Fri Sep 28, 2018 5:22 am
by phhebert
I finally make it work with both KeyPress and TextChanged Event! Thank you for your precious help.