如何以编程方式调用Button的Click事件? [英] How to programmatically invoke Click event of the Button ?
问题描述
我需要以编程方式从其他类调用
单击表单上按钮的事件。
Button.OnClick方法受保护,不公开。
如何执行此操作?
Oleg Subachev
Dnia 23 -03-2006 o 17:07:41 Oleg Subachev< ol ** @ urvb.ru> napisa3:
我需要以编程方式从其他类调用我的表单上按钮的Click事件。
Button.OnClick方法受保护,不公开。
如何执行此操作?
[PD]这样的事情应该有效:
typeof(Button).InvokeMember(" OnClick",BindingFlags .NonPublic | BindingFlags.InvokeMethod,null,
myButton,new object [] {EventArgs.Empty});
我写的没有实际编译这段代码,但你应该得到
的想法。
-
Piotr Dobrowolski
Piotr.Dobrowolski@_usun_gmail.com
2006年3月23日星期四21:07:41 +0500 ,Oleg Subachev写道:
我需要以编程方式从其他类调用
我的表单上按钮的Click事件。
Button.OnClick方法受到保护,而不是公共
您可以使用PerformClick()函数。但是我的
意见在单独的函数中移动按钮点击后面的逻辑要好得多
将在按钮的点击事件中调用处理程序。然后你可以从你想要的任何地方调用这个函数。
theButton.PerformClick();
显然,由于theButton是私有的,你需要用公共方法包装
Marc
>
" Oleg Subachev" <醇** @ urvb.ru>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...我需要以编程方式从其他类调用
单击我的表单上的按钮事件。
Button.OnClick方法受到保护,而不是公开。
如何执行此操作?
Oleg Subachev
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.
How to perform this ?
Oleg Subachev解决方案Dnia 23-03-2006 o 17:07:41 Oleg Subachev <ol**@urvb.ru> napisa3:
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.
How to perform this ?
[PD] Something like this should work:
typeof(Button).InvokeMember("OnClick",BindingFlags .NonPublic|BindingFlags.InvokeMethod,null,
myButton, new object[]{EventArgs.Empty});
I''m writing without actually compiling this code but you should get the
idea.
--
Piotr Dobrowolski
Piotr.Dobrowolski@_usun_gmail.com
On Thu, 23 Mar 2006 21:07:41 +0500, Oleg Subachev wrote:
I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.
You can use the PerformClick() function. But it''s a lot better in my
opinion to move the logic behind the button click in a separate function
that would be called in the button''s click event handler. You could then
call this function from anywhere else you want to.
theButton.PerformClick();
Obviously, since theButton is proabbly private, you probalby need to wrap
this in a public method
Marc
"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...I need to programmatically invoke from other class
Click event of the Button on my Form.
Button.OnClick method is protected, not public.
How to perform this ?
Oleg Subachev
这篇关于如何以编程方式调用Button的Click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!