Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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中的数字#_C# - Fatal编程技术网

C# 将文本框输入限制为C中的数字#

C# 将文本框输入限制为C中的数字#,c#,C#,我想限制文本框只接受C#中的数字。我该怎么做?您在WinForms中尝试过MaskedTextBox控件吗?看看类似的东西 最粗俗、最肮脏的做法是这样做: public partial class Form1 : Form { public Form1() { InitializeComponent(); this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

我想限制文本框只接受C#中的数字。我该怎么做?

您在WinForms中尝试过MaskedTextBox控件吗?

看看类似的东西


最粗俗、最肮脏的做法是这样做:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !char.IsDigit(e.KeyChar);
    }

}

这种方法仍然不安全,因为用户仍然可以将非数字字符复制并粘贴到文本框中。不管怎样,您仍然需要验证您的数据。

您可以使用AJAX控件工具包的。使用此选项可以允许/禁止任何字符类型。这是非常灵活的。

其他人在我之前说过,我们几乎必须知道您将在WinForms、ASP.NET、Silverlight中使用它

但现在我冒一个险,那就是WinForm:)


您可以使用Microsoft.VisualBasic.Information.IsNumeric是数字函数。只需添加引用Microsoft.VisualBasic

private void textBox1_Validating(object sender,
        System.ComponentModel.CancelEventArgs e) {
    if (!Microsoft.VisualBasic.Information.IsNumeric(textBox1.Text)) {
        e.Cancel = true;
    } else {
        // Do something here
    }
}

这允许用户输入科学记数法,这不是一个简单的筛选方法。

不确定这是否是正确的方法,但它对我有效,在TextChanged事件文本框上运行它有:

    private void CoordinateValidation(object sender, TextChangedEventArgs e) {
        TextBox inputBox = e.OriginalSource as TextBox;
        inputBox.TextChanged -= CoordinateValidation;
        int caretPos = inputBox.CaretIndex;
        foreach (TextChange change in e.Changes) {
            if (inputBox.Text.Substring(change.Offset, change.AddedLength).Any(c => !ValidChars.Contains(c)) ||
                inputBox.Text.Count(c => c == '.') > 1 ||
                (inputBox.Text.Length > 0 && inputBox.Text.Substring(1).Contains('-'))) {
                inputBox.Text = inputBox.Text.Remove(change.Offset, change.AddedLength);
                caretPos -= change.AddedLength;
            }
        }
        inputBox.CaretIndex = caretPos;
        inputBox.TextChanged += CoordinateValidation;
    }

这可能不是最好的方法,但对于WPF文本框来说效果很好

    private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        string originalText = ((TextBox)sender).Text.Trim();
        if (originalText.Length>0)
        {
            int inputOffset = e.Changes.ElementAt(0).Offset;
            char inputChar = originalText.ElementAt(inputOffset);
            if (!char.IsDigit(inputChar))
            {
                ((TextBox)sender).Text = originalText.Remove(inputOffset, 1);
            }
        }
    }

有一些错误,但简单的方法:D

 private void textbox1_TextChanged(object sender, EventArgs e)
        { 
            char[] originalText = textbox1.Text.ToCharArray();
            foreach (char c in originalText)
            {
                if (!(Char.IsNumber(c)))
                {
                    textbox1.Text = textbox1.Text.Remove(textbox1.Text.IndexOf(c));
                    lblError.Visible = true;
                }
                else
                    lblError.Visible = false;
            }
            textbox1.Select(textbox1.Text.Length, 0);
        }

看看下面的代码

private void txtbox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        const char Delete = (char)8;
        e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
    }

这将很有帮助,但用户可以复制和粘贴字符:-)

HTML?Windows窗体?WPF?Silverlight?如果与C#相关,可能是一个愚蠢的建议,但如果合适,请评估NumericUpDown控件。为什么要使用外部工具来实现内置的功能?如果他在这里谈论Web,那么只需在普通文本框中创建javascript函数,这样就只能输入数字。当然,它在服务器端进行了retrospect检查。因为客户端永远不会安全。因为您可以使用相同的头发送请求。我看不出在这里使用ajax有什么好处……好处是只需在页面上删除一个控件,然后忘记它,特别是如果您已经在使用ajax控件工具包。用户也不能删除字符。@LucasB,除非他们剪切。:)这很好,我建议的唯一更改是同时启用退格字符:这很好,我建议的唯一更改是同时启用退格字符:private void textBox1_KeyPress(object sender,KeyPressEventArgs e){e.Handled=!(char.IsDigit(e.KeyChar)| e.KeyChar==8)}这不允许退格,也不是这里要问的问题。但是如果你需要的话。。如果(e.KeyChar=vbKeyBack){return;}
private void txtbox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        const char Delete = (char)8;
        e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
    }