开发者

WinForms - MaskedTextBox - "variable-width" masking possible?

开发者 https://www.devze.com 2022-12-17 15:45 出处:网络
I need to create a WinForms textbox that allows decimal text exclusive-or integer text. Also, I don\'t wish to be required to specify the length of the text in the mask; the user should be able to ent

I need to create a WinForms textbox that allows decimal text exclusive-or integer text. Also, I don't wish to be required to specify the length of the text in the mask; the user should be able to enter as many characters as he wants, as long as the text fits the decimal or integer mold. However, the MaskedTextBox doesn't allow variable-length masking, as far as I know; I can't find a pre-existing control that does this, either.

Advice? I suppose I could inherit TextBox, override OnKeyPress and do the work there, but I don't know whether a开发者_JS百科 pre-existing control would do things more gracefully.


the NumericUpDown control has some built in decimal/integer parsing behavior - sounds like it might be what you're looking for. Of course, you end up with the updown controls on the text box too.


Try this:

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;
}

It's not a mask but it will let you tell the user if they have entered an incorrect format (i do it by showing the label), you can use any regular expression, in my case i only want numbers with 1 or 8 digits before the "." and 2 after it.


Here's a solution. It's not perfect - it may eat control characters in certain cases, and it doesn't cleanly handle pasting - but it works well enough!

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;
        }
    }
}


After much searching I was able to discover a control on codeproject that allows for a variable length number.

It is the Nullable Masked Edit, and a Better Masked Edit Also! project.

Note you need to use the 'EditMask' field to set the Mask properties. Trying to set the 'Mask' field won't work.

I was able to set a mask of 999.9 and then entering 1.1 would work without having to add leading spaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消