I created a composite control using C# Windows Forms Control Library, when I use the new control in a test program, I want to find a way to detect when did the name of new control changed at design time, what should I do?
How to detect C# control name changed?
1.2k views Asked by TwenteMaster AtThere are 3 answers
On
There is no event "OnNameChanged" and the Name-Property itself is not virtual and therefore not overrideable... I'm afraid, that there is no sure way...
Some suggestions:
Define your own Name-Property with "new", but inherited classes will not automatically overtake this and - even worse - casts to Control and usage of Control.Name will just work around your property...
Define a virtual "MyName"-Property and use this only to write through onto the Control's Name-Property. Still a call on Control.Name will fly by, but at least you can enforce the usage of MyName...
Puffer the Name within your constructor and define a Timer to look regularely if something has changed
Alltogether I must admit, that I would not use any of those... I don't know what exactly you want to achieve, but I'd try to find a different approach to reach this goal...
You can use the
IComponentChangeService(inSystem.ComponentModel.Design) like in this example:This service is only provided in design mode, not at runtime.
I unsubscribe and subscribe again to the
ComponentChangedevent (to avoid multiple subscriptions).In the event handler
OnComponentChangedI check if my name has changed and raise theNameChangedevent.