Replace character

hello, I would like to replace when I write, the period with a comma …
In txtfield KeyDown…

if key=chr(46) then
end if

But not work.

Generally speaking, I think when you detect the period key inside of .KeyDown, you will need to append the comma to the control’s value programmatically, then return True from .KeyDown, which will prevent the period from reaching the control.

Offhand, I’d say to do your character substitution in the TextChanged event of the control.

TextChanged is better than KeyDown. If the user pastes something in the field then KeyDown won’t catch it where TextChanged will.

1 Like

…and how do I capture the character in TextChanged since I only have the index and not the key pressed?

If it is a short string, read it in, replace all periods with commas and write it back out again on every event fire.

Dim pos As Integer = Me.SelectionStart 'remember where the caret was
Me.Text = Me.Text.ReplaceAll(".", ",")
Me.SelectionStart = pos 'put the caret back where it was

If it is a long string, it is a little more complicated if you don’t want to brute force it.

1 Like