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
- AuthenticationModulesSection.cs
- ServiceMetadataPublishingElement.cs
- hwndwrapper.cs
- AttachmentService.cs
- GridEntry.cs
- IconBitmapDecoder.cs
- TaskFileService.cs
- RegexMatchCollection.cs
- IncrementalHitTester.cs
- GetIndexBinder.cs
- MobileResource.cs
- CultureSpecificStringDictionary.cs
- XmlSchemaAppInfo.cs
- WarningException.cs
- safemediahandle.cs
- DefaultSettingsSection.cs
- SHA384.cs
- ThicknessKeyFrameCollection.cs
- HierarchicalDataSourceDesigner.cs
- CheckBoxRenderer.cs
- UnknownMessageReceivedEventArgs.cs
- RenderData.cs
- Point3DCollection.cs
- base64Transforms.cs
- QilLoop.cs
- StringConcat.cs
- DetailsViewCommandEventArgs.cs
- GrammarBuilderWildcard.cs
- _ListenerRequestStream.cs
- SecurityCriticalDataForSet.cs
- SynchronizedDispatch.cs
- UnknownBitmapEncoder.cs
- WebControl.cs
- ChildChangedEventArgs.cs
- EventBuilder.cs
- HostExecutionContextManager.cs
- ImplicitInputBrush.cs
- IIS7UserPrincipal.cs
- XPathBinder.cs
- SafeArrayRankMismatchException.cs
- FlowDocumentReaderAutomationPeer.cs
- Model3D.cs
- ResXResourceWriter.cs
- _ProxyChain.cs
- QilSortKey.cs
- GridViewUpdateEventArgs.cs
- JavaScriptObjectDeserializer.cs
- pingexception.cs
- TypeConstant.cs
- HtmlAnchor.cs
- PersonalizationStateInfo.cs
- KerberosSecurityTokenParameters.cs
- BasePropertyDescriptor.cs
- BoundField.cs
- Base64WriteStateInfo.cs
- ValueExpressions.cs
- StyleSheetComponentEditor.cs
- EventlogProvider.cs
- PeerHopCountAttribute.cs
- DataSourceHelper.cs
- GridErrorDlg.cs
- QuaternionRotation3D.cs
- TraceHandlerErrorFormatter.cs
- DataGridParentRows.cs
- BaseTreeIterator.cs
- ItemList.cs
- TraceXPathNavigator.cs
- Util.cs
- DataSourceCache.cs
- TypeConvertions.cs
- UnionCodeGroup.cs
- HttpRequest.cs
- SystemColors.cs
- PerformanceCounterScope.cs
- FilterableAttribute.cs
- TextFindEngine.cs
- TransformGroup.cs
- ButtonBaseAutomationPeer.cs
- ToolStripArrowRenderEventArgs.cs
- SourceChangedEventArgs.cs
- uribuilder.cs
- Double.cs
- DrawingContext.cs
- SqlNodeAnnotation.cs
- ProxyHwnd.cs
- DataBoundControl.cs
- XmlTypeAttribute.cs
- LinkConverter.cs
- LifetimeServices.cs
- SerializationInfo.cs
- InvariantComparer.cs
- TextLineResult.cs
- ComNativeDescriptor.cs
- FileLogRecord.cs
- DBSchemaRow.cs
- CodeExpressionStatement.cs
- PropertyMetadata.cs
- NonVisualControlAttribute.cs
- SecurityChannelFaultConverter.cs
- Queue.cs