调用一个eventhandler方法 [英] Call an method that is an eventhandler

查看:79
本文介绍了调用一个eventhandler方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hae,


我有一个带有ComboBox的窗体和其他东西。

在那个组合框上,我在de selectedindexchanged上有一个事件处理程序。 />

但是我的代码中的某个地方想要执行在事件处理程序中写入的相同代码



是否可以调用来自代码的eventhandler(不是来自表单上的真实

selectedindexchanged)。所以,我不必两次调用该代码。

tkx

jac

Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexchanged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexchanged on the form). So, I don''t have to call that code twice.
tkx
jac

推荐答案

只是为了确保我理解你...在你的代码的某个地方你已经得到了这样的东西:


这个。 comboBox1.SelectedIndexChanged + = new

System.EventHandler(this.comboBox1_SelectedIndexCh anged);


为SelectedIndexChanged被触发时设置一个EventHandler ...而你

好​​奇,如果你可以自己调用相当于comboBox1_SelectedIndexChanged

的东西?绝对!


需要注意的两件事情......首先,传递给

事件处理程序的第一个参数是对发件人的引用。如果你没有使用这个

引用,你可以简单地传入null,否则一定要传入

对相关ComboBox的引用。


第二...... EventArgs。一些处理程序使用它们,有些不管怎么做,如果你的话,你可以通过null或EventArgs.Empty传递



Brendan

" jac"写道:
Just to make sure I understand you... somewhere in your code youa??ve got
something to the effect of:

this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexCh anged);

setting up an EventHandler for when SelectedIndexChanged is fired... and you
are curious if you can call your equivalent of comboBox1_SelectedIndexChanged
on your own? Absolutely!

Two things to take note of... first up, the first argument passed into an
event handler is a reference to the sender... if you are not using this
reference you can simply pass in null, otherwise be sure to pass in a
reference to the ComboBox in question.

Second... EventArgs. Some handlers use them, some dona??t, if yours doesna??t
you can pass in null or EventArgs.Empty.

Brendan
"jac" wrote:
Hae,

我有一个带有ComboBox的窗体表格。
在那个组合框上我有一个关于de selectedindexchanged的事件处理程序。

但我的代码中的某个地方想要在事件处理程序中执行相同的代码。
从代码中调用eventhandler是否可行(而不是从代码中调用真正的
在表格上选择了索引。所以,我不必两次调用该代码。

tkx
jac
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexchanged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexchanged on the form). So, I don''t have to call that code twice.
tkx
jac



jac,


您可以像正常函数一样调用事件处理程序。


或者您是说要调用附加到<的所有事件处理程序br />
控制?如果是这样的话,你就不能这样做,因为只有物品才能发出自己的事件。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" jac" < ja*@discussions.microsoft.com>在留言中写道

news:84 ********************************** @ microsof t.com ...
jac,

You could just call the event handler like a normal function.

Or are you saying you want to call all the event handlers attached to
that control? If that is the case, you can''t do it, as only objects can
fire their own events.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"jac" <ja*@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
Hae,

我有一个带有ComboBox的窗体表格。
在那个组合框上我有一个关于de selectedindexchanged的事件处理程序。

但我的代码中的某个地方想要在事件处理程序中执行相同的代码。
从代码中调用eventhandler是否可行(而不是从代码中调用真正的
在表格上选择了索引。所以,我不必两次打电话给那个代码。

tkx
jac
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexchanged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexchanged on the form). So, I don''t have to call that code
twice.
tkx
jac



是的,只要你提供预期的参数(通常是
sender和EventArgs)就可以了。但是,将相关代码

放在一个单独的方法中并从事件处理程序中调用该方法

而不是将代码放在事件处理程序本身中会更清晰。这样,你就可以在代码中的任何地方调用该方法。

-

Kai Brinkmann [MSFT]


请不要直接向此别名发送电子邮件。此别名仅适用于

新闻组。

此帖子按原样提供。没有保证,也没有权利。


jac < ja*@discussions.microsoft.com>在留言中写道

news:84 ********************************** @ microsof t.com ...
Yes, it''s possible as long as you provide the expected parameters (usually
sender and EventArgs). However, it would be cleaner to put the relevant code
in a separate method and call that method from within the event handler
rather than putting the code in the event handler itself. That way, you
could simply call that method from anywhere in your code.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"jac" <ja*@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
Hae,

我有一个带有ComboBox的窗体表格。
在那个组合框上我有一个关于de selectedindexchanged的事件处理程序。

但我的代码中的某个地方想要在事件处理程序中执行相同的代码。
从代码中调用eventhandler是否可行(而不是从代码中调用真正的
在表格上选择了索引。所以,我不必两次打电话给那个代码。

tkx
jac
Hae,

I have a windows form with a ComboBox an other things.
On that combobox I have an eventhandler on de selectedindexchanged.

But somewhere in my code want to do excecute the same code that is writen
in the eventhandler.
Is it posible to call that eventhandler from the code (not from a real
selectedindexchanged on the form). So, I don''t have to call that code
twice.
tkx
jac



这篇关于调用一个eventhandler方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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