示例#1
0
        public static bool IsValidMaskDescriptor(MaskDescriptor maskDescriptor, out string validationErrorDescription)
        {
            validationErrorDescription = string.Empty;

            if (maskDescriptor is null)
            {
                validationErrorDescription = SR.MaskDescriptorNull;
                return(false);
            }

            if (string.IsNullOrEmpty(maskDescriptor.Mask) || string.IsNullOrEmpty(maskDescriptor.Name) || string.IsNullOrEmpty(maskDescriptor.Sample))
            {
                validationErrorDescription = SR.MaskDescriptorNullOrEmptyRequiredProperty;
                return(false);
            }

            MaskedTextProvider maskedTextProvider = new MaskedTextProvider(maskDescriptor.Mask, maskDescriptor.Culture);
            MaskedTextBox      maskedTextBox      = new MaskedTextBox(maskedTextProvider);

            maskedTextBox.SkipLiterals             = true;
            maskedTextBox.ResetOnPrompt            = true;
            maskedTextBox.ResetOnSpace             = true;
            maskedTextBox.ValidatingType           = maskDescriptor.ValidatingType;
            maskedTextBox.FormatProvider           = maskDescriptor.Culture;
            maskedTextBox.Culture                  = maskDescriptor.Culture;
            maskedTextBox.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
            maskedTextBox.MaskInputRejected       += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);

            // Add sample. If it fails we are done.
            maskedTextBox.Text = maskDescriptor.Sample;

            if (maskedTextBox.Tag is null) // Sample was added successfully (MaskInputRejected event handler did not change the maskedTextBox tag).
            {
                if (maskDescriptor.ValidatingType != null)
                {
                    maskedTextBox.ValidateText();
                }
            }

            if (maskedTextBox.Tag != null) // Validation failed.
            {
                validationErrorDescription = maskedTextBox.Tag.ToString();
            }

            return(validationErrorDescription.Length == 0);
        }
        public static bool ValidateText(MaskedTextBox masked)
        {
            object text = masked.ValidateText();

            if (text != null)
            {
                Debug.Write("Text Saccess");
                Debug.WriteLine(" " + Convert.ToDateTime(text).ToShortDateString());
                return(true);
            }
            else
            {
                Debug.Write("Fail");
                Debug.WriteLine(" " + Convert.ToDateTime(text).ToShortDateString());

                return(false);
            }
        }
示例#3
0
        public static bool IsValidMaskDescriptor(MaskDescriptor maskDescriptor, out string validationErrorDescription)
        {
            validationErrorDescription = string.Empty;
            if (maskDescriptor == null)
            {
                validationErrorDescription = System.Design.SR.GetString("MaskDescriptorNull");
                return(false);
            }
            if ((string.IsNullOrEmpty(maskDescriptor.Mask) || string.IsNullOrEmpty(maskDescriptor.Name)) || string.IsNullOrEmpty(maskDescriptor.Sample))
            {
                validationErrorDescription = System.Design.SR.GetString("MaskDescriptorNullOrEmptyRequiredProperty");
                return(false);
            }
            MaskedTextProvider maskedTextProvider = new MaskedTextProvider(maskDescriptor.Mask, maskDescriptor.Culture);
            MaskedTextBox      box = new MaskedTextBox(maskedTextProvider)
            {
                SkipLiterals   = true,
                ResetOnPrompt  = true,
                ResetOnSpace   = true,
                ValidatingType = maskDescriptor.ValidatingType,
                FormatProvider = maskDescriptor.Culture,
                Culture        = maskDescriptor.Culture
            };

            box.TypeValidationCompleted += new TypeValidationEventHandler(MaskDescriptor.maskedTextBox1_TypeValidationCompleted);
            box.MaskInputRejected       += new MaskInputRejectedEventHandler(MaskDescriptor.maskedTextBox1_MaskInputRejected);
            box.Text = maskDescriptor.Sample;
            if ((box.Tag == null) && (maskDescriptor.ValidatingType != null))
            {
                box.ValidateText();
            }
            if (box.Tag != null)
            {
                validationErrorDescription = box.Tag.ToString();
            }
            return(validationErrorDescription.Length == 0);
        }