在窗体定义一个BUTTON类型的属性。P_BUTTON(在BUTTON添加了一些事件比如(确定,取消))
继承这个窗体在窗体添加一个BUTTON1。
设置窗体的P_BUTTON属性为这个BUTTON1。
如何把这个BUTTON1的事件放到窗体层来。
    public class ebutton : System.Windows.Forms.Button
    {
        public event EventHandler  ok;    }
    public class eform : System.Windows.Forms.Form
    {
        private ebutton pbtn = null;        public event EventHandler formOk;        public ebutton p_button
        {
            get { return this.pbtn; }            set { this.pbtn = value; }
        }        public eform()
            : base()
        {
            this.pbtn.ok += new EventHandler(pbtn_ok);        }        void pbtn_ok(object sender, EventArgs e)
        {
            formOk(sender, e);
        }    }
在这个窗体上添加一个EBUTTON铵钮但不会触发FORMOK事件,请教高手??

解决方案 »

  1.   


    没看出来添加EBUTTON为什么会触发FORMOK事件....
      

  2.   

    建议你学习一个.net的事件委托模型。就可以理解和解决这个问题了。解决方法:
    1.给ebutton添加一个方法: public class ebutton:System.Windows.Forms.Button
     {
       private void OnOK(object sender,EventArgs arg)
       {
         if(this.ok!=null)
           this.ok(sender,arg);
       }
     }2.给ebutton添加一个构造方法: public class ebutton:System.Windows.Forms.Button
     {
       public ebutton()
       {
         this.OnClick+=new EventHandler(this.OnOK);
       }
     }
      

  3.   

    很久没来了,这里人气好像不如从前了
    public class eButton:System.Windows.Forms.Button 
    {
                    public event EventHandler  ok;               //你缺少了eButton OK事件的触发呀,所以你的OK事件永远没执行
                   protected override void OnClick(EventArgs e)
                   {
                       if(ok!=null)
                          ok(this,EventArgs.Empty);
                   } }
      

  4.   

    同意8搂的作法~
    顺便问一下,6搂的为什么要再套一个OnOK函数的壳子,是出于设计方面的考虑?
      

  5.   

      public class eButton:System.Windows.Forms.Button 
        {
                    public event EventHandler  ok;               //你缺少了eButton OK事件的触发呀,所以你的OK事件永远没执行
                   protected override void OnClick(EventArgs e)
                   {
                       if(ok!=null)
                          ok(this,EventArgs.Empty);
                   }    }
      

  6.   

     public class ebutton:System.Windows.Forms.Button
     {
       public ebutton()
       {
         this.OnClick+=new EventHandler(this.OnOK);
       }
     }这一步的确是忘些了。
    不过这个也不是重要的。我要的是把事件传到窗体层。
      

  7.   

      public class eButton:System.Windows.Forms.Button 
        {
                    public event EventHandler  ok;               //你缺少了eButton OK事件的触发呀,所以你的OK事件永远没执行
                   protected override void OnClick(EventArgs e)
                   {
                       if(ok!=null)
                          ok(this,EventArgs.Empty);
                   }    }LZ试下