C# 限制文本框仅允许十进制数字

C# 限制文本框仅允许十进制数字,c#,.net,wpf,validation,textbox,C#,.net,Wpf,Validation,Textbox,我需要制作一个十进制文本框(货币文本框),该文本框: 仅允许数字0-9(允许上numpad键0-9和右numpad键 键0-9) 只允许一个在开始时不显示的点 有效期: 0.5 一, 1.5000 无效: .5 5.500.55 编辑 我的代码是: private void floatTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !TextBox

我需要制作一个十进制文本框(货币文本框),该文本框:

  • 仅允许数字0-9(允许上numpad键0-9和右numpad键 键0-9)
  • 只允许一个在开始时不显示的点
  • 有效期:

    • 0.5
    • 一,
    • 1.5000
    无效:

    • .5
    • 5.500.55
    编辑

    我的代码是:

     private void floatTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !TextBoxValidation.AreAllValidDecimalChars(e.Text);
        }
    
    
      public static class TextBoxValidation
    {
        public static bool AreAllValidDecimalChars(string str)
        {
            bool ret = false;
            int x = 0;
            foreach (char c in str)
            {
                x = (int)c;
            }
            if (x >= 48 && x <= 57 || x == 46)
            {
                ret = true;
            }
            return ret;
        }
    }
    
    private void floatTextBox\u预览输出(对象发送方,文本组合ventargs e)
    {
    e、 Handled=!TextBoxValidation.AreAllValidDecimalChars(e.Text);
    }
    公共静态类TextBoxValidation
    {
    公共静态bool AreAllValidDecimalChars(字符串str)
    {
    bool-ret=假;
    int x=0;
    foreach(str中的字符c)
    {
    x=(int)c;
    }
    
    如果(x>=48&&x您可以通过在
    文本框上实现两个事件处理程序来实现您的目标:

    TextCompositionEventHandler textBox_PreviewTextInput = 
        new TextCompositionEventHandler((s, e) => e.Handled = !e.Text.All(
        c => Char.IsNumber(c) || c == '.'));
    KeyEventHandler textBox_PreviewKeyDown = new KeyEventHandler(
        (s, e) => e.Handled = e.Key == Key.Space);
    
    textBox.PreviewTextInput += textBox_PreviewTextInput;
    textBox.PreviewKeyDown += textBox_PreviewKeyDown;
    

    只需在
    文本框上实现两个事件处理程序即可实现目标:

    TextCompositionEventHandler textBox_PreviewTextInput = 
        new TextCompositionEventHandler((s, e) => e.Handled = !e.Text.All(
        c => Char.IsNumber(c) || c == '.'));
    KeyEventHandler textBox_PreviewKeyDown = new KeyEventHandler(
        (s, e) => e.Handled = e.Key == Key.Space);
    
    textBox.PreviewTextInput += textBox_PreviewTextInput;
    textBox.PreviewKeyDown += textBox_PreviewKeyDown;
    

    如果要同时允许复制和粘贴,则不能使用键盘事件。
    TextBox
    具有
    TextChanged
    事件,该事件允许您适当地处理此事件。如果要阻止任何非数字或点的输入,可以如下处理:

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        //get the textbox that fired the event
        var textBox = sender as TextBox;
        if (textBox == null) return;
    
        var text = textBox.Text;
        var output = new StringBuilder();
        //use this boolean to determine if the dot already exists
        //in the text so far.
        var dotEncountered = false;
        //loop through all of the text
        for (int i = 0; i < text.Length; i++)
        {
            var c = text[i];
            if (char.IsDigit(c))
            {
                //append any digit.
                output.Append(c);
            }
            else if (!dotEncountered && c == '.') 
            {
                //append the first dot encountered
                output.Append(c);
                dotEncountered = true;
            }
        }
        var newText = output.ToString();
        textBox.Text = newText;
        //set the caret to the end of text
        textBox.CaretIndex = newText.Length;
    }
    
    private void TextBox\u TextChanged(对象发送者,textchangedventargs e)
    {
    //获取触发事件的文本框
    var textBox=发送方作为textBox;
    if(textBox==null)返回;
    var text=textBox.text;
    var输出=新的StringBuilder();
    //使用此布尔值确定点是否已存在
    //在目前为止的文本中。
    var=false;
    //循环浏览所有文本
    for(int i=0;i
    如果您想同时允许复制和粘贴,则不能对键盘事件进行复制和粘贴。
    文本框
    有一个
    TextChanged
    事件,允许您适当地处理此事件。如果您想阻止任何不是数字或点的输入,可以如下处理:

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        //get the textbox that fired the event
        var textBox = sender as TextBox;
        if (textBox == null) return;
    
        var text = textBox.Text;
        var output = new StringBuilder();
        //use this boolean to determine if the dot already exists
        //in the text so far.
        var dotEncountered = false;
        //loop through all of the text
        for (int i = 0; i < text.Length; i++)
        {
            var c = text[i];
            if (char.IsDigit(c))
            {
                //append any digit.
                output.Append(c);
            }
            else if (!dotEncountered && c == '.') 
            {
                //append the first dot encountered
                output.Append(c);
                dotEncountered = true;
            }
        }
        var newText = output.ToString();
        textBox.Text = newText;
        //set the caret to the end of text
        textBox.CaretIndex = newText.Length;
    }
    
    private void TextBox\u TextChanged(对象发送者,textchangedventargs e)
    {
    //获取触发事件的文本框
    var textBox=发送方作为textBox;
    if(textBox==null)返回;
    var text=textBox.text;
    var输出=新的StringBuilder();
    //使用此布尔值确定点是否已存在
    //在目前为止的文本中。
    var=false;
    //循环浏览所有文本
    for(int i=0;i
    好的,那么你有一个要求。请向我们展示你的努力。并解释你努力失败的原因。这里有一个类似的问题,答案是@phoenix reborn,请参阅我的编辑“并向我们解释你努力失败的原因”....@Mussammil您在循环之外进行验证。这将只检查最后一个字符。如果正确,则如果任何字符都正常,而不是全部正常,则返回true。好吧,那么您有一个要求。请向我们展示您的努力。并解释您的努力失败的原因。下面是一个类似的问题,答案为@Phoenix Reborn,请查看我的编辑“并向我们解释您的努力失败的原因”....@Mussammil您的验证在循环之外。这将只检查最后一个字符。如果它是正确的,则如果任何字符都可以,而不是所有字符,它将返回true。尝试将“Hello World!”粘贴到文本框中。+1感谢您指出@BasBrekelmans…我已经使用了很多年,从未注意到这一点。我会修复它我建议使用
    TextChanged
    事件,但不想复制您的答案,我只保留-1。尝试将“Hello World!”粘贴到文本框中。+1感谢您指出@BasBrekelmans…我已经使用此事件多年了,但从未注意到。如果使用
    TextChanged
    事件,我会修复此问题,但不想复制y我们的答案是,我只保留-1,谢谢你的回复,这不会解决我的要求2(点从不作为第一个字符出现,例如:.5无效,而0.5有效)如何实现这一点,谢谢你的回复,这不会解决我的要求2(点从不作为第一个字符出现,例如:.5无效,而0.5有效)如何做到这一点呢