I have a custom control with a property allowing to select another control of a specific type on a form. For simplicity, let it be TextBox controls.
The property of my custom control looks like this:
private TextBox _targetControl;
public TextBox TargetControl
{
get {return _targetControl;}
set {_targetControl = value;}
}
Is there a way to get a notification in my custom control when the selected target control is removed from the form at design time? Note that the target control can be located in a container such as Panel (i.e. it is not an item of the form's Controls collection).
To receive notifications when a Component is added, removed, renamed or otherwise changed at Design-Time, you can call the [Component].GetService() method to initialize an IComponentChangeService on the current Site.
This Interface allows to subscribe to events, as IComponentChangeService.ComponentRemoved, that are raised when something happens to a Component in the Form Designer.
For example, when this UserControl is sited, it subscribes to the
ComponentRemovedandComponentRemovingevents, to verify whether the event is related to the Control referenced by theTargetControlProperty: