Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / MaskDescriptor.cs / 1 / MaskDescriptor.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms.Design { using System; using System.ComponentModel; using System.Design; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Threading; ////// MaskDescriptor abstract class defines the set of methods mask descriptors need to implement for the /// MaskedTextBox.Mask UITypeEditor to include as options in the property editor. MaskDescriptor /// types are discovered at designed time by querying the ITypeDiscoveryService service provider from /// the UITypeEditor object. /// public abstract class MaskDescriptor { ////// The mask being described. /// public abstract string Mask { get; } ////// The friendly name of the mask descriptor. /// Used also as the description for the mask. /// public abstract string Name { get; } ////// A sample text following the mask specification. /// public abstract string Sample { get; } ////// A Type representing the type providing validation for this mask. /// public abstract Type ValidatingType { get; } ////// The CultureInfo representing the locale the mask is designed for. /// public virtual CultureInfo Culture { get{ return Thread.CurrentThread.CurrentCulture; } } ////// Determines whether the specified mask descriptor is valid and hence can be added to the canned masks list. /// A valid MaskDescriptor must meet the following conditions: /// 1. Not null. /// 2. Not null or empty mask. /// 3. Not null or empty name. /// 4. Not null or empty sample. /// 5. The sample is correct based on the mask and all required edit characters have been provided (mask completed - not necessarily full). /// 6. The sample is valid based on the ValidatingType object (if any). /// public static bool IsValidMaskDescriptor( MaskDescriptor maskDescriptor ) { string dummy; return IsValidMaskDescriptor(maskDescriptor, out dummy); } public static bool IsValidMaskDescriptor( MaskDescriptor maskDescriptor, out string validationErrorDescription) { validationErrorDescription = string.Empty; if ( maskDescriptor == null ) { validationErrorDescription = SR.GetString(SR.MaskDescriptorNull); return false; } if ( string.IsNullOrEmpty(maskDescriptor.Mask) || string.IsNullOrEmpty(maskDescriptor.Name) || string.IsNullOrEmpty(maskDescriptor.Sample) ) { validationErrorDescription = SR.GetString(SR.MaskDescriptorNullOrEmptyRequiredProperty); return false; } MaskedTextProvider mtp = new MaskedTextProvider( maskDescriptor.Mask, maskDescriptor.Culture ); MaskedTextBox mtb = new MaskedTextBox(mtp); mtb.SkipLiterals = true; mtb.ResetOnPrompt = true; mtb.ResetOnSpace = true; mtb.ValidatingType = maskDescriptor.ValidatingType; mtb.FormatProvider = maskDescriptor.Culture; mtb.Culture = maskDescriptor.Culture; mtb.TypeValidationCompleted += new System.Windows.Forms.TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted); mtb.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected); // Add sample. If it fails we are done. mtb.Text = maskDescriptor.Sample; if (mtb.Tag == null) // Sample was added successfully (MaskInputRejected event handler did not change the mtb tag). { if( maskDescriptor.ValidatingType != null ) { mtb.ValidateText(); } // } if (mtb.Tag != null) // Validation failed. { validationErrorDescription = mtb.Tag.ToString(); } return validationErrorDescription.Length == 0; } private static void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) { MaskedTextBox mtb = sender as MaskedTextBox; mtb.Tag = MaskedTextBoxDesigner.GetMaskInputRejectedErrorMessage(e); } private static void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e) { if (!e.IsValidInput) { MaskedTextBox mtb = sender as MaskedTextBox; mtb.Tag = e.Message; } } ////// Determines whether this mask descriptor and the passed object describe the same mask. /// True if the following conditions are met in both, this and the passed object: /// 1. Mask property is the same. /// 2. Validating type is the same /// Observe that the Name property is not considered since MaskedTextProvider/Box are not /// aware of it. /// public override bool Equals( object maskDescriptor ) { MaskDescriptor descriptor = maskDescriptor as MaskDescriptor; if( !IsValidMaskDescriptor(descriptor) || !IsValidMaskDescriptor(this) ) { return this == maskDescriptor; // shallow comparison. } return ((this.Mask == descriptor.Mask) && (this.ValidatingType == descriptor.ValidatingType)); } ////// override. /// public override int GetHashCode() { string hash = this.Mask; if (this.ValidatingType != null ) { hash += this.ValidatingType.ToString(); } return hash.GetHashCode(); } ////// ToString override. /// public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "{0}/// Implements the manual sorting of items by columns in the mask descriptor table. /// Used by the MaskDesignerDialog to sort the items in the mask descriptors list. /// internal class MaskDescriptorComparer : System.Collections.Generic.IComparer { private SortOrder sortOrder; private SortType sortType; public enum SortType { ByName, BySample, ByValidatingTypeName } public MaskDescriptorComparer(SortType sortType, SortOrder sortOrder) { this.sortType = sortType; this.sortOrder = sortOrder; } public int Compare(MaskDescriptor maskDescriptorA, MaskDescriptor maskDescriptorB) { if( maskDescriptorA == null || maskDescriptorB == null ) { // Since this is an internal class we cannot throw here, the user cannot do anything about this. Debug.Fail( "One or more parameters invalid" ); return 0; } string textA, textB; switch( sortType ) { default: Debug.Fail( "Invalid SortType, defaulting to SortType.ByName" ); goto case SortType.ByName; case SortType.ByName: textA = maskDescriptorA.Name; textB = maskDescriptorB.Name; break; case SortType.BySample: textA = maskDescriptorA.Sample; textB = maskDescriptorB.Sample; break; case SortType.ByValidatingTypeName: textA = maskDescriptorA.ValidatingType == null ? SR.GetString( SR.MaskDescriptorValidatingTypeNone ) : maskDescriptorA.ValidatingType.Name; textB = maskDescriptorB.ValidatingType == null ? SR.GetString( SR.MaskDescriptorValidatingTypeNone ) : maskDescriptorB.ValidatingType.Name; break; } int retVal = String.Compare(textA, textB); return sortOrder == SortOrder.Descending ? -retVal : retVal; } [SuppressMessage("Microsoft.Performance", "CA1801:AvoidUnusedParameters")] [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public int GetHashCode(MaskDescriptor maskDescriptor) { if( maskDescriptor != null ) { return maskDescriptor.GetHashCode(); } Debug.Fail("Null maskDescriptor passed."); return 0; } [SuppressMessage("Microsoft.Performance", "CA1801:AvoidUnusedParameters")] [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public bool Equals( MaskDescriptor maskDescriptorA, MaskDescriptor maskDescriptorB ) { if( !MaskDescriptor.IsValidMaskDescriptor(maskDescriptorA) || !MaskDescriptor.IsValidMaskDescriptor(maskDescriptorB) ) { return maskDescriptorA == maskDescriptorB; // shallow comparison. } return maskDescriptorA.Equals(maskDescriptorB); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- StaticDataManager.cs
- UICuesEvent.cs
- ConfigurationSchemaErrors.cs
- PeerNearMe.cs
- Function.cs
- PaperSize.cs
- ModuleConfigurationInfo.cs
- ReadOnlyCollectionBase.cs
- SqlExpander.cs
- MsmqIntegrationSecurityMode.cs
- ServiceReference.cs
- StartUpEventArgs.cs
- ChildrenQuery.cs
- TypeConverter.cs
- ContainerActivationHelper.cs
- FixedSOMContainer.cs
- Converter.cs
- WebPartsPersonalization.cs
- TagMapInfo.cs
- RolePrincipal.cs
- AuthenticationServiceManager.cs
- SuppressMergeCheckAttribute.cs
- DataGridToolTip.cs
- CompilationSection.cs
- StoragePropertyMapping.cs
- KeyboardDevice.cs
- DocumentOrderQuery.cs
- TreeNodeSelectionProcessor.cs
- FixedSOMTableCell.cs
- LinqDataSourceUpdateEventArgs.cs
- RowUpdatingEventArgs.cs
- DesignTimeVisibleAttribute.cs
- DataGridViewCellStyle.cs
- ProgressPage.cs
- ConstraintStruct.cs
- GeneralTransform3DCollection.cs
- EventLogException.cs
- XmlDataSourceView.cs
- IisTraceListener.cs
- RawMouseInputReport.cs
- CryptoHelper.cs
- ExpandSegmentCollection.cs
- Int64AnimationUsingKeyFrames.cs
- XPathDocumentNavigator.cs
- SiteMapNodeItem.cs
- ValidatedControlConverter.cs
- RSACryptoServiceProvider.cs
- TableStyle.cs
- NameValueCollection.cs
- HostingPreferredMapPath.cs
- WebColorConverter.cs
- ConfigsHelper.cs
- MemberHolder.cs
- PackWebResponse.cs
- EvidenceTypeDescriptor.cs
- NullExtension.cs
- EdmError.cs
- ChildChangedEventArgs.cs
- AuthenticationModuleElement.cs
- StatusBarDrawItemEvent.cs
- QueueProcessor.cs
- DropSourceBehavior.cs
- PreservationFileReader.cs
- RC2CryptoServiceProvider.cs
- TriState.cs
- GridViewDeletedEventArgs.cs
- FontConverter.cs
- KeyedHashAlgorithm.cs
- CachedFontFamily.cs
- BindingBase.cs
- TemplatedControlDesigner.cs
- CharAnimationBase.cs
- HitTestParameters3D.cs
- Geometry.cs
- XhtmlTextWriter.cs
- HGlobalSafeHandle.cs
- CodeConstructor.cs
- WebZone.cs
- Latin1Encoding.cs
- ITreeGenerator.cs
- SmiSettersStream.cs
- SchemaInfo.cs
- CroppedBitmap.cs
- UseLicense.cs
- GPRECT.cs
- WebEventCodes.cs
- AttachedPropertyMethodSelector.cs
- PartialCachingControl.cs
- XmlReturnReader.cs
- HWStack.cs
- TraceUtility.cs
- basenumberconverter.cs
- TableItemPattern.cs
- AnimatedTypeHelpers.cs
- ColumnHeaderCollectionEditor.cs
- CodeTypeParameterCollection.cs
- XamlDesignerSerializationManager.cs
- updateconfighost.cs
- ResourceReferenceKeyNotFoundException.cs
- TableLayoutPanel.cs