Dim MyKey as string
if key=chr(44) then ' Cambia da virgola a punto
MyKey=chr(46)
messagebox " OK ??"
else
MyKey = key
end if ' Continue the code using MyKey
Instead of doing that in KeyUp, do this in Keydown:
Function KeyDown(key As String) Handles KeyDown as Boolean
if key=chr(44) then 'Cambia da virgola a punto
'key=chr(46)
If me.SelectionStart < me.text.Length then
me.text = me.text.Left(me.Selectionstart) + chr(46) + me.text.Right(me.text.Length-me.SelectionStart)
else
me.text = me.text + chr(46) //dot
end if
me.selectionstart = me.selectionstart + 1
return true
end if
End Function
This will insert the dot instead of the coma. The code takes into account cases when the insertion is at the right of text, or in the middle.
it doesn’t work for me, I understand what you are trying to do … but when I press the comma, it puts the period but it doesn’t move to the right position, it moves to the second digit after the start.
if key=chr(46) then 'if dot
If me.SelectionStart < me.text.Length then
me.text = me.text.Left(me.Selectionstart) + chr(44) + me.text.Right(me.text.Length+me.SelectionStart)
else
me.text = me.text + chr(44) //comma
end if
me.selectionstart = me.selectionstart + me.text.Length
return true
end if
I tested the code before posting. It behaves as expected. The caret moves one position after the inserted dot, as it would with any other character.
Perhaps that works when there is nothing in the textfield, but when the caret is in the middle of text (If me.SelectionStart < me.text.Length), that won’t work.
If key=chr(44) Then //you can use ","
Var s As Integer=Me.SelectionStart
Me.SelectedText=chr(46) //you can use "."
Me.SelectionStart=s+1
Return True
End If