Reputation: 580
I have a textbox in which I want the .
be typed instead of the ,
.
if (e.KeyCode == Keys.Oemcomma) tbPrecio.Text = tbPrecio.Text.Split(',')[0]+".";
But it doesn't work properly.
Upvotes: 0
Views: 91
Reputation: 6926
Simply change your handler method to:
private void tbPrecio_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Oemcomma)
{
tbPrecio.AppendText(".");
e.SuppressKeyPress = true;
}
}
I think the key addition here is SuppressKeyPress
.
Upvotes: 3
Reputation: 2750
try this code
private void tbPrecio_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Oemcomma)
tbPrecio.Text = tbPrecio.Text.Replace(',','.');
tbPrecio.Select(tbPrecio.Text.Length, 0);
}
But use @Jason's Logic as that's optimal
Upvotes: 0
Reputation: 85116
Is this a situation where you could use a masked input instead?
http://digitalbush.com/projects/masked-input-plugin/
Automatically replacing characters freaks some people out.
Upvotes: 1