Page 1 of 2

Textbox cursor position is reset after programmatically changed text property

Posted: Fri Sep 21, 2018 9:14 am
by phhebert
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.

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

Posted: Fri Sep 21, 2018 9:48 am
by mxtrinidad
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.

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

Posted: Fri Sep 21, 2018 10:05 am
by phhebert
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

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

Posted: Fri Sep 21, 2018 10:17 am
by mxtrinidad
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!

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

Posted: Fri Sep 21, 2018 12:28 pm
by phhebert
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

Posted: Fri Sep 21, 2018 2:03 pm
by jvierra
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.

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

Posted: Fri Sep 21, 2018 2:09 pm
by phhebert
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

Posted: Fri Sep 21, 2018 2:38 pm
by jvierra
Place you code in the following event:

Code: Select all

$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
	
}
[code]

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

Posted: Fri Sep 21, 2018 3:37 pm
by mxtrinidad
Thanks Jvierra!

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

Posted: Fri Sep 21, 2018 3:44 pm
by jvierra
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.