How can I know when a Control/Component has been selected in the designer surface?

Platform: WinForms| Category: Custom Designers

You can listen to the SelectionChanged event in your Control Designer.


public class MyContainerDesigner : 
		ParentControlDesigner
{
	public override /*ParentControlDesigner*/ void Initialize(IComponent component)
	{
		base.Initialize(component);
		iSelectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
		if (iSelectionService != null)
			iSelectionService.SelectionChanged += new EventHandler(this.OnSelectionChanged);
	}
	private void OnSelectionChanged(object sender, EventArgs e)
	{
		// To find out the current selection (can be more than 1) do this:
		System.ComponentModel.Design.ISelectionService iSelectionService;
		System.Collections.ICollection selectedComponents;

		this.iamSelected = false;
		iSelectionService = (ISelectionService)this.GetService(typeof(ISelectionService));

		if (iSelectionService != null)
		{
			selectedComponents = iSelectionService.GetSelectedComponents();
			foreach(object selectedComponent in selectedComponents)
			{
				if(selectedComponent == this.Component)
					this.iamSelected = true;
			}
		}
	}
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.