Rob
Rob

Reputation: 3381

Is there a way to stop the WinForms designer defaulting to control Load event

This drives me nuts. In the Visual Studio WinForms designer, if you double-click a control it will produce a XXXX_Load handler for that control. This is never, ever, what you want. You want Click event or a Changed event or more likely to go to the existing Click event. You do NOT was a new Load event. It is worse because a double click can happen by accident. You click once, then again - bingo a Load event handler. I use a pen-tablet combo and double clicks are very easy!

Can someone tell me if there is a way to stop this happening. I would like a Click handler. I would settle for nothing happening.

Upvotes: 1

Views: 782

Answers (2)

Ron Beyer
Ron Beyer

Reputation: 11273

In addition to Mehrzad's answer, you can completely disable it by passing an empty string to the DefaultEvent attribute:

[DefaultEvent("")]
public class MyCustomControl : UserControl
{

}

This works because the default control designer uses the ComponentDesigner.DoDefaultAction when the control is double clicked. By default, this uses reflection to look up the method signature of the default event. If it can't find it, it doesn't do anything.

Upvotes: 2

Mehrzad Chehraz
Mehrzad Chehraz

Reputation: 5137

One way to achieve that is inheriting the control and modifying the DefaultEventAttribute.

Specifies the default event for a component.

[DefaultEvent("Click")]
public class MyRadionButton : RadionButton {
}

But some care/extra work may need to be done when you override a control. You need to keep that in mind when using this approach.

Upvotes: 5

Related Questions