简体   繁体   English

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

[英]allow textbox to support numbers and OemMinus only?

In my code I use this piece of code to validate textbox to support only, but I want to allow OemMinus (-) also how can I do this? 在我的代码中,我使用这段代码来验证仅支持文本框,但是我想允许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;
    }
}

You could handle the KeyPress event like this 您可以像这样处理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;
}

But that won't prevent the user from copy pasting an invalid value so you can also handle the TextChanged event. 但这不会阻止用户复制粘贴无效的值,因此您还可以处理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");
        }
    }
}
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");
                }
            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM