Exemplo n.º 1
0
        /// <summary>
        /// Formata um CEP com a máscara 00.000-00
        /// </summary>
        /// <param name="cnpj">string CEP</param>
        /// <returns></returns>
        public static string ToCEPFormat(this string cep)
        {
            if (!cep.IsEmpty())
            {
                MaskedTextProvider mtpCEP = new MaskedTextProvider(@"00\.000-000");
                mtpCEP.Set(cep.PadLeft(8, '0'));
                return(mtpCEP.ToString());
            }

            return(null);
        }
Exemplo n.º 2
0
        private void UpdateText()
        {
            if (_provider.ToDisplayString().Equals(Text))
            {
                return;
            }

            bool success = _provider.Set(Text);

            SetText(success ? _provider.ToDisplayString() : Text, _provider.ToString(false, false));
        }
Exemplo n.º 3
0
        /// <summary>
        /// The mask has changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
        private static void MaskChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var maskedTextBox = (MaskedTextBox)sender;

            var maskedTextProvider = new MaskedTextProvider((string)e.NewValue);

            maskedTextProvider.Set(maskedTextBox.Text);

            maskedTextBox._maskedTextProvider = maskedTextProvider;
            maskedTextBox.Refresh(0);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Formata um CPF com a máscara 000.000.000-00
        /// </summary>
        /// <param name="cpf">string Cpf</param>
        /// <returns></returns>
        public static string ToCPFFormat(this string cpf)
        {
            if (!cpf.IsEmpty())
            {
                cpf = cpf.Trim();

                MaskedTextProvider mtpCpf = new MaskedTextProvider(@"000\.000\.000-00");
                mtpCpf.Set(cpf.PadLeft(11, '0'));
                return(mtpCpf.ToString());
            }
            return(null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// formata o CNPJ
        /// </summary>
        /// <param name="cnpj">valor a ser formatado</param>
        /// <returns></returns>
        public static string FormatCNPJ(string cnpj)
        {
            string             ret = "";
            MaskedTextProvider mascara;

            cnpj = Functions.OnlyNumbers(cnpj, "-.,/").ToString();
            //cnpj
            //##.###.###/####-##
            mascara = new MaskedTextProvider(@"00\.000\.000/0000-00");
            mascara.Set(cnpj);
            ret = mascara.ToString();
            return(ret);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Formata um CNPJ com a máscara 00.000.000/0000-00
        /// </summary>
        /// <param name="cnpj">string Cnpj</param>
        /// <returns></returns>
        public static string ToCNPJFormat(this string cnpj)
        {
            if (!cnpj.IsEmpty())
            {
                cnpj = cnpj.Trim();

                MaskedTextProvider mtpCnpj = new MaskedTextProvider(@"00\.000\.000/0000-00");
                mtpCnpj.Set(cnpj.PadLeft(14, '0'));
                return(mtpCnpj.ToString());
            }

            return(null);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Formata uma inscricao estadual com a máscara 000.000.000.000
        /// </summary>
        /// <param name="insc">string inscricaoEstadual</param>
        /// <returns></returns>
        public static string ToInscricaoEstadualFormat(this string insc)
        {
            if (!insc.IsEmpty())
            {
                insc = insc.Trim();

                MaskedTextProvider mtpInsc = new MaskedTextProvider(@"000\.000\.000\.000");
                mtpInsc.Set(insc.PadLeft(12, '0'));
                return(mtpInsc.ToString());
            }

            return(null);
        }
        /// <summary>
        /// Check if the received string value matches with the Introduction Mask for String datatype.
        /// </summary>
        /// <param name="stringValue">String to be checked.</param>
        /// <returns>Returns true if the received string matches with Introduction Mask.</returns>
        private bool IsValidStringAccordingMask(string stringValue)
        {
            bool result = true;

            if (Mask != string.Empty)
            {
                MaskedTextProvider maskTextProvider = new MaskedTextProvider(Mask);
                maskTextProvider.IncludeLiterals = false;
                maskTextProvider.Set(stringValue);
                result = maskTextProvider.VerifyString(stringValue) && maskTextProvider.MaskCompleted;
            }
            return(result);
        }
Exemplo n.º 9
0
        //force the text of the control to use the mask
        private static object ForceText(DependencyObject sender, object value)
        {
            MaskedTextBox textBox = (MaskedTextBox)sender;

            if (textBox.Mask != null)
            {
                MaskedTextProvider provider = new MaskedTextProvider(textBox.Mask);
                provider.Set((string)value);
                return(provider.ToDisplayString());
            }
            else
            {
                return(value);
            }
        }
Exemplo n.º 10
0
 public static string FormatarTelefone(string strTelefone)
 {
     if (strTelefone.Length >= 11)
     {
         MaskedTextProvider mtpTelefone = new MaskedTextProvider(@"(00) 00000-0000");
         mtpTelefone.Set(ZerosEsquerda(strTelefone, 11));
         return(mtpTelefone.ToString());
     }
     else
     {
         MaskedTextProvider mtpTelefone = new MaskedTextProvider(@"(00) 0000-0000");
         mtpTelefone.Set(ZerosEsquerda(strTelefone, 10));
         return(mtpTelefone.ToString());
     }
 }
Exemplo n.º 11
0
 public static string FormatarCpfCnpj(string strCpfCnpj)
 {
     if (strCpfCnpj.Length <= 11)
     {
         MaskedTextProvider mtpCpf = new MaskedTextProvider(@"000\.000\.000-00");
         mtpCpf.Set(ZerosEsquerda(strCpfCnpj, 11));
         return(mtpCpf.ToString());
     }
     else
     {
         MaskedTextProvider mtpCnpj = new MaskedTextProvider(@"00\.000\.000/0000-00");
         mtpCnpj.Set(ZerosEsquerda(strCpfCnpj, 11));
         return(mtpCnpj.ToString());
     }
 }
Exemplo n.º 12
0
        public bool VerificaExisteEmpresaNoRm(string cnpj)
        {
            MaskedTextProvider cnpjComMascara = new MaskedTextProvider(@"00\.000\.000/0000-00");

            cnpjComMascara.Set(cnpj);

            var connectionStringIntegracaoRM = _connectionStrings.Value.RMConnection;

            using (IDbConnection dbConnection = new SqlConnection(connectionStringIntegracaoRM))
            {
                string sQuery = "SELECT * FROM [dbo].[FCFO] WHERE cgccfo = '" + cnpjComMascara + "'";
                bool?  existe = dbConnection.Query <bool?>(sQuery).Any();
                dbConnection.Close();
                return(existe.HasValue && existe.Value);
            }
        }
        /*
         *  Mask Character Accepts Required?
         *  0  Digit (0-9)  Required
         *  9  Digit (0-9) or space  Optional
         #  Digit (0-9) or space  Required
         #  L  Letter (a-z, A-Z)  Required
         #  ?  Letter (a-z, A-Z)  Optional
         #  &  Any character  Required
         #  C  Any character  Optional
         #  A  Alphanumeric (0-9, a-z, A-Z)  Required
         #  a  Alphanumeric (0-9, a-z, A-Z)  Optional
         #     Space separator  Required
         #  .  Decimal separator  Required
         #  ,  Group (thousands) separator  Required
         #  :  Time separator  Required
         #  /  Date separator  Required
         #  $  Currency symbol  Required
         #
         #  In addition, the following characters have special meaning:
         #
         #  Mask Character  Meaning
         #  <  All subsequent characters are converted to lower case
         #  >  All subsequent characters are converted to upper case
         |  Terminates a previous < or >
         \  Escape: treat the next character in the mask as literal text rather than a mask symbol
         */

        private void AssociatedObjectLoaded(object sender, RoutedEventArgs e)
        {
            Provider = new MaskedTextProvider(InputMask, CultureInfo.CurrentCulture);
            Provider.Set(AssociatedObject.Text);
            Provider.PromptChar   = PromptChar;
            AssociatedObject.Text = Provider.ToDisplayString();

            //seems the only way that the text is formatted correct, when source is updated
            DependencyPropertyDescriptor textProp = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty,
                                                                                              typeof(TextBox));

            if (textProp != null)
            {
                textProp.AddValueChanged(AssociatedObject, (s, args) => UpdateText());
            }
        }
Exemplo n.º 14
0
 //force the text of the control to use the mask
 private static object ForceText(DependencyObject sender, object value)
 {
     if (!isValueChanging)
     {
         DateTimeEditor textBox = (DateTimeEditor)sender;
         if (textBox.Mask != null)
         {
             MaskedTextProvider provider = textBox.MaskProvider;
             provider.Set((string)value);
             isValueChanging = true; //prevent endless loop
             if (value == null || string.IsNullOrEmpty((string)value))
             {
                 if (textBox.IsNullable)
                 {
                     textBox.Value = DateTime.Parse("1/1/0001 00:00:00");
                 }
                 else
                 {
                     throw new Exception("Only IsNullable is set to true, Text can be empty.");
                 }
             }
             else
             {
                 DateTime result = new DateTime();
                 if (!DateTime.TryParse((string)value, textBox.CultureInfo, DateTimeStyles.None, out result))
                 {
                     textBox.lastValidValue = textBox.Value;
                 }
                 else
                 {
                     textBox.Value = result;
                 }
             }
             isValueChanging = false;
             if (value == null || string.IsNullOrEmpty((string)value))
             {
                 return("");
             }
             else
             {
                 return(provider.ToDisplayString());
             }
         }
     }
     return(value);
 }
Exemplo n.º 15
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var mask     = parameter as string;
            var inString = value as string;

            if (mask != null & inString != null)
            {
                MaskedTextProvider mtp = new MaskedTextProvider(mask);

                if (mtp.Set(inString))
                {
                    return(mtp.ToDisplayString());
                }
            }

            return(DependencyProperty.UnsetValue);
        }
        private static object CoerceText(DependencyObject d, object value)
        {
            MaskedTextBox textBox = (MaskedTextBox)d;

            if (String.IsNullOrEmpty(textBox.Mask))
            {
                return(String.Empty);
            }
            MaskedTextProvider maskProvider = new MaskedTextProvider(textBox.Mask);

            if (null != value)
            {
                maskProvider.Set((string)value);
            }

            return(maskProvider.ToDisplayString());
        }
Exemplo n.º 17
0
        protected virtual object ForceText(object value)
        {
            if (Mask != null)
            {
                var provider = new MaskedTextProvider(Mask)
                {
                    PromptChar = PromptChar
                };
                if (value != null)
                {
                    provider.Set((string)value);
                }
                return(provider.ToDisplayString());
            }

            return(value);
        }
Exemplo n.º 18
0
        public static string AplicarMascaraCNPJ(object Texto)
        {
            if (Texto != null &&
                Texto.ToString().Trim() != String.Empty)
            {
                MaskedTextProvider oMascara = null;

                oMascara = new MaskedTextProvider(@"99\.999\.999\/9999\-99");

                oMascara.Set(Texto.ToString());

                return(oMascara.ToString());
            }
            else
            {
                return(String.Empty);
            }
        }
Exemplo n.º 19
0
        private void RefreshProvider()
        {
            _provider = new MaskedTextProvider(!string.IsNullOrEmpty(InputMask) ? InputMask : " ", CultureInfo.CurrentCulture);

            _provider.Set(String.IsNullOrWhiteSpace(UnmaskedText) ? String.Empty : UnmaskedText);

            _provider.PromptChar = ' ';
            Text = _provider.ToDisplayString();

            DependencyPropertyDescriptor textProp = DependencyPropertyDescriptor.FromProperty(TextProperty,
                                                                                              typeof(MaskedTextBox));

            if (textProp != null)
            {
                textProp.AddValueChanged(this, (s, args) => UpdateText());
            }
            DataObject.AddPastingHandler(this, Pasting);
        }
Exemplo n.º 20
0
        public static string Formatar(string valor, string mascara)
        {
            MaskedTextProvider mtpCnpj = new MaskedTextProvider(mascara);

            mtpCnpj.Set(valor);
            var formatted = mtpCnpj.ToString();

            if (formatted.IndexOf(" ") == -1)
            {
                return(formatted);
            }

            mascara = mascara.Replace("0", "#").Replace(@"\", "");

            StringBuilder dado = new StringBuilder();

            foreach (char c in valor)
            {
                if (Char.IsNumber(c) || c == 'x' || c == 'X')
                {
                    dado.Append(c);
                }
            }

            int indMascara = mascara.Length;
            int indCampo   = dado.Length;

            for (; indCampo > 0 && indMascara > 0;)
            {
                if (mascara[--indMascara] == '#')
                {
                    indCampo -= 1;
                }
            }

            StringBuilder saida = new StringBuilder();

            for (; indMascara < mascara.Length; indMascara++)
            {
                saida.Append((mascara[indMascara] == '#') ? dado[indCampo++] : mascara[indMascara]);
            }

            return(saida.ToString());
        }
        /// <summary>
        /// Checks if the editor has value or not.
        /// </summary>
        /// <returns>Return true if the editor has a value.</returns>
        private bool HasValue()
        {
            bool lResult = true;

            if (Mask != null && !Mask.Equals(string.Empty))
            {
                // Only String and Time data types can have masks in this Presentation. Check them separately.
                if (mDataType == ModelType.Time)
                {
                    // Get the Time value without Time separators in order to check if the editor control has value.
                    string lAuxStringValue = mMaskedTextBoxIT.Text.Replace(mMaskedTextBoxIT.Culture.DateTimeFormat.TimeSeparator, "");
                    lResult = !string.IsNullOrEmpty(lAuxStringValue.Trim());
                }
                else
                {
                    // String data type.
                    MaskedTextProvider maskTextProvider = new MaskedTextProvider(Mask);
                    maskTextProvider.IncludeLiterals = false;
                    if (!maskTextProvider.VerifyString(mMaskedTextBoxIT.Text))
                    {
                        // If the value assigned to the control does not satisfies the mask,
                        // check if the editor control Text has value.
                        lResult = (mMaskedTextBoxIT.Text != string.Empty);
                    }
                    else
                    {
                        // If the value assigned satisfies the mask
                        // (it is not mandatory that the mask has been completed),
                        // assign the value to the MaskTextProvider and then check if
                        // it has value.
                        maskTextProvider.Set(mMaskedTextBoxIT.Text);
                        string auxString = maskTextProvider.ToString(false, false);
                        lResult = !string.IsNullOrEmpty(auxString);
                    }
                }
            }
            else
            {
                lResult = !mMaskedTextBoxIT.Text.Equals(string.Empty);
            }
            return(lResult);
        }
        private void Paste(object sender, RoutedEventArgs e)
        {
            if (IsReadOnly)
            {
                return;
            }

            MaskedTextProvider provider = MaskProvider;
            int position = SelectionStart;

            object data = Clipboard.GetData(DataFormats.Text);

            if (data != null)
            {
                string text = data.ToString().Trim();
                if (text.Length > 0)
                {
                    provider.Set(text);
                    UpdateText(provider, position);
                }
            }
        }
Exemplo n.º 23
0
        private void InputText(string text)
        {
            string input = text;

            int testPosition;
            MaskedTextResultHint resultHint;
            bool result;

            if (RejectInputOnFirstFailure)
            {
                result = provider.Set(input, out testPosition, out resultHint);
                if (!result)
                {
                    OnMaskInputRejected(new MaskInputRejectedEventArgs(testPosition, resultHint));
                }
            }
            else
            {
                provider.Clear();
                testPosition = 0;

                // Unfortunately we can't break if we reach the end of the mask, since
                // .net iterates over _all_ the chars in the input
                for (int i = 0; i < input.Length; i++)
                {
                    char c = input [i];

                    result = provider.InsertAt(c, testPosition, out testPosition, out resultHint);
                    if (result)
                    {
                        testPosition++;                         // Move to the next free position
                    }
                    else
                    {
                        OnMaskInputRejected(new MaskInputRejectedEventArgs(testPosition, resultHint));
                    }
                }
            }
        }
Exemplo n.º 24
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!targetType.IsAssignableFrom(typeof(string)))
            {
                return(DependencyProperty.UnsetValue);
            }

            string workingText = (value == null) ? string.Empty : value.ToString();

            string mask = null; // Defaults to no mask when no parameter is specified.

            if (parameter != null)
            {
                Type parameterType = parameter.GetType();
                if (parameterType == typeof(string))
                {
                    string stringParameter = ( string )parameter;

                    if (!string.IsNullOrEmpty(stringParameter))
                    {
                        mask = stringParameter;
                    }
                }
                else
                {
                    return(DependencyProperty.UnsetValue);
                }
            }

            if (!string.IsNullOrEmpty(mask))
            {
                try
                {
                    string rawText = string.Empty;

                    CultureInfo currentCulture = CultureInfo.CurrentCulture;

                    if (value != null)
                    {
                        try
                        {
                            Type valueDataType = value.GetType();

                            MethodInfo valueToStringMethodInfo =
                                valueDataType.GetMethod("ToString", new Type[] { typeof(string), typeof(IFormatProvider) });

                            string formatSpecifier = MaskedTextBox.GetFormatSpecifierFromMask(mask, currentCulture);

                            if (valueToStringMethodInfo != null)
                            {
                                rawText = ( string )valueToStringMethodInfo.Invoke(value, new object[] { formatSpecifier, currentCulture });
                            }
                            else
                            {
                                rawText = value.ToString();
                            }
                        }
                        catch
                        {
                            rawText = value.ToString();
                        }
                    }

                    MaskedTextProvider maskedTextProvider = new MaskedTextProvider(mask, currentCulture);

                    maskedTextProvider.Set(rawText);

                    return(maskedTextProvider.ToString(false, true));
                }
                catch
                {
                }
            }

            return(value.ToString());
        }