WinForms - MaskedTextBox - variable width masking possible?

I need to create a WinForms text box that allows you to use decimal text with exceptional or whole text. In addition, I do not want to require specifying the length of the text in the mask; the user should be able to enter as many characters as he wants if the text matches decimal or integer form. However, as far as I know, MaskedTextBox does not allow variable-length masking; I also cannot find an existing control that does this.

Advice? I suppose I can inherit a TextBox, override OnKeyPress and do the work there, but I don't know if the pre-existing control will do something more elegantly.

+3
source share
4 answers

The NumericUpDown control has some built-in decimal / integer parsing behavior - it seems like this might be what you are looking for. Of course, you also get updown controls in the text box.

+1
source

Try the following:

private void TextBox1_Validating(object sender, EventArgs e)
{
    string expression = "^\d{1,8}(\.\d{2,2})?$";
    if (System.Text.RegularExpressions.Regex.Match(this.txt_monto_total.Text, expression).Success)
        this.label_total.Visible = false;
    else
        this.label_total.Visible = true;
}

This is not a mask, but it will allow you to tell the user if they entered the wrong format (I do this by showing the label), you can use any regular expression, in my case I want only numbers from 1 or 8 digits to "." and 2 after him.

+1
source

. - , , !

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WinFormsTest
{
    public partial class MaskedTextBox : TextBox
    {
        public enum EntryTypeEnum
        {
            Any,
            Integer,
            Decimal
        }

        [DefaultValue(EntryTypeEnum.Any)]
        public EntryTypeEnum EntryType { get; set; }

        public MaskedTextBox()
        {
            InitializeComponent();
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            var keyIsValid =
                (EntryType == EntryTypeEnum.Any)
                || char.IsControl(e.KeyChar)
                || isValid(Text + e.KeyChar);
            e.Handled = !keyIsValid;
            base.OnKeyPress(e);
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            e.Cancel = !isValid(Text);
            base.OnValidating(e);
        }

        protected bool isValid(string textToValidate)
        {
            switch (EntryType)
            {
                case EntryTypeEnum.Any:
                    break;

                case EntryTypeEnum.Decimal:
                    {
                        decimal result;
                        if (!decimal.TryParse(textToValidate, out result))
                        {
                            return false;
                        }
                    }
                    break;

                case EntryTypeEnum.Integer:
                    {
                        int result;
                        if (!int.TryParse(textToValidate, out result))
                        {
                            return false;
                        }
                    }
                    break;
            }

            return true;
        }
    }
}
0

codeproject, .

This is a Nullable Masked Edit and a project with advanced masking Edit also! .

Note that you need to use the "EditMask" field to set the mask properties. Attempting to set the Mask field will not work.

I managed to set the mask 999.9, and then enter 1.1 will work without the need to add leading spaces.

0
source

Source: https://habr.com/ru/post/1729594/


All Articles