Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型。而不是,_C#_Visual Studio 2010_Textbox - Fatal编程技术网

C# 类型。而不是,

C# 类型。而不是,,c#,visual-studio-2010,textbox,C#,Visual Studio 2010,Textbox,我有一个文本框,希望在其中键入,而不是, 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.Ap

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

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

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

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

我认为这里添加的键是
SuppressKeyPress

这是一种可以使用屏蔽输入的情况吗


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

这是一种可以使用屏蔽输入的情况吗

自动替换字符会吓坏一些人。

试试这段代码

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的逻辑是最佳的

试试下面的代码

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的逻辑,因为这是最佳的

,但这不会取代,因为。我猜它会追加。哦,是的,它不会用句号替换任何现有的逗号。但它不会用句号替换。我猜它会追加。哦,是的,它不会用句号替换任何现有的逗号。我同意,替换字符可能是个坏主意,它会破坏你和访问者之间的信任;阻止输入字符是可以理解的,但仍然令人沮丧;但替换角色只会激怒访问者,因为你在操纵他们的浏览器的功能。一致性是神圣的,会干扰输入和预期输出,以最基本的方式剥夺用户的控制权,只会让他们有理由不再访问你的网站。我同意,替换字符可能是个坏主意,它会破坏你和访问者之间的信任;阻止输入字符是可以理解的,但仍然令人沮丧;但替换角色只会激怒访问者,因为你在操纵他们的浏览器的功能。一致性是神圣的,它扰乱了输入和预期输出,以最基本的方式剥夺了用户的控制权,只会让他们有理由不再访问您的站点。