简体   繁体   English

一个班级的Mouse Event,如何在另一个班级听呢?

[英]Mouse Event from one class, how do I listen for it in another class?

I have a UserControl on a Form, when I MouseMove on that UserControl I want to do something in the Form. 我在窗体上有一个UserControl,当我在该UserControl上进行MouseMove移动时,我想在窗体中做一些事情。

How can I make the Form 'listen' for this event? 如何使该活动的表单“监听”?

I am using Visual C#, .Net framework 3.5, winforms 我正在使用Visual C#、. Net Framework 3.5,winforms

I suppose you're referring to a use control or something like that. 我想您指的是使用控件或类似的控件。

You can add a public event , and trigger it inside your class when detecting the inner class event. 您可以添加一个public event ,并在检测内部类事件时在您的类内部触发它。

Then you have to subscribe to the published event in the second class. 然后,您必须订阅第二堂课中的已发布事件。

This is a sample so that you see the sintax: 这是一个示例,以便您看到正弦值:

    public class WithEvent
    {
        // this is the new published event
        public EventHandler<EventArgs> NewMouseEvent;

        // This handles the original mouse event of the inner class
        public void OriginalEventhandler(object sender, EventArgs e)
        {
            // this raises the published event (if susbcribedby any handler)
            if (NewMouseEvent != null)
            {
                NewMouseEvent(this, e);
            }
        }
    }

    public class Subscriber
    {
        public void Handler(object sender, EventArgs e)
        {
            // this is the second class handler
        }

        public void Subscribe()
        {
            WithEvent we = new WithEvent();
            // This is how you subscribe the handler of the second class
            we.NewMouseEvent += Handler;
        }

    }

If you are talking about Windows Forms (it's not clear from the question) you need to define a new event in the class who recieves the mouse-event. 如果您在谈论Windows Forms (问题尚不清楚),则需要在该类中定义一个接收鼠标事件的新事件。 After reciving it raises a new custom-event. 接收后,它将引发一个新的自定义事件。 Another class is subcribed to that (custom-event) a recieves notification. 另一类订阅了(自定义事件)接收通知。

For moe information (it's not something that can be presenteed in a couple of lines) can have alook here: 有关moe的信息(不是几行可以介绍的内容),可以在这里查看:

How to propagate an Event up to the MainForm? 如何将事件传播到MainForm?

If you are talking about WPF , there are different concept of events: event routing. 如果您在谈论WPF ,则有不同的事件概念:事件路由。 If your class is UI element present in UI tree of the component that recieves actually mouse-event, it will be propagated to your class too. 如果您的类是在实际上接收鼠标事件的组件的UI树中存在的UI元素,它也将传播到您的类。 So no need of more coding. 因此,无需更多编码。

To expand a little on the answer from JotaBe, there are two scenarios that I could see you having: 为了进一步说明JotaBe的答案,我可以看到两种情况:

a) class A calls a method in class B, and an exception happens. a)类A调用类B中的方法,并且发生异常。 In this case, you don't need to do anything: exception will walk the stack, until it finds a catch statement. 在这种情况下,您无需执行任何操作:异常将遍历堆栈,直到找到catch语句为止。 So, really, all you need to do is NOT catch an exception, or if you do need to catch it (for logging purposes and such), then rethrow it. 因此,实际上,您要做的就是不捕获异常,或者如果确实需要捕获异常(出于日志记录等目的),则将其重新抛出。

b) if you need to have a code triggered in some unrelated class, as a result of exception, then the best way is to use events. b)如果由于异常而需要在某个不相关的类中触发代码,那么最好的方法是使用事件。 In your class declare: 在您的课程中声明:

public class ClassA
{
    public static event EventHandler<Exception> OnException;

    public void Notify(Exception ex)
    {
        if (OnException != null)
        {
            OnException(this, ex);
        }
    }
}

and then, in order to be notified, all you need is to 然后,为了得到通知,您所需要做的就是

ClassA.OnException += (sender, exeption) => 
{
    ... some GetHashCode ..
};

... I guess JotaBe already added all necessary example code as I was typing ...我猜JotaBe在输入时已经添加了所有必要的示例代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM