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(); }
public void PasswordTest () { MaskedTextProvider mtp = new MaskedTextProvider ("abcd", CultureInfo.GetCultureInfo ("es-AR"), false, '>', '^', false); Assert.AreEqual (" bcd", mtp.ToString (), "#A1"); }
/// <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; }
private string GetFormattedString( MaskedTextProvider provider ) { System.Diagnostics.Debug.Assert( provider.EditPositionCount > 0 ); bool includePrompt = ( this.IsReadOnly ) ? false : ( !this.HidePromptOnLeave || this.IsFocused ); string displayString = provider.ToString( false, includePrompt, true, 0, m_maskedTextProvider.Length ); return displayString; }
/* START */ public static void AssertProperties (MaskedTextProvider mtp, string test_name, int counter, bool allow_prompt, bool ascii_only, int assigned_edit_position_count, int available_edit_position_count, CultureInfo culture, int edit_position_count, bool include_literals, bool include_prompt, bool is_password, int last_assigned_position, int length, string mask, bool mask_completed, bool mask_full, char password_char, char prompt_char, bool reset_on_prompt, bool reset_on_space, bool skip_literals, string tostring, string tostring_true, string tostring_false, string tostring_true_true, string tostring_true_false, string tostring_false_true, string tostring_false_false) { // Testing all properties... //return; int i = 1; ArrayList asserts = new ArrayList (); try { Assert.AreEqual (allow_prompt, mtp.AllowPromptAsInput, string.Format ("{0}-#{1} (AllowPromptAsInput)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (ascii_only, mtp.AsciiOnly, string.Format ("{0}-#{1} (AsciiOnly)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (assigned_edit_position_count, mtp.AssignedEditPositionCount, string.Format ("{0}-#{1} (AssignedEditPositionCount)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (available_edit_position_count, mtp.AvailableEditPositionCount, string.Format ("{0}-#{1} (AvailableEditPositionCount)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { //Assert.AreEqual (culture, mtp.Culture, string.Format ("{0}-#{1} (Culture)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (edit_position_count, mtp.EditPositionCount, string.Format ("{0}-#{1} (EditPositionCount)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { //Assert.AreEqual ({0}, mtp.EditPositions,string.Format( "{0}-#{1} (EditPositions)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (include_literals, mtp.IncludeLiterals, string.Format ("{0}-#{1} (IncludeLiterals)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (include_prompt, mtp.IncludePrompt, string.Format ("{0}-#{1} (IncludePrompt)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (is_password, mtp.IsPassword, string.Format ("{0}-#{1} (IsPassword)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (last_assigned_position, mtp.LastAssignedPosition, string.Format ("{0}-#{1} (LastAssignedPosition)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (length, mtp.Length, string.Format ("{0}-#{1} (Length)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (mask, mtp.Mask, string.Format ("{0}-#{1} (Mask)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (mask_completed, mtp.MaskCompleted, string.Format ("{0}-#{1} (MaskCompleted)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (mask_full, mtp.MaskFull, string.Format ("{0}-#{1} (MaskFull)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (password_char, mtp.PasswordChar, string.Format ("{0}-#{1} (PasswordChar)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (prompt_char, mtp.PromptChar, string.Format ("{0}-#{1} (PromptChar)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (reset_on_prompt, mtp.ResetOnPrompt, string.Format ("{0}-#{1} (ResetOnPrompt)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (reset_on_space, mtp.ResetOnSpace, string.Format ("{0}-#{1} (ResetOnSpace)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (skip_literals, mtp.SkipLiterals, string.Format ("{0}-#{1} (SkipLiterals)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring, mtp.ToString (), string.Format ("{0}-#{1} (tostring)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring_true, mtp.ToString (true), string.Format ("{0}-#{1} (tostring_true)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring_false, mtp.ToString (false), string.Format ("{0}-#{1} (tostring_false)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring_true_true, mtp.ToString (true, true), string.Format ("{0}-#{1} (tostring_true_true)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring_true_false, mtp.ToString (true, false), string.Format ("{0}-#{1} (tostring_true_false)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring_false_true, mtp.ToString (false, true), string.Format ("{0}-#{1} (tostring_false_true)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } try { Assert.AreEqual (tostring_false_false, mtp.ToString (false, false), string.Format ("{0}-#{1} (tostring_false_false)", test_name + counter.ToString (), (i++).ToString ())); } catch (AssertionException ex) { asserts.Add (ex); } if (asserts.Count > 0) { string msg = ""; foreach (AssertionException ex in asserts) { msg += ex.Message + Environment.NewLine; } throw new AssertionException (msg); } }
public void ToString_bool_bool_int_int_Test00008 () { MaskedTextProvider mtp; //int Int32_out = 0; //MaskedTextResultHint MaskedTextResultHint_out = MaskedTextResultHint.Unknown; mtp = new MaskedTextProvider (@"abc"); mtp.Add (@"a"); mtp.Remove (); mtp.InsertAt ('\x61', 1); Assert.AreEqual (@"_", mtp.ToString (true, true, -1, 1), "#0"); MaskedTextProviderTest.AssertProperties (mtp, "ToString_bool_bool_int_int_Test", 1, true, false, 0, 1, CultureInfo.GetCultureInfo ("es-ES"), 1, true, false, false, -1, 3, @"abc", true, false, '\x0', '\x5F', true, true, true, @" bc", @" bc", @" bc", @"_bc", @"_", @" bc", @""); }
public void ToString_bool_bool_bool_int_int_Test00055 () { MaskedTextProvider mtp; int Int32_out = 0; MaskedTextResultHint MaskedTextResultHint_out = MaskedTextResultHint.Unknown; mtp = new MaskedTextProvider (@"abc"); mtp.Add (@"a"); mtp.Add ('\x61'); mtp.Add ('\x61', out Int32_out, out MaskedTextResultHint_out); Assert.AreEqual (@"c", mtp.ToString (true, true, true, 2, 2), "#6"); MaskedTextProviderTest.AssertProperties (mtp, "ToString_bool_bool_bool_int_int_Test", 7, true, false, 1, 0, CultureInfo.GetCultureInfo ("es-ES"), 1, true, false, false, 0, 3, @"abc", true, true, '\x0', '\x5F', true, true, true, @"abc", @"abc", @"abc", @"abc", @"a", @"abc", @"a"); }
public void ToString_False_FalseTest () { MaskedTextProvider mtp; mtp = new MaskedTextProvider ("a?a"); Assert.AreEqual ("", mtp.ToString (false, false), "#01"); mtp.InsertAt ('a', 1); Assert.AreEqual (" a", mtp.ToString (false, false), "#02"); mtp = new MaskedTextProvider ("a?a"); Assert.AreEqual ("", mtp.ToString (false, false), "#03"); mtp.InsertAt ('a', 0); Assert.AreEqual ("a", mtp.ToString (false, false), "#04"); mtp = new MaskedTextProvider ("a?a?a"); Assert.AreEqual ("", mtp.ToString (false, false), "#05"); mtp.InsertAt ('a', 3); Assert.AreEqual (" a", mtp.ToString (false, false), "#06"); mtp = new MaskedTextProvider ("????a"); Assert.AreEqual ("", mtp.ToString (false, false), "#07"); mtp.InsertAt ('a', 3); Assert.AreEqual (" a", mtp.ToString (false, false), "#08"); mtp = new MaskedTextProvider ("LLLLa"); Assert.AreEqual ("", mtp.ToString (false, false), "#09"); mtp.InsertAt ('a', 3); Assert.AreEqual (" a", mtp.ToString (false, false), "#10"); mtp = new MaskedTextProvider ("CCCCa"); Assert.AreEqual ("", mtp.ToString (false, false), "#11"); mtp.InsertAt ('a', 3); Assert.AreEqual (" a", mtp.ToString (false, false), "#12"); mtp = new MaskedTextProvider ("aaaaa"); Assert.AreEqual ("", mtp.ToString (false, false), "#13"); mtp.InsertAt ('a', 3); Assert.AreEqual (" a", mtp.ToString (false, false), "#14"); mtp = new MaskedTextProvider ("aaaaaaaaaaaaaaaaaaaaaa"); Assert.AreEqual ("", mtp.ToString (false, false), "#15"); mtp.InsertAt ('a', 3); Assert.AreEqual (" a", mtp.ToString (false, false), "#16"); mtp.InsertAt ('a', 9); Assert.AreEqual (" a a", mtp.ToString (false, false), "#17"); mtp = new MaskedTextProvider ("aaa"); mtp.PasswordChar = '*'; mtp.InsertAt ('a', 2); Assert.AreEqual (" a", mtp.ToString (false, false), "#18"); }
public void ToString_PasswordTest () { MaskedTextProvider mtp; mtp = new MaskedTextProvider ("####"); mtp.PasswordChar = '*'; Assert.AreEqual ("____", mtp.ToString (true, true, true, 0, mtp.Length), "#A1"); Assert.AreEqual ("____", mtp.ToString (false, true, true, 0, mtp.Length), "#A2"); mtp.Add ("314"); Assert.AreEqual ("314_", mtp.ToString (true, true, true, 0, mtp.Length), "#B1"); Assert.AreEqual ("***_", mtp.ToString (false, true, true, 0, mtp.Length), "#B2"); mtp.Clear (); mtp.InsertAt ("666", 1); Assert.AreEqual ("_666", mtp.ToString (true, true, true, 0, mtp.Length), "#C1"); Assert.AreEqual ("_***", mtp.ToString (false, true, true, 0, mtp.Length), "#C2"); }
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(); }
public void InsertAt_charTest () { MaskedTextProvider mtp; int Int32_out = 0; MaskedTextResultHint MaskedTextResultHint_out = MaskedTextResultHint.Unknown; // insert space over space mtp = new MaskedTextProvider ("aaa"); mtp.Add (" "); Assert.AreEqual (true, mtp.InsertAt (' ', 0, out Int32_out, out MaskedTextResultHint_out), "#A1"); Assert.AreEqual ("", mtp.ToString (), "A2"); Assert.AreEqual (0, Int32_out, "A3"); Assert.AreEqual (MaskedTextResultHint.CharacterEscaped, MaskedTextResultHint_out, "A4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_A", 1, true, false, 0, 3, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, -1, 3, @"aaa", true, false, '\x0', '\x5F', true, true, true, @"", @"", @"", @"___", @"___", @"", @""); // insert space over normal char. mtp = new MaskedTextProvider ("aaa"); mtp.Add ("a"); Assert.AreEqual (true, mtp.InsertAt (' ', 0, out Int32_out, out MaskedTextResultHint_out), "#B1"); Assert.AreEqual (" a", mtp.ToString (), "B2"); Assert.AreEqual (0, Int32_out, "B3"); Assert.AreEqual (MaskedTextResultHint.Success, MaskedTextResultHint_out, "B4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_B", 1, true, false, 1, 2, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, 1, 3, @"aaa", true, false, '\x0', '\x5F', true, true, true, @" a", @" a", @" a", @"_a_", @"_a_", @" a", @" a"); // insert space over empty position. mtp = new MaskedTextProvider ("aaa"); Assert.AreEqual (true, mtp.InsertAt (' ', 0, out Int32_out, out MaskedTextResultHint_out), "#C1"); Assert.AreEqual ("", mtp.ToString (), "C2"); Assert.AreEqual (0, Int32_out, "C3"); Assert.AreEqual (MaskedTextResultHint.CharacterEscaped, MaskedTextResultHint_out, "C4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_C", 1, true, false, 0, 3, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, -1, 3, @"aaa", true, false, '\x0', '\x5F', true, true, true, @"", @"", @"", @"___", @"___", @"", @""); // insert space over empty position with other characters later in the string already inserted.. mtp = new MaskedTextProvider ("aaa"); mtp.InsertAt ('z', 2); Assert.AreEqual (true, mtp.InsertAt (' ', 0, out Int32_out, out MaskedTextResultHint_out), "#D1"); Assert.AreEqual (" z", mtp.ToString (), "D2"); Assert.AreEqual (0, Int32_out, "D3"); Assert.AreEqual (MaskedTextResultHint.CharacterEscaped, MaskedTextResultHint_out, "D4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_D", 1, true, false, 1, 2, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, 2, 3, @"aaa", true, false, '\x0', '\x5F', true, true, true, @" z", @" z", @" z", @"__z", @"__z", @" z", @" z"); // insert space over non-empty position with other characters later in the string already inserted.. mtp = new MaskedTextProvider ("aaa"); mtp.InsertAt ('z', 0); mtp.InsertAt ('z', 1); Assert.AreEqual (true, mtp.InsertAt (' ', 0, out Int32_out, out MaskedTextResultHint_out), "#E1"); Assert.AreEqual (" zz", mtp.ToString (), "E2"); Assert.AreEqual (0, Int32_out, "E3"); Assert.AreEqual (MaskedTextResultHint.Success, MaskedTextResultHint_out, "E4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_E", 1, true, false, 2, 1, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, 2, 3, @"aaa", true, false, '\x0', '\x5F', true, true, true, @" zz", @" zz", @" zz", @"_zz", @"_zz", @" zz", @" zz"); // Insert number over empty position with other characters later in the string. mtp = new MaskedTextProvider (@"aaa"); mtp.InsertAt ('\x33', 2); Assert.AreEqual (true, mtp.InsertAt ('\x34', 0, out Int32_out, out MaskedTextResultHint_out), "#F1"); Assert.AreEqual ("4 3", mtp.ToString (), "F2"); Assert.AreEqual (0, Int32_out, "F3"); Assert.AreEqual (MaskedTextResultHint.Success, MaskedTextResultHint_out, "F4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_F", 1, true, false, 2, 1, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, 2, 3, @"aaa", true, false, '\x0', '\x5F', true, true, true, @"4 3", @"4 3", @"4 3", @"4_3", @"4_3", @"4 3", @"4 3"); // insert space over literal with filled in positions later on (no more available edit positions) mtp = new MaskedTextProvider ("aba"); mtp.InsertAt ('z', 0); mtp.InsertAt ('z', 1); mtp.InsertAt ('z', 2); Assert.AreEqual (false, mtp.InsertAt (' ', 1, out Int32_out, out MaskedTextResultHint_out), "#F1"); Assert.AreEqual ("zbz", mtp.ToString (), "F2"); Assert.AreEqual (3, Int32_out, "F3"); Assert.AreEqual (MaskedTextResultHint.UnavailableEditPosition, MaskedTextResultHint_out, "F4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_F", 1, true, false, 2, 0, CultureInfo.GetCultureInfo ("es-ES"), 2, true, false, false, 2, 3, @"aba", true, true, '\x0', '\x5F', true, true, true, @"zbz", @"zbz", @"zbz", @"zbz", @"zz", @"zbz", @"zz"); // insert space over literal with filled in positions later on ( more available edit positions) mtp = new MaskedTextProvider ("abaa"); mtp.InsertAt ('z', 0); mtp.InsertAt ('z', 2); Assert.AreEqual (true, mtp.InsertAt (' ', 1, out Int32_out, out MaskedTextResultHint_out), "#G1"); Assert.AreEqual ("zb z", mtp.ToString (), "G2"); Assert.AreEqual (2, Int32_out, "G3"); Assert.AreEqual (MaskedTextResultHint.Success, MaskedTextResultHint_out, "G4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_G", 1, true, false, 2, 1, CultureInfo.GetCultureInfo ("es-ES"), 3, true, false, false, 3, 4, @"abaa", true, false, '\x0', '\x5F', true, true, true, @"zb z", @"zb z", @"zb z", @"zb_z", @"z_z", @"zb z", @"z z"); // insert space over literal with only more literals later on mtp = new MaskedTextProvider ("abb"); Assert.AreEqual (false, mtp.InsertAt (' ', 1, out Int32_out, out MaskedTextResultHint_out), "#G1"); Assert.AreEqual (" bb", mtp.ToString (), "G2"); Assert.AreEqual (3, Int32_out, "G3"); Assert.AreEqual (MaskedTextResultHint.UnavailableEditPosition, MaskedTextResultHint_out, "G4"); MaskedTextProviderTest.AssertProperties (mtp, "InsertAt_charTest_G", 1, true, false, 0, 1, CultureInfo.GetCultureInfo ("es-ES"), 1, true, false, false, -1, 3, @"abb", true, false, '\x0', '\x5F', true, true, true, @" bb", @" bb", @" bb", @"_bb", @"_", @" bb", @""); }
public void CloneTest () { MaskedTextProvider mtp; MaskedTextProvider mtp2; int counter = 0; mtp = new MaskedTextProvider ("mask"); mtp2 = (MaskedTextProvider)mtp.Clone (); Assert.AreEqual (mtp.Mask, mtp2.Mask, "#" + (counter++).ToString ()); AssertProperties (mtp2, "CloneTest", counter++, mtp.AllowPromptAsInput, mtp.AsciiOnly, mtp.AssignedEditPositionCount, mtp.AvailableEditPositionCount, mtp.Culture, mtp.EditPositionCount, mtp.IncludeLiterals, mtp.IncludePrompt, mtp.IsPassword, mtp.LastAssignedPosition, mtp.Length, mtp.Mask, mtp.MaskCompleted, mtp.MaskFull, mtp.PasswordChar, mtp.PromptChar, mtp.ResetOnPrompt, mtp.ResetOnSpace, mtp.SkipLiterals, mtp.ToString (), mtp.ToString (true), mtp.ToString (false), mtp.ToString (true, true), mtp.ToString (true, false), mtp.ToString (false, true), mtp.ToString (false, false)); }
/// <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; }
public static string CnpjFormat(string cnpj) { var mtpCnpj = new MaskedTextProvider(@"00\.000\.000/0000-00"); mtpCnpj.Set(LeadingZeros(cnpj, 11)); return mtpCnpj.ToString(); }
private static string GetRawText( MaskedTextProvider provider ) { return provider.ToString( true, false, false, 0, provider.Length ); }
private void UpdateSortedListView(MaskDescriptorComparer.SortType sortType) { if (this.listViewCannedMasks.IsHandleCreated) { MaskDescriptor maskDex = null; if (this.listViewCannedMasks.SelectedItems.Count > 0) { int num = this.listViewCannedMasks.SelectedIndices[0]; maskDex = this.maskDescriptors[num]; } this.maskDescriptors.RemoveAt(this.maskDescriptors.Count - 1); this.maskDescriptors.Sort(new MaskDescriptorComparer(sortType, this.listViewSortOrder)); System.Design.UnsafeNativeMethods.SendMessage(this.listViewCannedMasks.Handle, 11, false, 0); try { this.listViewCannedMasks.Items.Clear(); string str = System.Design.SR.GetString("MaskDescriptorValidatingTypeNone"); foreach (MaskDescriptor descriptor2 in this.maskDescriptors) { string str2 = (descriptor2.ValidatingType != null) ? descriptor2.ValidatingType.Name : str; MaskedTextProvider provider = new MaskedTextProvider(descriptor2.Mask, descriptor2.Culture); provider.Add(descriptor2.Sample); string str3 = provider.ToString(false, true); this.listViewCannedMasks.Items.Add(new ListViewItem(new string[] { descriptor2.Name, str3, str2 })); } this.maskDescriptors.Add(this.customMaskDescriptor); this.listViewCannedMasks.Items.Add(new ListViewItem(new string[] { this.customMaskDescriptor.Name, "", str })); if (maskDex != null) { this.SetSelectedMaskDescriptor(maskDex); } } finally { System.Design.UnsafeNativeMethods.SendMessage(this.listViewCannedMasks.Handle, 11, true, 0); this.listViewCannedMasks.Invalidate(); } } }
/// <summary> /// Format the specified text using the specified mask, prompt /// character, and culture information and return the result /// values. /// </summary> /// <param name="mask">The mask to use.</param> /// <param name="text">The text to format.</param> /// <param name="promptChar">The prompt character to use for missing /// characters. If a null character ('\x0') is specified, prompt /// characters are omitted.</param> /// <param name="culture">The culture information to use. If null, /// the current culture is used.</param> /// <param name="hint">The result of formatting the text.</param> /// <param name="hintPosition">The position related to the result /// hint.</param> /// <returns>The formatted text string.</returns> public static string Format(string mask, string text, char promptChar, CultureInfo culture, out MaskedTextResultHint hint, out int hintPosition) { if(text == null) text = String.Empty; if(culture == null) culture = CultureInfo.CurrentCulture; MaskedTextProvider provider = new MaskedTextProvider(mask, culture); // Set the prompt character options if(promptChar != '\x0') { provider.PromptChar = promptChar; provider.IncludePrompt = true; } // Format and return the string provider.Set(text, out hintPosition, out hint); // Positive hint results are successful if(hint > 0) return provider.ToString(); // Return the text as-is if it didn't fit the mask return text; }