Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
Übersetzung
Original
Dieser Artikel wurde noch nicht bewertet - Dieses Thema bewerten.

Gewusst wie: Implementieren eines Designers für ein Steuerelement

In diesem Thema wird das Implementieren eines Designers (HelpLabelDesigner) für das HelpLabel-Extenderanbietersteuerelement erläutert, das unter Gewusst wie: Implementieren eines HelpLabel-Extenderanbieters beschrieben wird. Der Designer ist eine geschachtelte Klasse im HelpLabel-Steuerelement. Im Codebeispiel für den Designer werden folgende Aspekte veranschaulicht:

  • HelpLabelDesigner wird von ControlDesigner abgeleitet.

  • HelpLabelDesigner stellt durch Überschreiben der in der IDesigner-Schnittstelle angegebenen Verbs-Eigenschaft ein Designerverb bereit. Zur Entwurfszeit erscheinen Verben als Befehle auf dem Objekt, das mit dem Designer verknüpft ist. Weitere Informationen finden Sie unter Designerverben.

  • HelpLabelDesigner fügt HelpLabel eine Entwurfszeiteigenschaft (TrackSelection) hinzu, indem die von der IDesignerFilter-Schnittstelle angegebene PreFilterProperties-Methode überschrieben wird. Weitere Informationen über das Hinzufügen oder Ersetzen von Eigenschaften und Ereignissen finden Sie unter Filtern von Metadaten.

Das folgende Codebeispiel enthält den Code für den Designer.

Hinweis Hinweis

Der folgende Designercode kann allein nicht kompiliert werden. Kompilieren Sie stattdessen das Beispiel in Gewusst wie: Implementieren eines HelpLabel-Extenderanbieters, das den Code für den Designer als geschachtelte Klasse enthält.


		//
		// <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();
					}
				}
			}
		}


Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)

Community-Beiträge

HINZUFÜGEN
© 2013 Microsoft. Alle Rechte vorbehalten.