By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Replacing chars when typing

P: n/a
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.
Nov 18 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 18 Nov 2008 15:45:36 -0800, Trooper <tr*@nema.emailwrote:
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?
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
Nov 19 '08 #2

P: n/a
DH
Trooper wrote:
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?
Nov 19 '08 #3

P: n/a
Peter Duniho wrote:
>
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.
Nov 19 '08 #4

P: n/a
On Tue, 18 Nov 2008 16:02:48 -0800, Trooper <tr*@nema.emailwrote:
Peter Duniho wrote:
> 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.
If KeyChar is read-only, then you are not actually handling the KeyPress
event as your original post claimed.

http://msdn.microsoft.com/en-us/libr...s.keychar.aspx
Nov 19 '08 #5

P: n/a
DH wrote:
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 '.'
Nov 19 '08 #6

P: n/a
Trooper skrev:
DH wrote:
>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'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.

--
Bjørn Brox
Nov 19 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.