How to: Implement a Designer for a Control

This topic describes how to implement a designer (HelpLabelDesigner) for the HelpLabel extender provider control described in the How to: Implement a HelpLabel Extender Provider. The designer is a nested class in the HelpLabel control. The designer code example demonstrates the following points:

  • HelpLabelDesigner derives from ControlDesigner.

  • HelpLabelDesigner provides a designer verb by overriding the Verbs property specified in the IDesigner interface. At design time, verbs appear as commands on the object that is associated with the designer. For more information, see Designer Verbs.

  • HelpLabelDesigner adds a design-time property (TrackSelection) to HelpLabel by overriding the PreFilterProperties method specified by the IDesignerFilter interface. For more information about adding or replacing properties and events, see Metadata Filtering.

The following code example contains the code for the designer.

Note Note

The following designer code by itself will not compile. Instead, compile the example in How to: Implement a HelpLabel Extender Provider, which contains the code for the designer as a nested class.

		// 
		// <doc> 
		// <desc> 
		//      This is a designer for the HelpLabel.  This designer provides 
		//      design time feedback for the label.  The help label responds 
		//      to changes in the active control, but these events do not 
		//      occur at design time.  In order to provide some usable feedback 
		//      that the control is working the right way, this designer listens 
		//      to selection change events and uses those events to trigger active 
		//      control changes. 
		// </desc> 
		// </doc> 
		//
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
		public class HelpLabelDesigner : System.Windows.Forms.Design.ControlDesigner 
		{

			private bool trackSelection = true;

			/// <summary> 
			/// This property is added to the control's set of properties in the method 
			/// PreFilterProperties below.  Note that on designers, properties that are 
			/// explictly declared by TypeDescriptor.CreateProperty can be declared as 
			/// private on the designer.  This helps to keep the designer's publi 
			/// object model clean. 
			/// </summary>
            [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
			private bool TrackSelection
			{
				get
				{
					return trackSelection;
				}
				set
				{
					trackSelection = value;
					if (trackSelection)
					{
						ISelectionService ss = (ISelectionService)GetService(typeof(ISelectionService));
						if (ss != null)
						{
							UpdateHelpLabelSelection(ss);
						}
					}
					else
					{
						HelpLabel helpLabel = (HelpLabel)Control;
						if (helpLabel.activeControl != null)
						{
							helpLabel.activeControl = null;
							helpLabel.Invalidate();
						}
					}
				}
			}

			public override DesignerVerbCollection Verbs
			{
				get
				{
					DesignerVerb[] verbs = new DesignerVerb[] {
																  new DesignerVerb("Sample Verb", new EventHandler(OnSampleVerb))
															  };
					return new DesignerVerbCollection(verbs);
				}
			}

			// 
			// <doc> 
			// <desc> 
			//      Overrides Dispose.  Here we remove our handler for the selection changed 
			//      event.  With designers, it is critical that they clean up any events they 
			//      have attached.  Otherwise, during the course of an editing session many 
			//      designers may get created and never destroyed. 
			// </desc> 
			// </doc> 
			// 
			protected override void Dispose(bool disposing) 
			{
				if (disposing) 
				{
					ISelectionService ss = (ISelectionService)GetService(typeof(ISelectionService));
					if (ss != null) 
					{
						ss.SelectionChanged -= new EventHandler(OnSelectionChanged);
					}
				}

				base.Dispose(disposing);
			}

			// 
			// <doc> 
			// <desc> 
			//       Overrides initialize.  Here we add an event handler to the selection service. 
			//      Notice that we are very careful not to assume that the selection service is 
			//      available.  It is entirely optional that a service is available and you should 
			//      always degrade gracefully if a service could not be found. 
			// </desc> 
			// </doc> 
			// 
			public override void Initialize(IComponent component) 
			{
				base.Initialize(component);

				ISelectionService ss = (ISelectionService)GetService(typeof(ISelectionService));
				if (ss != null) 
				{
					ss.SelectionChanged += new EventHandler(OnSelectionChanged);
				}
			}

			private void OnSampleVerb(object sender, EventArgs e)
			{
				MessageBox.Show("You have just invoked a sample verb.  Normally, this would do something interesting.");
			}

			// 
			// <doc> 
			// <desc> 
			//      Our handler for the selection change event.  Here we update the active control within 
			//      the help label. 
			// </desc> 
			// </doc> 
			// 
			private void OnSelectionChanged(object sender, EventArgs e) 
			{
				if (trackSelection)
				{
					ISelectionService ss = (ISelectionService)sender;
					UpdateHelpLabelSelection(ss);
				}
			}

			protected override void PreFilterProperties(IDictionary properties)
			{
				// Always call base first in PreFilter* methods, and last in PostFilter* 
				// methods. 
				base.PreFilterProperties(properties);

				// We add a design-time property called "TrackSelection" that is used to track
				// the active selection.  If the user sets this to true (the default), then 
				// we will listen to selection change events and update the control's active 
				// control to point to the current primary selection.
				properties["TrackSelection"] = TypeDescriptor.CreateProperty(
					this.GetType(),        // the type this property is defined on 
					"TrackSelection",    // the name of the property 
					typeof(bool),        // the type of the property 
					new Attribute[] {CategoryAttribute.Design});    // attributes
			}

			/// <summary> 
			/// This is a helper method that, given a selection service, will update the active control 
			/// of our help label with the currently active selection. 
			/// </summary> 
			/// <param name="ss"></param>
			private void UpdateHelpLabelSelection(ISelectionService ss)
			{
				Control c = ss.PrimarySelection as Control;
				HelpLabel helpLabel = (HelpLabel)Control;
				if (c != null)
				{
					helpLabel.activeControl = c;
					helpLabel.Invalidate();
				}
				else
				{
					if (helpLabel.activeControl != null)
					{
						helpLabel.activeControl = null;
						helpLabel.Invalidate();
					}
				}
			}
		}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft