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.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

Textbox cursor position is reset after programmatically changed text property

Post by aartogba »

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.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post 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.
User avatar
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

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

Post by aartogba »

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
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post 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!
User avatar
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

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

Post by aartogba »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
User avatar
aartogba
Posts: 15
Last visit: Tue Jan 23, 2024 8:00 am

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

Post by aartogba »

Ok so can you give me a simple example of using keystroke so I can figure it out in my own application?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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]
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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

Post 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# ... }
:
}
:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked