Как сделать текстовое поле, которое принимает только цифры? - PullRequest
535 голосов
/ 21 января 2009

У меня есть приложение Windows Forms с элементом управления textbox, которое я хочу принимать только целочисленные значения. В прошлом я делал такую ​​проверку, перегружая событие KeyPress и просто удаляя символы, которые не соответствовали спецификации. Я посмотрел на элемент управления MaskedTextBox, но мне хотелось бы более общего решения, которое могло бы работать с, возможно, регулярным выражением или зависеть от значений других элементов управления.

В идеале это будет вести себя так, что нажатие нецифрового символа либо не даст результата, либо сразу предоставит пользователю отзыв о недопустимом символе.

Ответы [ 34 ]

733 голосов
/ 21 января 2009

Два варианта:

  1. Используйте взамен NumericUpDown. NumericUpDown делает фильтрацию за вас, что приятно. Конечно, это также дает вашим пользователям возможность нажимать стрелки вверх и вниз на клавиатуре для увеличения и уменьшения текущего значения.

  2. Обрабатывать соответствующие события клавиатуры, чтобы запретить что-либо, кроме числового ввода. Я имел успех с этими двумя обработчиками событий в стандартном TextBox:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
            (e.KeyChar != '.'))
        {
                e.Handled = true;
        }
    
        // only allow one decimal point
        if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
        {
            e.Handled = true;
        }
    }
    

Вы можете удалить проверку для '.' (и последующую проверку для более чем одного '.'), если ваш TextBox не должен разрешать десятичные разряды. Вы также можете добавить проверку для '-', если ваш TextBox должен разрешать отрицательные значения.

Если вы хотите ограничить пользователя количеством цифр, используйте: textBox1.MaxLength = 2; // this will allow the user to enter only 2 digits

128 голосов
/ 21 января 2009

И только потому, что в одной строке всегда веселее ...

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
    }

ПРИМЕЧАНИЕ. Это НЕ мешает пользователю копировать / вставлять в это текстовое поле. Это не безопасный способ очистки ваших данных.

48 голосов
/ 21 января 2009

Исходя из контекста и используемых вами тегов, вы пишете приложение .NET C #. В этом случае вы можете подписаться на событие изменения текста и проверить каждое нажатие клавиши.

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
        {
            MessageBox.Show("Please enter only numbers.");
            textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
        }
    }
33 голосов
/ 11 сентября 2012

Вот простой автономный пользовательский элемент управления Winforms, полученный из стандартного TextBox, который допускает только ввод System.Int32 (его можно легко адаптировать для других типов, таких как System.Int64 и т. Д.). Он поддерживает операции копирования / вставки и отрицательные числа:

public class Int32TextBox : TextBox
{
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);

        NumberFormatInfo fi = CultureInfo.CurrentCulture.NumberFormat;

        string c = e.KeyChar.ToString();
        if (char.IsDigit(c, 0))
            return;

        if ((SelectionStart == 0) && (c.Equals(fi.NegativeSign)))
            return;

        // copy/paste
        if ((((int)e.KeyChar == 22) || ((int)e.KeyChar == 3))
            && ((ModifierKeys & Keys.Control) == Keys.Control))
            return;

        if (e.KeyChar == '\b')
            return;

        e.Handled = true;
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        const int WM_PASTE = 0x0302;
        if (m.Msg == WM_PASTE)
        {
            string text = Clipboard.GetText();
            if (string.IsNullOrEmpty(text))
                return;

            if ((text.IndexOf('+') >= 0) && (SelectionStart != 0))
                return;

            int i;
            if (!int.TryParse(text, out i)) // change this for other integer types
                return;

            if ((i < 0) && (SelectionStart != 0))
                return;
        }
        base.WndProc(ref m);
    }

Обновление 2017 : у моего первого ответа есть некоторые проблемы:

  • вы можете ввести что-то длиннее целого числа данного типа (например, 2147483648 больше Int32.MaxValue);
  • в более общем смысле, нет реальной проверки результата того, что было напечатано;
  • он обрабатывает только int32, вам придется написать определенный производный элемент управления TextBox для каждого типа (Int64 и т. Д.)

Итак, я придумал другую более общую версию, которая по-прежнему поддерживает копирование / вставку, + и - знак и т. Д.

public class ValidatingTextBox : TextBox
{
    private string _validText;
    private int _selectionStart;
    private int _selectionEnd;
    private bool _dontProcessMessages;

    public event EventHandler<TextValidatingEventArgs> TextValidating;

    protected virtual void OnTextValidating(object sender, TextValidatingEventArgs e) => TextValidating?.Invoke(sender, e);

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (_dontProcessMessages)
            return;

        const int WM_KEYDOWN = 0x100;
        const int WM_ENTERIDLE = 0x121;
        const int VK_DELETE = 0x2e;

        bool delete = m.Msg == WM_KEYDOWN && (int)m.WParam == VK_DELETE;
        if ((m.Msg == WM_KEYDOWN && !delete) || m.Msg == WM_ENTERIDLE)
        {
            DontProcessMessage(() =>
            {
                _validText = Text;
                _selectionStart = SelectionStart;
                _selectionEnd = SelectionLength;
            });
        }

        const int WM_CHAR = 0x102;
        const int WM_PASTE = 0x302;
        if (m.Msg == WM_CHAR || m.Msg == WM_PASTE || delete)
        {
            string newText = null;
            DontProcessMessage(() =>
            {
                newText = Text;
            });

            var e = new TextValidatingEventArgs(newText);
            OnTextValidating(this, e);
            if (e.Cancel)
            {
                DontProcessMessage(() =>
                {
                    Text = _validText;
                    SelectionStart = _selectionStart;
                    SelectionLength = _selectionEnd;
                });
            }
        }
    }

    private void DontProcessMessage(Action action)
    {
        _dontProcessMessages = true;
        try
        {
            action();
        }
        finally
        {
            _dontProcessMessages = false;
        }
    }
}

public class TextValidatingEventArgs : CancelEventArgs
{
    public TextValidatingEventArgs(string newText) => NewText = newText;
    public string NewText { get; }
}

Для Int32 вы можете либо получить его, например:

public class Int32TextBox : ValidatingTextBox
{
    protected override void OnTextValidating(object sender, TextValidatingEventArgs e)
    {
        e.Cancel = !int.TryParse(e.NewText, out int i);
    }
}

или без деривации, используйте новое событие TextValidating, например:

var vtb = new ValidatingTextBox();
...
vtb.TextValidating += (sender, e) => e.Cancel = !int.TryParse(e.NewText, out int i);

но приятно то, что он работает с любой строкой и любой процедурой проверки.

17 голосов
/ 27 июля 2013

Это именно то, для чего были разработаны события Validated / Validating.

Вот статья MSDN по теме: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx

Версия TL; DR: проверьте свойство .Text в событии Validating и установите e.Cancel=True, если данные недействительны.

Когда вы устанавливаете e.Cancel = True, пользователь не может покинуть поле, но вам нужно будет дать ему некоторую обратную связь, что что-то не так. Я изменил цвет фона окна на светло-красный, чтобы указать на проблему. Обязательно установите его обратно на SystemColors.Window, когда Validating вызывается с правильным значением.

12 голосов
/ 21 января 2009

Попробуйте MaskedTextBox . Требуется простой формат маски, чтобы вы могли ограничить ввод цифрами, датами или чем-то еще.

11 голосов
/ 31 марта 2012

Вы можете использовать TextChanged событие

private void textBox_BiggerThan_TextChanged(object sender, EventArgs e)
{
    long a;
    if (! long.TryParse(textBox_BiggerThan.Text, out a))
    {
        // If not int clear textbox text or Undo() last operation
        textBox_LessThan.Clear();
    }
}
6 голосов
/ 09 августа 2012

Это может быть полезно. Он допускает «реальные» числовые значения, включая правильные десятичные точки и предшествующие знаки плюс или минус. Вызовите его из соответствующего события KeyPress.

       private bool IsOKForDecimalTextBox(char theCharacter, TextBox theTextBox)
    {
        // Only allow control characters, digits, plus and minus signs.
        // Only allow ONE plus sign.
        // Only allow ONE minus sign.
        // Only allow the plus or minus sign as the FIRST character.
        // Only allow ONE decimal point.
        // Do NOT allow decimal point or digits BEFORE any plus or minus sign.

        if (
            !char.IsControl(theCharacter)
            && !char.IsDigit(theCharacter)
            && (theCharacter != '.')
            && (theCharacter != '-')
            && (theCharacter != '+')
        )
        {
            // Then it is NOT a character we want allowed in the text box.
            return false;
        }



        // Only allow one decimal point.
        if (theCharacter == '.'
            && theTextBox.Text.IndexOf('.') > -1)
        {
            // Then there is already a decimal point in the text box.
            return false;
        }

        // Only allow one minus sign.
        if (theCharacter == '-'
            && theTextBox.Text.IndexOf('-') > -1)
        {
            // Then there is already a minus sign in the text box.
            return false;
        }

        // Only allow one plus sign.
        if (theCharacter == '+'
            && theTextBox.Text.IndexOf('+') > -1)
        {
            // Then there is already a plus sign in the text box.
            return false;
        }

        // Only allow one plus sign OR minus sign, but not both.
        if (
            (
                (theCharacter == '-')
                || (theCharacter == '+')
            )
            && 
            (
                (theTextBox.Text.IndexOf('-') > -1)
                ||
                (theTextBox.Text.IndexOf('+') > -1)
            )
            )
        {
            // Then the user is trying to enter a plus or minus sign and
            // there is ALREADY a plus or minus sign in the text box.
            return false;
        }

        // Only allow a minus or plus sign at the first character position.
        if (
            (
                (theCharacter == '-')
                || (theCharacter == '+')
            )
            && theTextBox.SelectionStart != 0
            )
        {
            // Then the user is trying to enter a minus or plus sign at some position 
            // OTHER than the first character position in the text box.
            return false;
        }

        // Only allow digits and decimal point AFTER any existing plus or minus sign
        if  (
                (
                    // Is digit or decimal point
                    char.IsDigit(theCharacter)
                    ||
                    (theCharacter == '.')
                )
                &&
                (
                    // A plus or minus sign EXISTS
                    (theTextBox.Text.IndexOf('-') > -1)
                    ||
                    (theTextBox.Text.IndexOf('+') > -1)
                )
                &&
                    // Attempting to put the character at the beginning of the field.
                    theTextBox.SelectionStart == 0
            )
        {
            // Then the user is trying to enter a digit or decimal point in front of a minus or plus sign.
            return false;
        }

        // Otherwise the character is perfectly fine for a decimal value and the character
        // may indeed be placed at the current insertion position.
        return true;
    }
5 голосов
/ 24 февраля 2015

Я работал над набором компонентов, чтобы завершить недостающий материал в WinForms, вот он: Расширенные формы

В частности, это класс для Regex TextBox

/// <summary>Represents a Windows text box control that only allows input that matches a regular expression.</summary>
public class RegexTextBox : TextBox
{
    [NonSerialized]
    string lastText;

    /// <summary>A regular expression governing the input allowed in this text field.</summary>
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public virtual Regex Regex { get; set; }

    /// <summary>A regular expression governing the input allowed in this text field.</summary>
    [DefaultValue(null)]
    [Category("Behavior")]
    [Description("Sets the regular expression governing the input allowed for this control.")]
    public virtual string RegexString {
        get {
            return Regex == null ? string.Empty : Regex.ToString();
        }
        set {
            if (string.IsNullOrEmpty(value))
                Regex = null;
            else
                Regex = new Regex(value);
        }
    }

    protected override void OnTextChanged(EventArgs e) {
        if (Regex != null && !Regex.IsMatch(Text)) {
            int pos = SelectionStart - Text.Length + (lastText ?? string.Empty).Length;
            Text = lastText;
            SelectionStart = Math.Max(0, pos);
        }

        lastText = Text;

        base.OnTextChanged(e);
    }
}

Достаточно просто добавить что-то вроде myNumbericTextBox.RegexString = "^(\\d+|)$";.

4 голосов
/ 23 декабря 2014

просто используйте этот код в текстовом поле:

private void textBox1_TextChanged(object sender, EventArgs e)
{

    double parsedValue;

    if (!double.TryParse(textBox1.Text, out parsedValue))
    {
        textBox1.Text = "";
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...