English 中文(简体)
某几类活动,我如何在另一类活动中聆听这些活动?
原标题:Mouse Event from one class, how do I listen for it in another class?
  • 时间:2012-05-07 17:29:16
  •  标签:
  • c#

I have a UserControl on a Form, when I MouseMove on that UserControl I want to do something in the Form.

我怎么能为这次活动听取形式发言?

我正在使用视觉C#, .Net framework 3.5,赢家式。

最佳回答

我恳请你提及使用控制或类似控制。

可在检测内班活动时添加<条码>的公开活动,并在你的班子内启动。

然后,你们不得不在第二课堂上加入所出版的活动。

这是一份样本,以便你看到:

    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. 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:

如何向主角宣传一项活动?

如果你谈论<代码>WPF,则事件的概念不同:事件路线。 如果您的班子是植树中actually树成分的元件,该树将传播给你的班子。 因此不需要更多的编码。

为了扩大乔塔贝的答复,我可以看到以下两种情况:

a) A类在B类中采用一种方法,有例外。 在此情况下,你不需要做任何事情:例外情况将步行,直到找到渔获量说明。 因此,实际上,你们都需要做的是,NOT捕获一个例外,或者如果你确实需要捕获(用于伐木目的,这样的话),那么就重新捕获。

b) 如果由于例外情况,需要在某些无关的类别中启动一部法典,那么最好的方法是利用事件。 阁下:

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

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

之后,为了得到通知,你们需要的是:

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

...... 我在打字时已经增加了所有必要的榜样。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签