我不能围绕事件 [英] can't wrap my mind around events

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

问题描述

我对事件的一般问题似乎是他们有很多部分

,我不知道他们去哪里或何时使用哪些部件。

例如,有委托,事件名称,事件处理程序,

然后是将它连接在一起的编码。在大多数情况下,我会理解这些东西,但是当我把它们放在一起时我感到很困惑




更具体地说,这里是一本书中的一些代码:


this.txtMonthlyInvestment.TextChanged + = new

System.EventHandler(this.txtMonthlyInvestment_Text改变了);


首先,您需要''this''关键字吗?第二,这段代码在哪里?
去了?在包含文本框的表单的Load事件中?

另一个例子:


ProductList products = new ProductList(); //一个数组列表对象


products.Changed + = new EventHandler(ChangedHandler);


为什么不是'这个' '在第二个例子中使用过?这本书是否与b $ b不一致,还是不同的情况? (在书中,它说是'

从另一个班级调用它,但我不知道这是什么意思

确切地说:处理来自另一个类的事件,你创建一个实例

类,它引发事件并将其分配给一个类变量。)


哪里有那第一行去了吗? (产品声明)。以上

两行是整个例子(除了事件处理程序本身),

所以我觉得有些东西被遗漏了。它没有显示它包含在哪个类别中。

基本上我只是非常迷失在事件中,我总是这样。

我不明白每个''部分'的所有代码都在哪里,而且'

可能是它归结为什么,我猜。


任何帮助将不胜感激,或任何建议明确

解释事件。


谢谢,

John

My general problem with events seems to be that there are so many parts
to them, and I''m not sure where they all go or when you use which parts.
For example, there''s the delegate, the event name, the event handler,
then the coding that wires it together. For the most part, I do
understand these things, but I get confused when it comes to putting it
all together.

More specifically, here''s some code from a book:

this.txtMonthlyInvestment.TextChanged += new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

First off, do you need the ''this'' keyword? Second, where does this code
go? In the Load event of the form that contains the text box?

Another example:

ProductList products = new ProductList(); //an array list object

products.Changed += new EventHandler(ChangedHandler);

Why isn''t ''this'' used in the second example? Is the book being
inconsistent, or is it a different situation? (In the book, it says it''s
calling it from a different class, but I don''t know what that means
exactly: "To handle an event from another class, you create an instance
of the class that raises the event and assign it to a class variable.")

Where does that first line go? (The declaration of products). The above
two lines are the entire example (except for the event handler itself),
so I think some stuff has been left out. It doesn''t show which class
it''s included in.
Basically I''m just very lost when it comes to events, and I always have.
I don''t understand where all the code goes for each ''part'', and that''s
probably what it all comes down to, I guess.

Any help would be appreciated, or any suggestions for sites that clearly
explain events.

Thanks,
John

推荐答案

John Salerno写道:
John Salerno wrote:
更具体地说,这里''一本书中的一些代码:

this.txtMonthlyInvestment.TextChanged + = new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

首先,你需要吗? ''this''关键字?第二,这段代码在哪里?在包含文本框的表单的Load事件中?


这总是指当前的类实例。在你的情况下,因为

它正在访问文本框,''this''可能是指当前的表格。

可能没有必要使用''this' 。代码的作者可能会使用它来强调txtMonthlyInvestment是

当前表单的一部分。至于它的位置,它通常放在表单或load事件的

构造函数中。真正重要的是

是在您需要捕获

事件之前执行的代码行。

另一个例子:

ProductList products = new ProductList(); //数组列表对象

products.Changed + = new EventHandler(ChangedHandler);

为什么在第二个例子中没有使用它?这本书是不一致的,还是一个不同的情况? (在书中,它说它是


它可能不一致,但这里可能不需要''这个'。它只是

取决于代码出现的位置。

从另一个类调用它,但我不知道这意味着什么呢?"要处理来自另一个类的事件,你创建引发事件并将其分配给类变量的类的实例。)

第一行在哪里? (产品声明)。上面的两行是整个例子(除了事件处理程序本身),
所以我认为有些东西被遗漏了。它没有显示它包含在哪个类中。
More specifically, here''s some code from a book:

this.txtMonthlyInvestment.TextChanged += new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

First off, do you need the ''this'' keyword? Second, where does this code
go? In the Load event of the form that contains the text box?

this always refers to the current class instance. In your case, since
it is accessing text boxes, ''this'' probably refers to the current form.
It may not be necessary to use ''this''. The author of the code may
have used it to stress that the txtMonthlyInvestment is part of the
current form. As to where it goes, it is normally placed in the
constructor of the form or in the load event. All that really matters
is that the line of code is executed before you need to catch the
events.
Another example:

ProductList products = new ProductList(); //an array list object

products.Changed += new EventHandler(ChangedHandler);
Why isn''t ''this'' used in the second example? Is the book being
inconsistent, or is it a different situation? (In the book, it says it''s
It might be inconsistent, but ''this'' may not be required here. It just
depends on where the code appears.
calling it from a different class, but I don''t know what that means
exactly: "To handle an event from another class, you create an instance
of the class that raises the event and assign it to a class variable.")

Where does that first line go? (The declaration of products). The above
two lines are the entire example (except for the event handler itself),
so I think some stuff has been left out. It doesn''t show which class
it''s included in.




产品声明可以在任何地方。它可能位于表单类的顶部

。它可以是方法中的私有变量,也可以是作为用户定义类的属性的
。也许作者是

打算将它放在表格类的顶部。

我希望这有点帮助。我敢肯定,一旦你仔细观察事件,就会变得更加容易。



The declaration of Products could be anywhere. It could be at the top
of your form class. It could be a private variable inside a method or
as a property to a user defined class. Perhaps the author was
intending that it be placed in the top of the form class.
I hope this helped a little. I''m sure, once you look at events more
closely, that it will become easier for you.


John Salerno< jo ****** @ NOSPAMgmail.com>写道:
John Salerno <jo******@NOSPAMgmail.com> wrote:
我对事件的一般问题似乎是它们有很多部分,我不确定它们的去向或使用哪些部分。<例如,有委托,事件名称,事件处理程序,然后是将它连接在一起的编码。在大多数情况下,我会理解这些事情,但是当把它放在一起时我会感到困惑。

更具体地说,这里有一些代码来自一本书:

this.txtMonthlyInvestment.TextChanged + = new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

首先,你需要''这个''关键字?


Nope。

其次,这个代码在哪里?在包含文本框的窗体的Load事件中?


嗯,你可以把它放在任何你想要的地方 - 但很明显,事件不会发生,你需要给你的经理人开火,直到你把它连接起来为止。

另一个例子:

ProductList products = new ProductList(); //数组列表对象

products.Changed + = new EventHandler(ChangedHandler);

为什么在第二个例子中没有使用它?这本书是不一致的,还是一个不同的情况? (在书中,它说它是从另一个班级调用它,但我不知道这意味着什么......>要处理来自另一个班级的事件,你要创建提升事件并将其分配给类变量的类的实例。


这是错的。你不需要为一个实例提供一个带有

引用的成员变量来订阅一个事件 - 你只需要

需要长时间参考足以订阅。

第一行去哪儿了? (产品声明)。上面的两行是整个例子(除了事件处理程序本身),
所以我认为有些东西被遗漏了。它没有显示它包含在哪个类中。

基本上我只是在事件发生时非常迷失,而且我总是这样。
我不知道所有代码在每个''部分'的所有代码都在哪里,而且这可能是我所想要的。

任何帮助都会非常感谢,或者对明确解释事件的网站提出任何建议。
My general problem with events seems to be that there are so many parts
to them, and I''m not sure where they all go or when you use which parts.
For example, there''s the delegate, the event name, the event handler,
then the coding that wires it together. For the most part, I do
understand these things, but I get confused when it comes to putting it
all together.

More specifically, here''s some code from a book:

this.txtMonthlyInvestment.TextChanged += new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

First off, do you need the ''this'' keyword?
Nope.
Second, where does this code
go? In the Load event of the form that contains the text box?
Well, you can put it wherever you want - but obviously the event won''t
fire your handler until you''ve hooked it up.
Another example:

ProductList products = new ProductList(); //an array list object

products.Changed += new EventHandler(ChangedHandler);

Why isn''t ''this'' used in the second example? Is the book being
inconsistent, or is it a different situation? (In the book, it says it''s
calling it from a different class, but I don''t know what that means
exactly: "To handle an event from another class, you create an instance
of the class that raises the event and assign it to a class variable.")
That''s just wrong. You don''t need to have a member variable with a
reference to an instance in order to subscribe to an event - you just
need to have a reference for long enough to subscribe.
Where does that first line go? (The declaration of products). The above
two lines are the entire example (except for the event handler itself),
so I think some stuff has been left out. It doesn''t show which class
it''s included in.

Basically I''m just very lost when it comes to events, and I always have.
I don''t understand where all the code goes for each ''part'', and that''s
probably what it all comes down to, I guess.

Any help would be appreciated, or any suggestions for sites that clearly
explain events.




对他们来说没什么特别神奇的 - 那可能是

令你困惑的是什么。试想一下+ =称为添加方法

和 - ="称为删除方法,并将委托视为

处理程序列表。关于代表的唯一奇怪的事情是C#处理

+ =和 - =(对于代表本身,而不是事件),这是

实际调用静态方法是如何添加到null ...


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该团体,请不要给我发邮件



There''s nothing particularly magical about them - that''s possibly
what''s confusing you. Just think of the "+=" as calling an "add" method
and "-=" as calling a "remove" method, and think of a delegate as a
list of handlers. The only odd thing about delegates are C#''s handling
of += and -= (for the delegates themselves, not events), which are
actually calls to static methods which is how you can add to null...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


InLine评论:


" John萨莱诺" <乔****** @ NOSPAMgmail.com>在留言中写道

新闻:Ao ******************** @ rcn.net ...
InLine Comments:

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:Ao********************@rcn.net...
我的事件的一般问题似乎是它们有很多部分,我不确定它们的去向或使用哪些部分。对于
示例,有委托,事件名称,事件处理程序,然后是将它连接在一起的
编码。在大多数情况下,我确实理解这些事情,但是当把它们放在一起时我感到很困惑。

更具体地说,这里有一些书中的代码:

this.txtMonthlyInvestment.TextChanged + = new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

首先,你需要''this''关键字吗? ?第二,这段代码在哪里?在包含文本框的窗体的Load事件中?
你不需要这个关键词。这只是意味着使用这个类方法

命名为txtMonthlyInvestment_TextChanged。没有它,如果在基础

类上有一个名为''txtMonthlyInvestment_TextChanged'的公共

方法,并且在派生类上没有,它将使用基类''方法

(如果它有相同的签名)。


事件在Page_Load之后被触发但事件得到附加之前

Page_Load。所以,为了确保事件触发,你会把它放在

InitializeComponent(我相信这是正确的方法,可能是

Page_Init ...不能记得)。

另一个例子:

ProductList products = new ProductList(); //数组列表对象

products.Changed + = new EventHandler(ChangedHandler);

为什么在第二个例子中没有使用它?这本书是不一致的,还是一个不同的情况? (在书中,它说它是从另一个班级调用它,但我不知道这意味着什么......>要处理来自另一个班级的事件,你要创建
引发事件并将其分配给类变量的类的实例。

第一行在哪里? (产品声明)。以上两行是整个例子(除了事件处理程序本身),所以
我认为有些东西被遗漏了。它没有显示它包含在哪个类中。
My general problem with events seems to be that there are so many parts to
them, and I''m not sure where they all go or when you use which parts. For
example, there''s the delegate, the event name, the event handler, then the
coding that wires it together. For the most part, I do understand these
things, but I get confused when it comes to putting it all together.

More specifically, here''s some code from a book:

this.txtMonthlyInvestment.TextChanged += new
System.EventHandler(this.txtMonthlyInvestment_Text Changed);

First off, do you need the ''this'' keyword? Second, where does this code
go? In the Load event of the form that contains the text box? You do not need the "this" keyword. This just means use this classes method
named txtMonthlyInvestment_TextChanged. Without it, if there was a public
method with the same name ''txtMonthlyInvestment_TextChanged'' on the base
class, and none on the derived class, it would use the base class'' method
(if it had the same signature).

The event gets fired after Page_Load but the events get "attached" prior to
Page_Load. So, to make sure the event fires, you would place it in
InitializeComponent (I believe that is the correct method, may be
Page_Init...can''t remember).

Another example:

ProductList products = new ProductList(); //an array list object

products.Changed += new EventHandler(ChangedHandler);

Why isn''t ''this'' used in the second example? Is the book being
inconsistent, or is it a different situation? (In the book, it says it''s
calling it from a different class, but I don''t know what that means
exactly: "To handle an event from another class, you create an instance of
the class that raises the event and assign it to a class variable.")

Where does that first line go? (The declaration of products). The above
two lines are the entire example (except for the event handler itself), so
I think some stuff has been left out. It doesn''t show which class it''s
included in.



再一次,''this''关键字不是必需的。你有A班和班级

B. A级暴露一个事件,事件E.在B级,你想要抓住事件

E,当A级提出它时。因此,您将创建一个B类实例,并将其分配给类型为B的变量。然后,您将在类型为B的变量上为事件E添加事件处理程序

。 ?理解:)\


HTH至少有点;)


Mythran


Once again, the ''this'' keyword is not required. You have class A and class
B. Class A exposes an event, Event E. In class B, you want to catch Event
E for when class A raises it. So, you would create a class B instance and
assign it to a variable of type "B". Then you would add the event handler
for Event E on the variable of type "B". ? Understand :)\

HTH a little at least ;)

Mythran


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

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