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?
Textbox cursor position is reset after programmatically changed text property
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.
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.
Re: Textbox cursor position is reset after programmatically changed text property
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
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
Thanks
Re: Textbox cursor position is reset after programmatically changed text property
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.
The character is available in the event object. Type "$_." to see the properties.
Re: Textbox cursor position is reset after programmatically changed text property
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
I finally make it work with both KeyPress and TextChanged Event! Thank you for your precious help.