Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / EditorServiceContext.cs / 1 / EditorServiceContext.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.Windows.Forms.Design { using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.Windows.Forms.Design.Behavior; using System.Drawing; using System.Drawing.Design; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; ////// /// internal class EditorServiceContext : IWindowsFormsEditorService, ITypeDescriptorContext { private ComponentDesigner _designer; private IComponentChangeService _componentChangeSvc; private PropertyDescriptor _targetProperty; [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal EditorServiceContext(ComponentDesigner designer) { this._designer = designer; } internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor prop) { this._designer = designer; this._targetProperty = prop; if (prop == null) { prop = TypeDescriptor.GetDefaultProperty(designer.Component); if (prop != null && typeof(ICollection).IsAssignableFrom(prop.PropertyType)) { _targetProperty = prop; } } Debug.Assert(_targetProperty != null, "Need PropertyDescriptor for ICollection property to associate collectoin edtior with."); } internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor prop, string newVerbText) : this(designer, prop) { Debug.Assert(!string.IsNullOrEmpty(newVerbText), "newVerbText cannot be null or empty"); _designer.Verbs.Add(new DesignerVerb(newVerbText, new EventHandler(this.OnEditItems))); } public static object EditValue(ComponentDesigner designer, object objectToChange, string propName) { // Get PropertyDescriptor PropertyDescriptor descriptor = TypeDescriptor.GetProperties(objectToChange)[propName]; // Create a Context EditorServiceContext context = new EditorServiceContext(designer, descriptor); // Get Editor UITypeEditor editor = descriptor.GetEditor(typeof(UITypeEditor)) as UITypeEditor; // Get value to edit object value = descriptor.GetValue(objectToChange); // Edit value object newValue = editor.EditValue(context, context, value); if (newValue != value) { try { descriptor.SetValue(objectToChange, newValue); } catch (CheckoutException) { } } return newValue; } ////// Provides an implementation of IWindowsFormsEditorService and ITypeDescriptorContext. /// Also provides a static method to invoke a UITypeEditor given a designer, an object /// and a property name. /// ////// Our caching property for the IComponentChangeService /// private IComponentChangeService ChangeService { get { if (_componentChangeSvc == null) { _componentChangeSvc = (IComponentChangeService)((IServiceProvider)this).GetService(typeof(IComponentChangeService)); } return _componentChangeSvc; } } ////// Self-explanitory interface impl. /// IContainer ITypeDescriptorContext.Container { get { if (_designer.Component.Site != null) { return _designer.Component.Site.Container; } return null; } } ////// Self-explanitory interface impl. /// void ITypeDescriptorContext.OnComponentChanged() { ChangeService.OnComponentChanged(_designer.Component, _targetProperty, null, null); } ////// Self-explanitory interface impl. /// bool ITypeDescriptorContext.OnComponentChanging() { try { ChangeService.OnComponentChanging(_designer.Component, _targetProperty); } catch (CheckoutException checkoutException) { if (checkoutException == CheckoutException.Canceled) { return false; } throw; } return true; } ////// Self-explanitory interface impl. /// object ITypeDescriptorContext.Instance { get { return _designer.Component; } } ////// Self-explanitory interface impl. /// PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor { get { return _targetProperty; } } ////// Self-explanitory interface impl. /// object IServiceProvider.GetService(Type serviceType) { if (serviceType == typeof(ITypeDescriptorContext) || serviceType == typeof(IWindowsFormsEditorService)) { return this; } if (_designer.Component.Site != null) { return _designer.Component.Site.GetService(serviceType); } return null; } ////// Self-explanitory interface impl. /// void IWindowsFormsEditorService.CloseDropDown() { // we'll never be called to do this. // Debug.Fail("NOTIMPL"); return; } ////// Self-explanitory interface impl. /// void IWindowsFormsEditorService.DropDownControl(Control control) { // nope, sorry // Debug.Fail("NOTIMPL"); return; } ////// Self-explanitory interface impl. /// System.Windows.Forms.DialogResult IWindowsFormsEditorService.ShowDialog(Form dialog) { IUIService uiSvc = (IUIService)((IServiceProvider)this).GetService(typeof(IUIService)); if (uiSvc != null) { return uiSvc.ShowDialog(dialog); } else { return dialog.ShowDialog(_designer.Component as IWin32Window); } } ////// When the verb is invoked, use all the stuff above to show the dialog, etc. /// private void OnEditItems(object sender, EventArgs e) { object propertyValue = _targetProperty.GetValue(_designer.Component); if (propertyValue == null) { return; } CollectionEditor itemsEditor = TypeDescriptor.GetEditor(propertyValue, typeof(UITypeEditor)) as CollectionEditor; Debug.Assert(itemsEditor != null, "Didn't get a collection editor for type '" + _targetProperty.PropertyType.FullName + "'"); if (itemsEditor != null) { itemsEditor.EditValue(this, this, propertyValue); } } } } // 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
- ColorAnimationBase.cs
- SingleSelectRootGridEntry.cs
- TextElement.cs
- CommunicationObject.cs
- DmlSqlGenerator.cs
- TemplateControl.cs
- CalendarBlackoutDatesCollection.cs
- SQLBytes.cs
- DynamicRendererThreadManager.cs
- Figure.cs
- AutomationPatternInfo.cs
- TextFormatterHost.cs
- Message.cs
- AliasedExpr.cs
- ExpressionBindingCollection.cs
- DefaultProfileManager.cs
- ContentPosition.cs
- SqlTriggerContext.cs
- UpdatePanel.cs
- BaseContextMenu.cs
- InputEventArgs.cs
- EntityDesignerUtils.cs
- XmlSchemaSimpleTypeList.cs
- GPPOINTF.cs
- EntityPropertyMappingAttribute.cs
- ToolStripPanelRenderEventArgs.cs
- CodeIdentifier.cs
- MessageQueueInstaller.cs
- WindowsTitleBar.cs
- PrivateUnsafeNativeCompoundFileMethods.cs
- CodeDelegateInvokeExpression.cs
- DocumentApplicationJournalEntry.cs
- DependencySource.cs
- Utils.cs
- ReaderOutput.cs
- EndpointConfigContainer.cs
- CacheDependency.cs
- UnrecognizedPolicyAssertionElement.cs
- CacheDependency.cs
- safex509handles.cs
- XhtmlBasicPhoneCallAdapter.cs
- TokenCreationParameter.cs
- FormViewInsertedEventArgs.cs
- HuffCodec.cs
- OleDbCommandBuilder.cs
- LabelAutomationPeer.cs
- MapPathBasedVirtualPathProvider.cs
- GridViewHeaderRowPresenter.cs
- HtmlButton.cs
- ChangeProcessor.cs
- ResourceContainer.cs
- CodeSnippetTypeMember.cs
- CodeArrayCreateExpression.cs
- DebugView.cs
- UpdatePanelTrigger.cs
- PolyBezierSegmentFigureLogic.cs
- SafeProcessHandle.cs
- ArraySortHelper.cs
- Vector3DCollection.cs
- OptimizedTemplateContentHelper.cs
- ThemeDictionaryExtension.cs
- NotifyCollectionChangedEventArgs.cs
- PeerCredential.cs
- Properties.cs
- Rect3DValueSerializer.cs
- QilNode.cs
- SessionPageStateSection.cs
- InputElement.cs
- InvariantComparer.cs
- XmlBoundElement.cs
- LineSegment.cs
- UrlMapping.cs
- Frame.cs
- PositiveTimeSpanValidator.cs
- ManagementObject.cs
- NamedObject.cs
- ValidationRuleCollection.cs
- SendActivityDesigner.cs
- FileReader.cs
- FillRuleValidation.cs
- FormViewRow.cs
- ConfigXmlAttribute.cs
- DataGridViewTopLeftHeaderCell.cs
- StylusButtonEventArgs.cs
- RegexRunner.cs
- ObjectSet.cs
- DataGridViewCellFormattingEventArgs.cs
- IntranetCredentialPolicy.cs
- MobileTextWriter.cs
- HtmlInputText.cs
- DataViewListener.cs
- Timer.cs
- EditableTreeList.cs
- ModelVisual3D.cs
- ExceptionRoutedEventArgs.cs
- ModelProperty.cs
- Filter.cs
- HandlerFactoryCache.cs
- QuaternionIndependentAnimationStorage.cs
- RegexFCD.cs