2

在我的代码中,我使用这段代码来验证文本框是否仅支持,但我也想允许 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;
    }
}
4

2 回答 2

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");
        }
    }
}
于 2013-09-14T11:53:08.610 回答
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");
                }
            }
        }

    }
于 2013-09-14T12:05:14.823 回答