如何将一个事件称为另一个事件 [英] how can call one event into another event

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

问题描述

我有一个numericUpDown1_ValueChanged事件,我想从form1的按键事件中调用此事件.

该怎么办???
tnx

i have a numericUpDown1_ValueChanged event and i want to call this event from keypress''s event of form1

how can do this???
tnx

推荐答案

您不应调用事件处理程序.应通过用户操作来引发和处理事件.如果要在KeyPress事件处理程序中重用ValueChanged中的代码,请执行以下操作:

You should not call event handlers. Events should be raised and handled by user action. If you want to reuse the code in the ValueChanged in your KeyPress event handler, do something like this:

KeyPressEvent Start

// Some other processing
CommonMethod();
// Some other processing

End

ValueChangedEvent Start

// Some other processing
CommonMethod();
// Some other processing

End

CommonMethod Start
// Some processing
End



如果您还有其他意思,请更新您的问题和/或在回复中留下评论.



If you meant something else, update your question and/or leave comment to the reply.


为什么?
除非表单上的唯一控件是向上/向下控件,否则这会惹恼用户,除非它是毫无意义的,因为该控件将获得键盘按下信息,而不是表单...

为什么不处理NumericUpDown.KeyPress事件呢?
Why?
That would annoy the heck out of a user, unless the only control on the form was the up/down control - which would make it pointless since the control would get the keydown, not the form...

Why not handle the NumericUpDown.KeyPress event, instead?


没有这样的事件numericUpDown1_ValueChanged.事件为ValueChanged.您根本无法调用事件实例.可以调用事件处理程序.同样,触发事件仅允许在声明事件的同一类中进行,这是常规委托实例上事件的主要防呆限制之一.

因此,解决方法是:为服务器创建一个单独的(命名的,不是匿名的)方法,并为ValueChanged提供事件处理程序,将此方法添加到事件的调用列表中.您可能已经做到了;这是numericUpDown1_ValueChanged(将其重命名为语义名称;带有"_"的自动生成的名称违反了良好的Microsoft命名约定).现在,这是一种常规方法.从您的按键事件处理程序调用它.对于发件人,最好传递实例numericUpDown1,而不要传递"this"(可能不相关,具体取决于numericUpDown1_ValueChanged的作用.无论如何,参数的含义取决于此方法的作用.您甚至可以通过null来指示伪造"呼叫,并相应地在您的numericUpDown1_ValueChanged中对其进行处理.

—SA
There is no such event numericUpDown1_ValueChanged. The event is ValueChanged. You cannot call event instance at all. Event handler can be called. Also, firing event is only allowed from the same class where the event is declared, this is one of the main fool-proof limitation of events over regular delegate instances.

So, here is the resolution: create a separate (named, not anonymous) method to server as and event handler for ValueChanged, add this method to event''s invocation list. You probably already done it; this is numericUpDown1_ValueChanged (rename it to semantic name; auto-generated names with "_" violate good Microsoft naming conventions). Now, this is a regular method. Call it from your handler of key press event. For sender, better pass your instance numericUpDown1, not "this" (it may be irrelevant, depending what numericUpDown1_ValueChanged does. Anyway, what parameters mean depends on what this method does. You can even pass null to indicate "faked" call and process it in your numericUpDown1_ValueChanged accordingly.

—SA


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

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