dbncourt
dbncourt

Reputation: 580

Type . insted of ,

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

Answers (3)

Jason
Jason

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

Jaspreet Chahal
Jaspreet Chahal

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

Abe Miessler
Abe Miessler

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

Related Questions