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.
User avatar
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

Textbox cursor position is reset after programmatically changed text property

Post by phhebert » Fri Sep 21, 2018 9:14 am

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
Site Admin
Posts: 212
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Fri Sep 21, 2018 9:48 am

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
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

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

Post by phhebert » Fri Sep 21, 2018 10:05 am

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
Site Admin
Posts: 212
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Fri Sep 21, 2018 10:17 am

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
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

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

Post by phhebert » Fri Sep 21, 2018 12:28 pm

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.

User avatar
jvierra
Posts: 12756
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Sep 21, 2018 2:03 pm

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
phhebert
Posts: 15
Joined: Mon Sep 26, 2016 8:24 am

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

Post by phhebert » Fri Sep 21, 2018 2:09 pm

Ok so can you give me a simple example of using keystroke so I can figure it out in my own application?

User avatar
jvierra
Posts: 12756
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Sep 21, 2018 2:38 pm

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
Site Admin
Posts: 212
Joined: Sun Mar 03, 2013 12:42 pm

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

Post by mxtrinidad » Fri Sep 21, 2018 3:37 pm

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# ... }
:
}
:)

User avatar
jvierra
Posts: 12756
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Sep 21, 2018 3:44 pm

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.

Post Reply