C# 如何阻止文本框中的第一个字符为'';?

C# 如何阻止文本框中的第一个字符为'';?,c#,.net,winforms,textbox,keypress,C#,.net,Winforms,Textbox,Keypress,这是我目前拥有的代码: private void textBox_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.'; if (e.KeyChar == '.' && (sender as TextBox).Text.I

这是我目前拥有的代码:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.';
    if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) e.Handled = true; 

}

KeyPress不足以进行这种验证。绕过它的一个简单方法是使用Ctrl+V或上下文菜单将文本粘贴到文本框中,而不使用任何键事件

在此特定情况下,TextChanged事件将完成作业:

    private void textBox_TextChanged(object sender, EventArgs e) {
        var box = (TextBox)sender;
        if (box.Text.StartsWith(".")) box.Text = "";
    }
但验证数值还有很多工作要做。你还需要拒绝像1.1.1或1.-2之类的东西。改为使用验证事件。在表单上删除ErrorProvider并执行如下事件:

    private void textBox_Validating(object sender, CancelEventArgs e) {
        var box = (TextBox)sender;
        decimal value;
        if (decimal.TryParse(box.Text, out value)) errorProvider1.SetError(box, "");
        else {
            e.Cancel = true;
            box.SelectAll();
            errorProvider1.SetError(box, "Invalid number");
        }
    }

KeyPress不足以进行这种验证。绕过它的一个简单方法是使用Ctrl+V或上下文菜单将文本粘贴到文本框中,而不使用任何键事件

在此特定情况下,TextChanged事件将完成作业:

    private void textBox_TextChanged(object sender, EventArgs e) {
        var box = (TextBox)sender;
        if (box.Text.StartsWith(".")) box.Text = "";
    }
但验证数值还有很多工作要做。你还需要拒绝像1.1.1或1.-2之类的东西。改为使用验证事件。在表单上删除ErrorProvider并执行如下事件:

    private void textBox_Validating(object sender, CancelEventArgs e) {
        var box = (TextBox)sender;
        decimal value;
        if (decimal.TryParse(box.Text, out value)) errorProvider1.SetError(box, "");
        else {
            e.Cancel = true;
            box.SelectAll();
            errorProvider1.SetError(box, "Invalid number");
        }
    }

您可能希望使用TextChanged事件,因为用户可以粘贴值。为了获得给定需求的最佳体验,我建议只需删除任何前导的
字符

void textBox1_TextChanged(object sender, EventArgs e)
{
  if (textBox1.Text.StartsWith("."))
  {
    textBox1.Text = new string(textBox1.Text.SkipWhile(c => c == '.').ToArray());
  }
}

这并没有满足只使用数字的要求——如果是这样的话,这个问题还不清楚。

您可能想使用TextChanged事件,因为用户可以粘贴值。为了获得给定需求的最佳体验,我建议只需删除任何前导的
字符

void textBox1_TextChanged(object sender, EventArgs e)
{
  if (textBox1.Text.StartsWith("."))
  {
    textBox1.Text = new string(textBox1.Text.SkipWhile(c => c == '.').ToArray());
  }
}

这并没有解决只使用数字的要求——如果是这样的话,问题还不清楚。

这也适用于复制和粘贴

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        int decimalCount=0;
        string rebuildText="";
        for(int i=0; i<textBox1.Text.Length; i++)
        {
            if (textBox1.Text[i] == '.')
            {
                if (i == 0) break;
                if (decimalCount == 0)
                    rebuildText += textBox1.Text[i];
                decimalCount++;
            }
            else if ("0123456789".Contains(textBox1.Text[i]))
                rebuildText += textBox1.Text[i];
        }
        textBox1.Text = rebuildText;    
        textBox1.SelectionStart = textBox1.Text.Length;

    }
private void textBox1\u KeyUp(对象发送方,KeyEventArgs e)
{
整数小数=0;
字符串重建文本=”;

对于(int i=0;i这也适用于复制和粘贴

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        int decimalCount=0;
        string rebuildText="";
        for(int i=0; i<textBox1.Text.Length; i++)
        {
            if (textBox1.Text[i] == '.')
            {
                if (i == 0) break;
                if (decimalCount == 0)
                    rebuildText += textBox1.Text[i];
                decimalCount++;
            }
            else if ("0123456789".Contains(textBox1.Text[i]))
                rebuildText += textBox1.Text[i];
        }
        textBox1.Text = rebuildText;    
        textBox1.SelectionStart = textBox1.Text.Length;

    }
private void textBox1\u KeyUp(对象发送方,KeyEventArgs e)
{
整数小数=0;
字符串重建文本=”;
对于(int i=0;i您可以尝试以下方法:

private void TextBox_TextChanged(object sender, EventArgs e)        
{        
        TextBox.Text = TextBox.Text.TrimStart('.');        
}
您可以尝试以下方法:

private void TextBox_TextChanged(object sender, EventArgs e)        
{        
        TextBox.Text = TextBox.Text.TrimStart('.');        
}

这段代码有什么不起作用呢?我想输入一个双变量。例如“1.1”。我想阻止它输入“.1”你是说你现在的代码阻止你在文本框的任何地方输入
,但是你只想阻止第一个字符成为
?我现在的代码允许我在表单的任何地方输入它,并阻止它输入多个字符。但是我想阻止它作为第一个字符输入在文本框中,确保你考虑当有人把一个数字粘贴到文本框中的情况。请不要通过禁用粘贴来解决它。什么对这个代码不起作用?我想输入一个双变量。例如,“1.1”…我想阻止它进入“1”。你是说你现在的代码阻止你在文本框的任何地方输入
,但是你只想阻止第一个字符成为
?我现在的代码允许我在表单的任何地方输入它,并阻止它输入多个字符。但是我想阻止它作为第一个字符输入R在文本框中确保你考虑的是当有人把一个数字粘贴到文本框中的时候。请不要通过禁用粘贴来解决它。谢谢你的回答,它是有效的,也感谢Ctrl + V感谢你的答案的建议,它是有效的,并且感谢CTRL+VTIs的建议不局限于一个。ng.从你的评论中,哪一个似乎是你想要的。但它回答了主要问题。这并不局限于一个“.”。从你的评论中,哪一个似乎是你想要的。但它回答了主要问题。