如何声明事件 [英] How to declare an event

查看:70
本文介绍了如何声明事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

由于我不是一位经验丰富的程序员,所以我对如何在代码中声明事件存有疑问.

我有一个dataGridView,每次更改列中的值时,都必须激活一个方法.

我知道实现这一目标的方法如下:

Hi all,

as I am not an experienced programmer, I have a doubt about how to declare an event on my code.

I have a dataGridView that has to activate a method each time a value in a column is changed.

I know the method to make it happens is this one below:

private void dgrid_CellValueChanged(object sender, ataGridViewCellEventArgs e)<br />
{ ... }




但是,即使在我的班级中包含了此方法后,它也不起作用!

我还需要做其他事情吗?我是否需要在其他地方声明任何内容(我的意思是,在事件发生时需要调用此方法)?

如果是,该怎么办?在哪里做?

提前致谢! :)




But, even after including this method in my class it doesn''t work!

Do I have to do something else? Do I have to declare anything somewhere else ( I mean, something to call this method when the event happens )?

If yes, how do I do it and where do I do it?

Thanks in advance! :)

推荐答案

也许这就是您所需要的.将其放在表单Load事件上.

Maybe this is what you need. Put it on the form Load event.

this.dgrid.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgrid_CellValueChanged);


是的,您必须订阅该事件:
Yes, you have to subscribe to that event: subscribe to the event[^]

In your form''s constructor, you could add,
dgrid.CellValueChanged += new DataGridViewCellEventHandler(dgrid_CellValueChanged);



另外,如果您使用的是Visual Studio,则在窗体/窗口的设计"视图中,转到属性"选项卡,切换到事件"(而不是属性),然后双击所需的事件.并且所有事件的自动整理都将自动为您完成.



Also, if you''re using Visual Studio, in the Design View of your form/window, go to the Properties tab, switch to "Events" (instead of properties) and double click on the event you need. And all the wiring up of events will automatically be done for you.


这篇关于如何声明事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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