Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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#_Winforms_Maskedtextbox - Fatal编程技术网

C# 屏蔽文本框文本被拒绝

C# 屏蔽文本框文本被拒绝,c#,winforms,maskedtextbox,C#,Winforms,Maskedtextbox,我想弄清楚怎么做,如果我按下按钮做一个动作,比如显示一个messagebox,我的maskedtextbox的文本不是一个数字,然后它就会去做一些事情,比如说你只能在文本框中有一个数字或者类似的事情。我似乎不明白 我试着用这个: if (!System.Text.RegularExpressions.Regex.IsMatch(binTxtbx.Text, @"0-9")) e.Handled = true; 但是如果我使用它,它不会将任何文本放入maskedtextb

我想弄清楚怎么做,如果我按下按钮做一个动作,比如显示一个messagebox,我的maskedtextbox的文本不是一个数字,然后它就会去做一些事情,比如说你只能在文本框中有一个数字或者类似的事情。我似乎不明白

我试着用这个:

if (!System.Text.RegularExpressions.Regex.IsMatch(binTxtbx.Text, @"0-9"))
            e.Handled = true;
但是如果我使用它,它不会将任何文本放入maskedtextbox

如果你知道有人问我同样的问题,请告诉我。

表达式应该是带方括号的[0-9]

完整代码:

!System.Text.RegularExpressions.Regex.IsMatch(binTxtbx.Text, "^[0-9]*$")
表达式应为带方括号的[0-9]

完整代码:

!System.Text.RegularExpressions.Regex.IsMatch(binTxtbx.Text, "^[0-9]*$")
也许你可以用

if (binTxtbx.Text.Any(c => char.IsNumber(c)))
{
   // found a number in the string
}

也许你可以用

if (binTxtbx.Text.Any(c => char.IsNumber(c)))
{
   // found a number in the string
}


如果您不介意使用maskedTextBox,并且不喜欢注释中提到的下划线,只需将PromptChar更改为空白即可

您可以在MaskedTextBox属性的设计视图中执行此操作,也可以在如下代码中执行此操作:

myMaskedTextBox.PromptChar = ' ';
    private void numericComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            e.SuppressKeyPress = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace or arrow key
                    if ((e.KeyCode != Keys.Back) && (e.KeyCode != Keys.Up) && (e.KeyCode != Keys.Right) && (e.KeyCode != Keys.Down) && (e.KeyCode != Keys.Left))
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Handle any exception here...
        }
    }
编辑:

或者,如果不想使用maskedTextBox,可以将KeyDown事件连接到EventHandler,如下所示:

myMaskedTextBox.PromptChar = ' ';
    private void numericComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            e.SuppressKeyPress = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace or arrow key
                    if ((e.KeyCode != Keys.Back) && (e.KeyCode != Keys.Up) && (e.KeyCode != Keys.Right) && (e.KeyCode != Keys.Down) && (e.KeyCode != Keys.Left))
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Handle any exception here...
        }
    }

如果您不介意使用maskedTextBox,并且不喜欢注释中提到的下划线,只需将PromptChar更改为空白即可

您可以在MaskedTextBox属性的设计视图中执行此操作,也可以在如下代码中执行此操作:

myMaskedTextBox.PromptChar = ' ';
    private void numericComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            e.SuppressKeyPress = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace or arrow key
                    if ((e.KeyCode != Keys.Back) && (e.KeyCode != Keys.Up) && (e.KeyCode != Keys.Right) && (e.KeyCode != Keys.Down) && (e.KeyCode != Keys.Left))
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Handle any exception here...
        }
    }
编辑:

或者,如果不想使用maskedTextBox,可以将KeyDown事件连接到EventHandler,如下所示:

myMaskedTextBox.PromptChar = ' ';
    private void numericComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            e.SuppressKeyPress = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace or arrow key
                    if ((e.KeyCode != Keys.Back) && (e.KeyCode != Keys.Up) && (e.KeyCode != Keys.Right) && (e.KeyCode != Keys.Down) && (e.KeyCode != Keys.Left))
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Handle any exception here...
        }
    }

是否应该是:[0-9]、@[0-9]或@[0-9]?是否应该是:[0-9]、@[0-9]或@[0-9]?如果您使用的是MaskedTextBox,则应该使用Mask属性来防止无效条目MaskedTextBox。Mask=000我只是不喜欢在MaskedTextBox中使用下划线。这很好,但防止用户输入无效数据比允许用户输入无效数据更好,之后再告诉用户不能输入无效数据。您可以将默认提示更改为空格字符。我个人不喜欢这样,因为它是非标准的,或者您可以使用TextBox并处理KeyDown事件来防止非数字输入-如果您使用MaskedTextBox,则应该使用Mask属性来防止无效输入MaskedTextBox.Mask=000I只是不希望在MaskedTextBox中有下划线。这很好,但防止用户输入无效数据比允许用户输入无效数据更好,之后再告诉用户不能输入无效数据。您可以将默认提示更改为空格字符。我个人不喜欢这样,因为它是非标准的,或者你可以使用文本框和处理按键事件来防止非数字输入-谢谢,但我仍然不知道如何使它成为一个数字,如果它不是一个数字,它会删除其他人输入的任何东西,比如一封信,或者显示一个messagebox或其他东西。你知道怎么做吗?谢谢,但我还是不知道怎么做,如果不是一个数字,它会删除其他人放入的任何东西,比如一封信,或者显示一个消息框或其他东西。你知道怎么做吗?我刚刚知道如何更改属性中的提示字符,以及你使用的第一个代码!非常感谢。我刚刚知道了如何更改属性中的提示字符,以及您使用的第一个代码!谢谢。