locked
Can these eventhanler change to a static class ? RRS feed

  • 問題

  •     public partial class A0_Form : Form
    {
    private bool dragging;
    private Point pointClicked;

    public A0_Form()
    {
    InitializeComponent();
    }
    private void Panel_Title_MouseDown(object sender, MouseEventArgs e)
    {
    dragging = (e.Button == MouseButtons.Left);
    pointClicked = (e.Button == MouseButtons.Left) ? (new Point(e.X, e.Y)) : pointClicked;
    }
    private void Panel_Title_MouseMove(object sender, MouseEventArgs e)
    {
    if (dragging)
    {
    Point pointMoveTo;
    pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
    pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
    this.Location = pointMoveTo;
    }
    }
    private void Panel_Title_MouseUp(object sender, MouseEventArgs e)
    {
    dragging = false;
    }
    }
    i want to put the 3 event to a static class ,so when i create new form , i just need to add those eventhandler rather than copy those 3 event everytime
    Panel_Title_MouseDown,
    Panel_Title_MouseMove,
    Panel_Title_MouseUp


    but  there is a problem about the 2 global var , how to solve it ?
    private bool dragging;
    private Point pointClicked;

    class sample:
    public static class Common_Event
        {
            public static void Panel_Title_AddEvent(Panel pan)
            {
                pan.MouseDown += new MouseEventHandler(Panel_Title_MouseDown);
                pan.MouseMove += new MouseEventHandler(Panel_Title_MouseMove);
                pan.MouseUp += new MouseEventHandler(Panel_Title_MouseUp);
            }
            private void Panel_Title_MouseDown(object sender, MouseEventArgs e)
            {
                dragging = (e.Button == MouseButtons.Left);
                pointClicked = (e.Button == MouseButtons.Left) ? (new Point(e.X, e.Y)) : pointClicked;
            }
            private void Panel_Title_MouseMove(object sender, MouseEventArgs e)
            {
                if (dragging)
                {
                    Point pointMoveTo;
                    pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
                    pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
                    this.Location = pointMoveTo;
                }
            }
            private void Panel_Title_MouseUp(object sender, MouseEventArgs e)
            {
                dragging = false;
            }
    }

    form sample:
     public partial class A0_Form : Form
        {
            private bool dragging;
            private Point pointClicked;
    
            public A0_Form()
            {
                InitializeComponent();
                Common_Event.Panel_Title_AddEvent(pan);
            }
    }

    2009年12月12日 10:59

解答

  • Hi,

    I'm affraid you can't place the event handler in a static class. As we know, any field or method in a static class must be static type.
    So the definition for event handler mentioned in your post is not correct. Generally speaking, the event handler method defined by framework wouldn't be a static type.

    If you really want to warp the event handler in a static class, you can warp some operation in it.
    e.g
    1. Declare the handler in your form as follows
    pan.MouseDown += new MouseEventHandler(Panel_Title_MouseDown);
    

    2.Define the event handler method
     protected void Panel_Title_MouseDown(object sender, MouseEventArgs e)
            {
               YourStaticClass.Operaton(sender,e);
    }
    3.Define the static class
    public static class YourStaticClass
    {
    public static void YourOperation(object sender, MouseEventArges e)
    {
    }
    }
    }

    Microsoft Online Community Support
    • 已標示為解答 KeFang Chen 2009年12月18日 5:48
    2009年12月14日 6:04