小弟现在写了一个button类,是可移动的button 。代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;namespace WindowsApplication1
{
    public class MoveButton : System.Windows.Forms.Button
    {
        bool moveStart;
        Point mousePosition;
        public MoveButton() 
        {        }
        protected override void OnMouseDown (System.Windows.Forms.MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            moveStart = true;
            mousePosition = mevent.Location;
        }
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs mevent)
        {
            base.OnMouseMove(mevent);
            if (moveStart)
            {
                int movedX, movedY;
                movedX = mousePosition.X - mevent.X;
                movedY = mousePosition.Y - mevent.Y;
                this.Location = new Point(this.Location.X - movedX,   this.Location.Y - movedY);     
            }
        }
        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            moveStart = false;
        }}
然后在form1内,new了一百个这样的button,代码如下:
public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                int TempInt = i % 5;
                int TempInt2 = (int)i / 5;
                MoveButton myButton = new MoveButton();
                myButton.Size = new Size(75, 25);
                myButton.Text = "button" + (i + 1);
                myButton.Name = "button" + (i + 1);
                myButton.Left = 0 + TempInt * 75;
                myButton.Top = 0 + TempInt2 * 25;
                this.Controls.Add(myButton);
  } }
现在,想给这个movebutton类写一个自定义事件,就是如果这个button,被鼠标拖动超过了form的边框,能触发这个事件,并且使这个button不会移出此form,同时弹出messagebox,显示“button1(或者button2,3....100)超过移动范围”。请教各位大虾。小弟对于自定义事件还不了解。

解决方案 »

  1.   

    public delegate void MyHandler(string msg);public event MyHandler OnMyEvent; 
    然后当button被移动的时候,检测边界,如果超出边界就
    触发事件
    if(OnMyEvent!=null)
    OnMyEvent("msgbox msg")
      

  2.   

    声明就可以了,然后就可以直接调用了。
    public delegate void MyHandler(string msg);public event MyHandler OnMyEvent; 在控件的move事件里处理
    this.Move+=new EventHandler(button_Move);
    private void button_Move(object sender, System.EventArgs e)
    {
      if(this.Left<this.FindForm().Left)
         OnMyEvent("超出左边界!");
    }在button控件那里就会出现OnMyEvent事件
      

  3.   

    我试了,在移动button并触发事件时,会产生一个NullReferenceException.指向  OnMyEvent("outrange!");这句。
      

  4.   

    if(OnMyEvent!=null)
    OnMyEvent("msgbox msg")
      

  5.   

    你不用事件,直接MessageBox.show("outrange!")就行了,如果用事件,是需要用户添加这个事件,才会处理的(交给拥护处理)
      

  6.   

    private void button_Move(object sender, System.EventArgs e)
    {
    if(this.Left<this.FindForm().Left)
    MessageBox.Show("超出左边界!");
    }
      

  7.   

    最终button类
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public delegate void MyHandler(string msg);    public class MoveButton : System.Windows.Forms.Button
        {
            bool moveStart;
            Point mousePosition;
            public MoveButton() 
            {        }        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs mevent)
            {
                base.OnMouseDown(mevent);
                moveStart = true;
                mousePosition = mevent.Location;
            }        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs mevent)
            {
                base.OnMouseMove(mevent);
                this.Move += new EventHandler(button_Move);
                if (moveStart)
                {
                    int movedX, movedY;
                    movedX = mousePosition.X - mevent.X;
                    movedY = mousePosition.Y - mevent.Y;
                    this.Location = new Point(this.Location.X - movedX, this.Location.Y - movedY);     
                }
            }        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)
            {
                base.OnMouseUp(mevent);
                moveStart = false;
            }
            
            public event MyHandler OnMyEvent;        private void button_Move(object sender, System.EventArgs e)
            {
                if (this.Right > this.FindForm().Width)
                {
                    
                    MessageBox.Show(this.Name + " is out range!");
                    this.Left = this.FindForm().Width - this.Width-6;
                    moveStart = false;
                    if (OnMyEvent != null)
                    {
                        OnMyEvent("outrange!");
                    }
                }
                if (this.Left < 0)
                {                MessageBox.Show(this.Name + " is out range!");
                    this.Left = 0;
                    moveStart = false;
                    if (OnMyEvent != null)
                    {
                        OnMyEvent("outrange!");
                    }
                }
                if (this.Top < 0)
                {                MessageBox.Show(this.Name + " is out range!");
                    this.Top = 0;
                    moveStart = false;
                    if (OnMyEvent != null)
                    {
                        OnMyEvent("outrange!");
                    }
                }
                if (this.Bottom > this.FindForm().Height - this.Height)
                {                MessageBox.Show(this.Name + " is out range!");
                    this.Top = this.FindForm().Height - 2*this.Height - 1;
                    moveStart = false;
                    if (OnMyEvent != null)
                    {
                        OnMyEvent("outrange!");
                    }
                }
            }         
        }
    }