Replacing chars when typing

  • Thread starter Thread starter Trooper
  • Start date Start date
T

Trooper

Hi!

I want when user type comma in textbox to
automaticly replace in dot.

In keypress event i have something like this:

if (e.KeyChar == 44)
{
textBox1.Text = textBox1.Text.Replace(",",".");
}

but it doesn't work in way that i want. Is there any
way to do that?

Thnx.
 
Trooper said:
Hi!

I want when user type comma in textbox to
automaticly replace in dot.

In keypress event i have something like this:

if (e.KeyChar == 44)
{
textBox1.Text = textBox1.Text.Replace(",",".");
}

but it doesn't work in way that i want. Is there any
way to do that?

Thnx.
I would do it in the TextChanged event handler and just
textBox1.Text=textBox1.Text.Replace(',','.');

that is assuming it is ok for it to replace all the ',' with '.' once
the user has finished entering what ever they are entering.

what exactly do you mean by it doesn't work the way you want it to?
What do you expect and what does it do?
 
Peter said:
Just change the KeyChar property in your event handler. When it's equal
to ',', set it to '.' (and use the character literals, not ASCII codes).

Pete


Did you mean to put e.KeyChar = Keys.Decimal; ??
KeyChar property is read-only and i can't assign anything.
 
DH said:
I would do it in the TextChanged event handler and just
textBox1.Text=textBox1.Text.Replace(',','.');

This works fine, but it moves cursor at begining of textbox.
that is assuming it is ok for it to replace all the ',' with '.' once
the user has finished entering what ever they are entering.

what exactly do you mean by it doesn't work the way you want it to?
What do you expect and what does it do?

All I want to do is when user type ',' it automaticly change to '.'
I want to intercept ',' and replace it with '.'
 
Trooper skrev:
This works fine, but it moves cursor at begining of textbox.
That's because you replace textBox1.Text with a new value (the only
thing you are allowed to)

Store the cursor position before modifying the text, and then restore it.
 
Back
Top