如何在C#中处理表单事件 [英] How to handle form events in C#

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

问题描述

您好,


我绝对是C#的初学者,所以请原谅我以下问题

太容易了。


我有一个带一个按钮的表格。当我点击我想要的按钮时,
会显示一条消息,上面写着点击了按钮

但是,我不想处理该表格中的点击事件,

而不是我想要转移该事件发生在控制器上。

的课程显示该消息。


理念是这样的:

有一个类(称之为控制器)

控制器打开表格

当我点击按钮时,控制器被通知。通过单击按钮

的形式

控制器显示消息点击了按钮


现在,如何我能做到这一点吗?


谢谢你,理查德

Hello,

I am absolute beginner in C# so pardon me if the following question is
too easy.

I have a form with a one button. When I click the button I want to
display a message that reads "Button was clicked"
However, I do not want to handle the click event from that form,
instead I want to "transfer" that event to a "controller" class that
shows that message.

The ides is this:
Have a class (call it "controller")
Controller opens the form
When I click the button the controller is "notified" by the form that
the button was clicked
The controller displays the message "Button was clicked"

Now, how do I accomplish this?

Thank you, Richard

推荐答案

< me * *****@hotmail.com写信息

news:11 ******************** @ m37g2000prh.googlegrou ps.com。 ..
<me******@hotmail.comwrote in message
news:11********************@m37g2000prh.googlegrou ps.com...

我有一个带有一个按钮的表单。当我点击我想要的按钮时,
会显示一条消息,上面写着点击了按钮

但是,我不想处理该表格中的点击事件,

而不是我想要转移该事件发生在控制器上。

的课程显示该消息。


理念是这样的:

有一个类(称之为控制器)

控制器打开表格

当我点击按钮时,控制器被通知。通过单击按钮

的形式

控制器显示消息点击了按钮


现在,如何我能做到这一点吗?
I have a form with a one button. When I click the button I want to
display a message that reads "Button was clicked"
However, I do not want to handle the click event from that form,
instead I want to "transfer" that event to a "controller" class that
shows that message.

The ides is this:
Have a class (call it "controller")
Controller opens the form
When I click the button the controller is "notified" by the form that
the button was clicked
The controller displays the message "Button was clicked"

Now, how do I accomplish this?



实现此目的的一种方法是在表单中发布事件。当

控制器打开表单时,它会订阅表单的事件。在按钮的

点击事件中,您采取的唯一操作是触发您在表单中发布的

事件。以这种方式,它被中继。到

控制器。

One way to accomplish this is to publish an event in your form. When the
controller opens the form, it subscribes to the event of the form. In the
click event of the button, the only action that you take is to fire the
event that you published in your form. In this way, it gets "relayed" to the
controller.


Alberto,

感谢您的回复

正如我刚才所说,我用C#初学者,所以我

需要一些代码exmples

"发布事件","订阅事件"等等

您在表单中使用的代码是什么以及

是您在控制器类中使用的代码?

" Alberto Poblacion < ea ****************************** @ poblacion.orgwro te
消息新闻中的
: %2 **************** @ TK2MSFTNGP04.phx.gbl ...
Alberto,
Thank you for your reply
As I have mentioned I am a beginner with C# so I
need some code exmples.
"publish an event", "subscribe to the event" etc
what is the code that you use in your form and what
is the code that you use in your controller class?
"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP04.phx.gbl...

< me **** **@hotmail.com写信息

新闻:11 ******************** @ m37g2000prh.googlegrou ps.com ...
<me******@hotmail.comwrote in message
news:11********************@m37g2000prh.googlegrou ps.com...

>我有一个带有一个按钮的表单。当我点击按钮时我想显示一条消息,上面写着点击了按钮
但是,我不想处理该表单中的点击事件,而是我想要"传递"该事件发生在控制器上。

这个想法是这样的:
有一个班级(称之为控制器)
控制器打开表格
当我点击按钮时,控制器被通知。按下单击按钮的形式
控制器显示消息点击了按钮

现在,我该如何做到这一点?
>I have a form with a one button. When I click the button I want to
display a message that reads "Button was clicked"
However, I do not want to handle the click event from that form,
instead I want to "transfer" that event to a "controller" class that
shows that message.

The ides is this:
Have a class (call it "controller")
Controller opens the form
When I click the button the controller is "notified" by the form that
the button was clicked
The controller displays the message "Button was clicked"

Now, how do I accomplish this?



实现此目的的一种方法是在表单中发布事件。当

控制器打开表单时,它会订阅表单的事件。在

按钮的单击事件中,您采取的唯一操作是触发您在表单中发布的事件
。通过这种方式,它被转发给控制器b / b。


One way to accomplish this is to publish an event in your form. When
the controller opens the form, it subscribes to the event of the form. In
the click event of the button, the only action that you take is to fire
the event that you published in your form. In this way, it gets "relayed"
to the controller.



public类从

{

公共事件的EventHandler BtnClicked;


私人无效点击(对象o,eventrags E)

{

if(BtnClicked!= null)

BtnClicked(o,e);

}

}


公共类控制器

{


private void OpenFrom()

{

从FRM =新从();

frm.BtnClicked + =新的EventHandler(FromClickedHander);

Frm.shopw( );

}


private void FromClickedHander(object o,eventrags e)

{

.......

}


}

-

真诚

亚龙卡尔尼
http://dotnetbible.blogspot.com/

" Richard_AU"写道:
public class From
{
public event EventHandler BtnClicked;

private void Clicked(object o , eventrags e)
{
if(BtnClicked != null)
BtnClicked (o,e) ;
}
}

public class Controller
{

private void OpenFrom()
{
From frm = new From ();
frm.BtnClicked += new EventHandler(FromClickedHander);
Frm.shopw();
}

private void FromClickedHander(object o , eventrags e)
{
.....
}

}
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"Richard_AU" wrote:

Alberto,

感谢您的回复

正如我所说,我是初学者C#所以我

需要一些代码例外。

发布一个事件,订阅事件等等

您在表单中使用的代码是什么以及

是您在控制器类中使用的代码?


" Alberto Poblacion" < ea ****************************** @ poblacion.orgwro te
消息新闻中的
: %2 **************** @ TK2MSFTNGP04.phx.gbl ...
Alberto,
Thank you for your reply
As I have mentioned I am a beginner with C# so I
need some code exmples.
"publish an event", "subscribe to the event" etc
what is the code that you use in your form and what
is the code that you use in your controller class?
"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP04.phx.gbl...

< me **** **@hotmail.com写信息

新闻:11 ******************** @ m37g2000prh.googlegrou ps.com ...
<me******@hotmail.comwrote in message
news:11********************@m37g2000prh.googlegrou ps.com...

我有一个带有一个按钮的表单。当我点击我想要的按钮时,
会显示一条消息,上面写着点击了按钮

但是,我不想处理该表格中的点击事件,

而不是我想要转移该事件发生在控制器上。

的课程显示该消息。


理念是这样的:

有一个类(称之为控制器)

控制器打开表格

当我点击按钮时,控制器被通知。通过单击按钮

的形式

控制器显示消息点击了按钮


现在,如何我能做到这一点吗?
I have a form with a one button. When I click the button I want to
display a message that reads "Button was clicked"
However, I do not want to handle the click event from that form,
instead I want to "transfer" that event to a "controller" class that
shows that message.

The ides is this:
Have a class (call it "controller")
Controller opens the form
When I click the button the controller is "notified" by the form that
the button was clicked
The controller displays the message "Button was clicked"

Now, how do I accomplish this?



实现此目的的一种方法是在表单中发布事件。当

控制器打开表单时,它会订阅表单的事件。在

按钮的单击事件中,您采取的唯一操作是触发您在表单中发布的事件
。通过这种方式,它可以转发给控制器。


One way to accomplish this is to publish an event in your form. When
the controller opens the form, it subscribes to the event of the form. In
the click event of the button, the only action that you take is to fire
the event that you published in your form. In this way, it gets "relayed"
to the controller.






这篇关于如何在C#中处理表单事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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