I have a custom control and I subscribe to an event in the InitializeComponent method. Whenever I go into the designer and move the control or make any changes, the initialize component will rewrite and remove the subscription to the event. Is there any way to avoid this? I would rather not just subscribe in the Load event of the form, but I am unfamiliar with how this method gets dynamically created.
Asked
Active
Viewed 416 times
0
-
1Is there still that comment about not editing a Designer.cs file at the top? – H H Sep 02 '14 at 16:52
-
There is no way around it, it explicitly states that the Method is autogenerated which will get rid of anything you add externally – Mark Hall Sep 02 '14 at 17:01
-
If the custom control is properly written the event you subscribe to should be visible as a "property" in the designer, and then it should be OK to subscribe to it that way. – RenniePet Sep 02 '14 at 17:04
-
@HenkHolterman I imagine he deleted it first, so it should work now, right? – Servy Sep 02 '14 at 17:07
-
Is this custom control one you've written yourself? Or do you have access to the source code and can fix it? Like I say, if it's properly written you can subscribe to events via the designer's properties window, just like you subscribe to the Click event for a Button control. – RenniePet Sep 02 '14 at 17:23
-
You shouldn't modify the code inside `InitializeComponent`. Component's modifications will be wiped out when you open your design view and do something on that control. – nevets Sep 02 '14 at 18:04
-
@RenniePet I think this might be what I am looking for, I'll take a look and see if that gets it working. – ReddShepherd Sep 02 '14 at 18:30
-
If you want I can post an answer that shows the general technique needed to create a user-defined event in a custom control, so that it is visible in the designer, and is compatible with the designer's persisting of the control's properties to Designer.cs. Let me know - it would take me a bit of time, and I don't want to do it if you aren't interested. – RenniePet Sep 02 '14 at 22:51
-
Found an article that does a good job of explaining it: http://www.akadia.com/services/dotnet_user_controls.html The key thing is putting [Category()] and [Description()] and [DefaultValue()] attributes on the public properties and events that you want to work with via the designer, and maybe a [DefaultEvent()] attribute on the whole class. – RenniePet Sep 02 '14 at 23:19
-
@RenniePet The event was a basic click event inherited from a base control, so I didn't declare it. For example if I inherit from the button control and am subscribing to that click event with my custom control. For time purposed, I moved it into the constructor. – ReddShepherd Sep 03 '14 at 20:24
1 Answers
2
This is why you don't edit the designer code. It's specifically designed to not maintain these types of changes.
You should be subscribing to the event in the object's constructor, load event, or some similar location.

Servy
- 202,030
- 26
- 332
- 449
-
I'll subscribe in the constructor. For normal controls, I can subscribe in the initialize components without it disappearing. That is why I was wondering the difference for custom controls. – ReddShepherd Sep 02 '14 at 19:00