Question

I created a custom user control and want it to work like a Button.

It works now, But I have problem, when I click it in visual studio's designer window, it always generate code that handlers user control's Load event, but I want visual studio handler to handle it's click event by default.

How can I do that?

Was it helpful?

Solution

Thanks a lot for Udan

I found the solution. I just declare like this, then everything okay now

[DefaultEvent("Click")]
public partial class FlatButton : UserControl
{
}

OTHER TIPS

In Properties window, you have a option to change the default event of your control.

  • First, you select the control in the form, then see the top of the properties window.

  • Choose the Events button on the tool bar in the Properties window to view the Events page in the Properties window. Scroll down to the Click event, and enter Button_Click in the box. Choose the ENTER key.

Image of Properties window

  • The IDE adds a Click event handler called label_Click() to the code, and hooks it to each of the labels on the form.

  • Fill in the rest of the code in the event

frist you should decleare function structure

public delegate void _func(your param);

then decleare your event

public event _func NewEvent;

and you should call it in your usercontrol event,for exam if you want to run this event when user clicked on the lable in your user control,on the lable event you should add this:

private void lbl_click(object sender,EventArgs e){
   if(this.NewEvent != null){
       this.NewEvent(your param)
   }
}

finlay when you use your user control and add it in your other project you can see your NewEvent in properties->event windows

I had a particular situation related to setting the default Click event. While the self-answer from @dexiang is a simple solution, I needed to handle the click event internally as well as provide external default event.

For my user control, I have a picturebox and label that I need to handle the click events for while also expose the click of the entire control.

-----------------
| ------------- |
| |PictureBox | |
| ------------- |
|               |
| ------------- |
| |  Label    | |
| ------------- |
-----------------

To accomplish that, I needed to name the default click handler something different than Click. I chose [DefaultEvent("ControlClick")]

Then in the constructor for the UserControl, right after InitializeComponent(); I have click event handlers for the control, picturebox, and label:

this.Click += OnClick;
this.PictureBox1.Click += OnClick;
this.Label1.Click += OnClick;

I also expose the custom event with:

 public event EventHandler ControlClick;

The click event handler looks like:

private void OnClick(object sender, EventArgs e)
     {
     // Invoke parent's button click handler if exists
     ControlClick?.Invoke(sender, e);

     //... code to process click...

Hopefully this helps someone else as I originally struggled trying to use the Click default name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top