Hi everyone,
I'm having a hard time to code this one. I have to validate input on a textbox control and everytime I correct the input by setting the text property of my textbox in the textchanged event handler, the cursor position is reset to 0. Even worse, the event handler is retriggered because of that change. How can I handle this?! Please help.
Thank you.
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.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Textbox cursor position is reset after programmatically changed text property
Based in what you're saying, something is missing. Why are you doing a textchange event when you're using a Input Textbox?
I would just add the validation at the button event to make sure is correct. You won't move from that textbox unless is properly validated.
I don't think you need to use the TextChanged event,unless you're pulling data from somewhere else in order to make corrections. Then, this is a different story.
I would just add the validation at the button event to make sure is correct. You won't move from that textbox unless is properly validated.
I don't think you need to use the TextChanged event,unless you're pulling data from somewhere else in order to make corrections. Then, this is a different story.
Re: Textbox cursor position is reset after programmatically changed text property
Sorry I didn't explain the whole thing.
Well, I'm using textchanged event because I want to dynamically update it when user is entering text because this textbox is filling two others textboxes. In fact, there are two textboxes called $txtFirstName and $lastname which fills ID and email textboxes as soon as we do any changes in them. I want to force a specific input which is a capital letter for the first letter and avoid any accented characters.
Thanks
Well, I'm using textchanged event because I want to dynamically update it when user is entering text because this textbox is filling two others textboxes. In fact, there are two textboxes called $txtFirstName and $lastname which fills ID and email textboxes as soon as we do any changes in them. I want to force a specific input which is a capital letter for the first letter and avoid any accented characters.
Thanks
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Textbox cursor position is reset after programmatically changed text property
I still think you don't need a TextChanged event. You can loop thru the boxes doing validation, then only post the desired result when everything is validated.
During the validation you can write back to the textbox. Also, you could change Button label to display during the validation process.
You can be very creative!
During the validation you can write back to the textbox. Also, you could change Button label to display during the validation process.
You can be very creative!
Re: Textbox cursor position is reset after programmatically changed text property
This is the way I did it before using TextChanged event but the only way to fire the Validated event was to get out of the control and I did't want that behavior unless I missed something there? I just wanted to validate textboxes as soon as something has changed and immediately fill other textboxes.
Re: Textbox cursor position is reset after programmatically changed text property
To dynamically edit a textbox we usually use the keystroke events check each character.
The problem with TextChanged is that changing the text from this event causes a recursive chain of events that is very hard to control.
The problem with TextChanged is that changing the text from this event causes a recursive chain of events that is very hard to control.
Re: Textbox cursor position is reset after programmatically changed text property
Ok so can you give me a simple example of using keystroke so I can figure it out in my own application?
Re: Textbox cursor position is reset after programmatically changed text property
Place you code in the following event:
Code: Select all
$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
}
[code]
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Textbox cursor position is reset after programmatically changed text property
Thanks Jvierra!
Or, for looking at specific character(s):
Or, for looking at specific character(s):
Code: Select all
$textbox1_KeyPress = [System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
if ($_.KeyChar -eq 'character_here') { ... #validation code# ... }
:
}
Re: Textbox cursor position is reset after programmatically changed text property
Another thing to consider for per-character validation is the MaskedTextBox control which, when combined with the "KeyPress" event allows for simple pre-validation.
Without a purpose for this it is impossible to advise on the correct controls and methods for accomplishing the end purpose.
Without a purpose for this it is impossible to advise on the correct controls and methods for accomplishing the end purpose.