我在哪里可以在Visual Studio中找到事件声明[已解决] [英] Where do I find event declarations in Visual Studio [Solved]

查看:127
本文介绍了我在哪里可以在Visual Studio中找到事件声明[已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道VS会通过双击事件打开一个eventhandler存根。
我在InitializeComponent中找到了按钮所在窗体的底层事件声明。

  this.buttonWorkOn.Click + = new System.EventHandler(this.buttonWorkOn_Click); 

我可以使用这个事件声明(Visual Studio)并注册另一个事件处理方法吗?



在实例化该其他表单之后,其事件处理方法将需要向主表单上的按钮的点击事件注册。

我有即使我已经阅读了很多关于代表和事件的原理,我也不知道该怎么做,实际上我明白它是如何工作的。



谢谢

解决方案

如果您右键单击事件处理程序在代码编辑器中并浏览定义,您将找到它被声明的方式,然后您可以在自己的代码中使用。



例如, 按钮点击事件是:

 公共事件EventHandler点击; 

您可以自己添加,并从其他地方使用它们来响应您创建的任何类中的事件。



这是一个带有单个按钮(通过设计师添加)的示例表单,当点击时将会提高自己的活动:

  public partial class Form1:Form 
{
public event EventHandler ButtonClicked;

private void RaiseButtonClicked()
{
if(ButtonClicked!= null)
ButtonClicked(this,EventArgs.Empty);
}

public Form1()
{
InitializeComponent();
}

private void Button1_Click(object sender,EventArgs e)
{
RaiseButtonClicked();
}
}

在另一个类中,您可以添加一个处理程序:

  public class Responder 
{
public Responder(Form1 form)
{
form.ButtonClicked + = OnButtonClicked;
}

private void OnButtonClicked(object sender,EventArgs args)
{
MessageBox.Show(Button was click);
}
}

现在Responder类的每个实例都会告诉你什么时候单击表单上的按钮。


I know that VS will open an eventhandler stub by doubleclicking on an event. I found the underlying event declaration in InitializeComponent of the form on which the button is located.

this.buttonWorkOn.Click += new System.EventHandler(this.buttonWorkOn_Click);

Can I use this event declaration (of Visual Studio) and register another eventhandling method with it?

Upon instantiation of that other form its eventhandling method would need to register itself with the click event of the button on the main form.
I have no clue how to do that even though I have read quite a bit about delegates and events and in principle I do understand how it works.

Thank you

解决方案

If you right click on an event handler in the code editor and browse the definition you will find the way that it is declared, which you can then use in your own code.

For example, the declaration for a Button's Click event is:

    public event EventHandler Click;

You can add these yourself and use them from other places to respond to events in any class you create.

Here's a sample form with a single button (added via the designer) that when clicked will raise its own event:

public partial class Form1 : Form
{
    public event EventHandler ButtonClicked;

    private void RaiseButtonClicked()
    {
        if (ButtonClicked != null)
            ButtonClicked(this, EventArgs.Empty);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        RaiseButtonClicked();
    }
}

In another class you can then add a handler to that:

public class Responder
{
    public Responder(Form1 form)
    {
        form.ButtonClicked += OnButtonClicked;
    }

    private void OnButtonClicked(object sender, EventArgs args)
    {
        MessageBox.Show("Button was clicked");
    }
}

Now every instance of the Responder class will tell you when the button is clicked on the form.

这篇关于我在哪里可以在Visual Studio中找到事件声明[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆