简体   繁体   English

如何创建自定义事件?

[英]How can I create a custom event?

I have a custom set of UserControls: NavigationBar and NavigationItem. 我有一个自定义的UserControls集:NavigationBar和NavigationItem。

I'd like that whenever the user clicks anywhere in the NavigationItem, an event is fired. 我希望每当用户单击NavigationItem中的任何位置时,都会触发一个事件。 I don't know how to set this up. 我不知道该如何设置。

http://i.stack.imgur.com/ocP2D.jpg http://i.stack.imgur.com/ocP2D.jpg

I've tried this: 我已经试过了:

public partial class NavigationBar : UserControl
{
    public NavigationBar()
    {
        InitializeComponent();
        SetupEvents();
    }

    public List<NavigationItem> NavigationItems { private get; set; }
    public NavigationItem SelectedItem { get; set; }

    private void SetupEvents()
    {
        navigationItem1.Click += new EventHandler(navigationItemClick);
    }

    void navigationItemClick(object sender, EventArgs e)
    {
        MessageBox.Show("Clicked on " + sender.ToString());
    }
}

But that event only fires when the user specifically clicks on the NavigationItem control, but not when he clicks on the picture or text. 但是,只有在用户专门单击NavigationItem控件时才会触发该事件,而在用户单击图片或文本时则不会触发。 (Those are PictureBox and Label). (这些是PictureBox和Label)。

What would be the best course of action? 最好的行动方针是什么? I'd like to create something well, not hacky code. 我想创建一些不错的东西,而不是hacky代码。 Thanks! 谢谢!

Put something like this into your class: 在您的班级中输入以下内容:

public event EventHandler NavigationItemClick;

This creates a new event in your class named NavigationItemClick . 这将在您的名为NavigationItemClick的类中创建一个新事件。 The form designer will even see it. 表单设计师甚至会看到它。

In your method navigationItemClick you can do this to call the event. 在方法navigationItemClick您可以执行此操作以调用事件。

EventHandler handler = this.NavigationItemClick;
if (handler != null)
{
    handler(this, EventArgs.Empty);
}

It is important to save the event into the handler variable to avoid race conditions. 将事件保存到handler变量中很重要,以避免出现竞争情况。 EventHandler is a delegate, so you call it like a method, hence the line in the if statement. EventHandler是一个委托,因此您可以像调用方法一样调用它,因此if语句中的行。 The if itself makes sure that someone has attached to your event. if本身可确保有人已加入您的活动。

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

相关问题 如何在自定义用户控件上创建单击事件? - How can I create a click event on a custom user control? 在以下情况下,如何为自定义控件创建名为valuechanged的自定义事件 - How can I create a custom event named valuechanged for a custom control when 如何创建自定义UserControl OnClosing事件并仅在用户未取消的情况下处理该表单 - How can I create a custom UserControl OnClosing event and only dispose of the form if the user does NOT cancel 如何使用EventWaitHandle创建事件? - How can I use EventWaitHandle to create an event? 如何为自定义控件创建事件 - How to create an event for custom control 如何创建自定义SynchronizationContext,以便我自己的单线程事件循环可以处理所有延续? - How do I create a custom SynchronizationContext so that all continuations can be processed by my own single-threaded event loop? 如何从用户控件中引发自定义路由事件? - How can I raise a custom Routed Event from user control? 如何从DataTemplate引发自定义的路由事件? - How can I raise a custom Routed Event from a DataTemplate? 我该如何在style-&gt; eventsetter中处理自定义事件? - how can i handle my custom event in style->eventsetter? 如何在事件处理程序中使用acumatica自定义属性? - How can I use acumatica custom attribute in Event Handler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM