类型代替,

本文关键字:类型 | 更新日期: 2023-09-27 18:25:57

我有一个文本框,希望在其中键入.而不是,

if (e.KeyCode == Keys.Oemcomma) tbPrecio.Text = tbPrecio.Text.Split(',')[0]+".";

但它不能正常工作。

类型代替,

只需将处理程序方法更改为:

private void tbPrecio_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Oemcomma)
    {
        tbPrecio.AppendText(".");
        e.SuppressKeyPress = true;
    }
}

我认为这里的关键添加是SuppressKeyPress

在这种情况下,是否可以使用屏蔽输入?

http://digitalbush.com/projects/masked-input-plugin/

自动替换字符会让一些人感到害怕。

尝试此代码

private void tbPrecio_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Oemcomma)
            tbPrecio.Text = tbPrecio.Text.Replace(',','.');
        tbPrecio.Select(tbPrecio.Text.Length, 0);
}

但使用@Jason的逻辑作为最佳