允许文本框仅支持数字和OemMinus?

时间:2013-09-14 11:25:09

标签: c#

在我的代码中,我使用这段代码来验证文本框仅支持,但我想允许OemMinus( - )我该怎么做呢?

private void card_No_KeyDown(object sender, KeyEventArgs e)
{
    nonNumberEntered = 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 && 
                                        e.KeyCode == Keys.Oemplus)
        {
            // Determine whether the keystroke is a backspace.
            if (e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
}

private void card_No_KeyPress(object sender, KeyPressEventArgs e)
{
    if (nonNumberEntered == true)
    {
        MessageBox.Show("not allowed");
        e.Handled = true;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以像这样处理KeyPress事件

private void card_No_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsNumber(e.KeyChar) && e.KeyChar != '-' && e.KeyChar != (char)Keys.Back)
       e.Handled = true;
}

但这不会阻止用户复制粘贴无效值,因此您也可以处理TextChanged事件。

private void card_No_TextChanged(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(card_No.Text))
    {
        if (Regex.Matches(card_No.Text, @"(-|\d)").Count != card_No.Text.Length)
        {
            //pasted an invalid value
            MessageBox.Show("Invalid value entered");
        }
    }
}

答案 1 :(得分:0)

private void card_No_KeyPress(object sender, KeyPressEventArgs e)
        {
            var textBox = sender as TextBox;
            //handels integers, decimal numbers and OemMinus (-) character
            if (((!char.IsControl(e.KeyChar))
                 && (!char.IsDigit(e.KeyChar))
                 && (e.KeyChar != '-')
                ) || (textBox != null
                      && (e.KeyChar != '.'
                          && (textBox.Text.IndexOf('.') > -1))))
                e.Handled = true;


            if (e.Handled)
                MessageBox.Show(@"not allowed");
        }
//prevent copy and past and delete pasted text
        private void card_No_TextChanged(object sender, EventArgs e)
    {
        if (card_No.Text.Length >0)
        {
            if (Regex.Matches(card_No.Text, @"(-|\d)").Count != card_No.Text.Length)
            {
                if (!Clipboard.ContainsText()) return;
                var txt = Clipboard.GetText();
                if (card_No.Text.Contains(txt))
                {
                    int ind = card_No.Text.IndexOf(txt, System.StringComparison.Ordinal);
                    var text = card_No.Text.Substring(0, ind);
                    card_No.Text = text;
                    MessageBox.Show(@"not allowed");

                }

                else if (txt.Contains(card_No.Text))
                {
                    int ind = txt.IndexOf(card_No.Text, System.StringComparison.Ordinal);
                    var text = txt.Substring(0, ind);
                    card_No.Text = text;
                    MessageBox.Show(@"not allowed");
                }
            }
        }

    }