开发者

Custom Control that exposes Button Control's OnClick event

开发者 https://www.devze.com 2023-04-09 09:35 出处:网络
I need to design a custom control that is basically going to be a fancy button, let\'s call it CtrlButton.

I need to design a custom control that is basically going to be a fancy button, let's call it CtrlButton.

I want to drop these on my blank form, then go to the Events for my control and specify what to do for the Click event.

If my custom control CtrlButton contained 1 Windows Form Button named button1, could I simply expose the underlying Click event handler for the button1?

public EventHandler Click {
  get { return button1.Click; }
  set { button1.Click = value; }
}

This code doesn't work! But, that's essentially what I'm trying to do.

EDIT: I see I was awarded Popular Question for this post, yet it sti开发者_运维百科ll sits there at -1. If this question helps you find your answer, please vote it up.


Button.Click is an event, not a property. You can't get and set its value, because it's not a property; you can only add and remove handlers, because it's an event and that's how events work.

But you can write your own event with custom add and remove handlers that wrap the Button:

public event EventHandler Click {
    add { button1.Click += value; }
    remove { button1.Click -= value; }
}


If this is derived from a standard Button control, as this is:

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsControlLibrary1
{
   public class RedButton : Button
   {
       public RedButton()
       {
           base.BackColor = Color.Red;
       }
   }
}

the events are available. If you have completely customized to make your own button, you will have to wire up all of the events, ala this post.

0

精彩评论

暂无评论...
验证码 换一张
取 消