如何从一个不同的类注册鼠标事件鼠标、不同、事件

2023-09-03 20:27:02 作者:╳灬ャ香香

我有尝试注册另一个类的事件类。

I have a class which trying to register an event in another class.

在A级我有一个方法,如下所示:

In class A I have a method as shown below:

 public void Mouse_Down(object sender, MouseEventArgs e)
        {

        }

我注册事件类B中,像这样:

I am registering the event in class B like so:

            ClassA classA = new ClassA();
            classA.MouseDown += new MouseEventHandler(classA.Mouse_Down);

当点击什么也没有发生。有谁知道什么问题可以。

When click nothing is happening. Does anyone know what the problem could be.

推荐答案

设置断点在事件触发code(ClassA的),并检查您的事件处理器集合。我敢打赌,没有人注册,你玩弄ClassA的实例。

Set a breakpoint at the event triggering code (of classA) and check your EventHandlers collection. I bet there is no one registered and you're messing with classA instances.

这也可能是你将永远不会得到你的断点,这意味着你的事件不会触发。

It is also possible that you will never get your breakpoint, which means that your event is not fired.

总之,你想要做的事看起来有点怪我。

Anyway the thing that you want to do looks a little weird for me.

编辑:

你试过注册一个不同的方法来'正常方式'同一事件(从ClassA的构造函数前。)?如果您没有访问事件触发code,这是最好的方法,以检查是否有注册的正确处理,在事件中被解雇的那一刻。

Have you tried to register a different method to the same event 'in normal way' (ex. from a ClassA constructor)? If you don't have access to event triggering code, that is to best way to check if there are proper handlers registered, at the moment of event being fired.