日期:2014-05-17  浏览次数:20715 次

给picturebox控件写一个click事件 为什么没反应
  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tank mtk = new tank();

            mtk.Image = Image.FromFile("black.png");
            mtk.Location = new Point(100, 200);
            mtk.SizeMode = PictureBoxSizeMode.AutoSize;
            this.Controls.Add(mtk);
            mtk.yourkeypress += new tank.mykprs(mtk_yourkeypress);
        }

     

        void mtk_yourkeypress(object sender,MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            { MessageBox.Show("haha", "haha"); }
        }


        public class tank : PictureBox
        {
            public delegate void mykprs(object sender, MouseEventArgs e);
            public event mykprs yourkeypress;
        }

    }



哪里有错误.  一共就剩20分了.

------解决方案--------------------
public class tank : PictureBox
        {
            public delegate void mykprs(object sender, MouseEventArgs e);
            public event mykprs yourkeypress;
            protected override void OnMouseClick(MouseEventArgs e)
            {
                if (yourkeypress != null)
                    yourkeypress(this, e);
                base.OnMouseClick(e);
            }
        }