EventHandling问题:Java有解决方案,但微软呢? [英] EventHandling problem : Java has solution but what about Microsoft???

查看:58
本文介绍了EventHandling问题:Java有解决方案,但微软呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在一些控件中添加了一些事件,示例代码是:

btnControl.GotFocus + = new EventHandler(EventHandlingMethod);

btnControl.Click + = new EventHandler(EventHandlingMethod);

lblControl.Click + = new EventHandler(EventHandlingMethod);


private void EventHandlingMethod (对象发送者,EventArgs e)

{

.......

}


btnControl = Button对象,lblControl = Lable对象


现在的问题是:我在btnControl和lblControl的所有事件中都添加了相同的事件处理程序方法。
<现在在EventHandlingMethod方法中,我可以找到哪个控件正在使用发送器,但现在如果我想在事件名称的方法中添加一些切换案例,那么我在eventargs或任何其他方式中都没有任何此类信息。 ...


有人可以帮助/指导我获取活动名称吗?


有可能吗?如果是的话怎么样?如果没有,那么任何解决方法吗?


提前致谢


问候,

Mahesh Devjibhai Dhola

Hi,
I have added few of the events in some control, example code is:
btnControl.GotFocus +=new EventHandler(EventHandlingMethod);
btnControl.Click +=new EventHandler(EventHandlingMethod);
lblControl.Click +=new EventHandler(EventHandlingMethod);

private void EventHandlingMethod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.

now in EventHandlingMethod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....

Can someone help/guide me to get the event name?

Is it possible? if yes then how? If no, then any work around?

Thanks in Advance

Regards,
Mahesh Devjibhai Dhola

推荐答案

(一些新闻组被修剪 - 这真的不适合所有

列出的组) br />

Mahesh Devjibhai Dhola [MVP]写道:
(some newsgroups trimmed - this really wasn''t appropriate for all the
listed groups)

Mahesh Devjibhai Dhola [MVP] wrote:

我在一些控件中添加了一些事件,示例代码是:
btnControl.GotFocus + = new EventHandler(EventHandlingMethod);
btnControl.Click + = new EventHandler(EventHandlingMethod);
lblControl.Click + = new EventHandler(EventHandlingMethod);

private void EventHandlingMethod(object sender,EventArgs e)
{
.......
}
btnControl = Button对象,lblControl = Lable对象

现在的问题是:我在btnControl和lblControl的所有事件中都添加了相同的事件处理程序方法。

现在在EventHandlingMethod方法中,我可以找到哪个控件正在使用发送器,但现在如果我想在事件名称的方法中放置一些开关案例,那么我在事件中没有任何此类信息或任何其他方式....

有人可以帮助/引导我获取活动名称?

有可能吗?如果是的话怎么样?如果没有,那么任何解决方法?
Hi,
I have added few of the events in some control, example code is:
btnControl.GotFocus +=new EventHandler(EventHandlingMethod);
btnControl.Click +=new EventHandler(EventHandlingMethod);
lblControl.Click +=new EventHandler(EventHandlingMethod);

private void EventHandlingMethod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.

now in EventHandlingMethod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....

Can someone help/guide me to get the event name?

Is it possible? if yes then how? If no, then any work around?




你的设计很糟糕。方法应该做一件事。如果你真的想要在GotFocus和Click上发生同样的事情,那就好了。如果你想要*
*不同的*事情发生,使用*不同的*程序。如果有一些常见的和一些不同的功能,请将常见的

功能放在一个从两个事件处理程序调用的单独过程中。


您不应该将事件处理程序视为Windows

消息循环的替代品,它必须处理可能发生的所有事情。

GotFocus事件处理程序应该做GotFocus需要的东西;

Click事件处理程序应该执行Click所需的操作;等等。


-

Larry Lard

回复团体请



Your design is bad. Methods should do one thing. If you actually want
the same thing to happen on GotFocus and on Click, fine. If you want
*different* things to happen, use *different* procedures. If there is
some common and some different functionality, put the common
functionality in a separate procedure called from both event handlers.

You shouldn''t treat event handlers like a substitute for a Windows
message loop, that has to handle everything that can possible happen.
The GotFocus event handler should do what is required on GotFocus; the
Click event handler should do what is required on Click; and so on.

--
Larry Lard
Replies to group please


" Mahesh Devjibhai Dhola [MVP]" < DH ********* @ hotmail.com> schrieb:

我在一些控件中添加了一些事件,示例代码是:

btnControl.GotFocus + = new EventHandler(EventHandlingMethod);

btnControl.Click + = new EventHandler(EventHandlingMethod);

lblControl.Click + = new EventHandler(EventHandlingMethod);


private void EventHandlingMethod(object sender ,EventArgs e)

{

.......

}


btnControl = Button对象,lblControl = Lable对象


现在问题是:我在所有事件中添加相同的事件处理程序方法

也用于btnControl和lblControl。


现在在EventHandlingMethod方法中,我可以找到哪个控件正在使用发件人

但现在如果我想在事件名称的方法中加入一些开关案例那么

我在eventargs或任何其他方式都没有任何此类信息....

=======


我很好奇处理不同控件的不同事件在

中,相同的事件处理程序会产生。相反,为所有事件类型添加单独的事件处理程序,而不仅仅是委托类型。这意味着单独的处理程序用于

控制''''点击''和''GotFocus''事件。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http:// classicvb。 org / petition />

"Mahesh Devjibhai Dhola [MVP]" <dh*********@hotmail.com> schrieb:
I have added few of the events in some control, example code is:
btnControl.GotFocus +=new EventHandler(EventHandlingMethod);
btnControl.Click +=new EventHandler(EventHandlingMethod);
lblControl.Click +=new EventHandler(EventHandlingMethod);

private void EventHandlingMethod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events
for btnControl and lblControl too.

now in EventHandlingMethod method, i can find which control is using sender
but now if i want to put some switch cases in the method for Event Name then
I have no any such information in the eventargs or anyother way....
=======

I am curious which sense handling different events of different controls in
the same event handler would make. Instead, add separate event handlers for
all event types, not just delegate types. This means separate handlers for
controls'' ''Click'' and ''GotFocus'' events.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


如果你需要根据事件做不同的事情,为什么不拥有不同的事件处理程序?你可以将公共代码放入一个两个事件处理程序都可以调用的方法中。


我明白你在说什么,但这看起来并不是什么大不了的事。它很容易解决,大多数时候它甚至都不是问题,因为无论如何你要么在处理程序中需要不同的代码,要么事件有不同的签名等。

" Mahesh Devjibhai Dhola [MVP]" < DH ********* @ hotmail.com>在留言新闻中写道:uL ************** @ TK2MSFTNGP14.phx.gbl ...



我添加了一些控件中的事件很少,示例代码是:

btnControl.GotFocus + = new EventHandler(EventHandlingMethod);

btnControl.Click + = new EventHandler(EventHandlingMethod);

lblControl.Click + = new EventHandler(EventHandlingMethod);


private void EventHandlingMethod(object sender,EventArgs e)

{

.......

}


btnControl =按钮对象,lblControl = Lable对象


现在的问题是:我在btnControl和lblControl的所有事件中都添加了相同的事件处理程序方法。


现在在EventHandlingMethod方法中,我可以找到哪个控制是使用发送者,但现在如果我想在事件名称的方法中放置一些开关案例,那么我在eventargs或任何其他方式中没有任何此类信息....


可以有人帮助/指导我获取活动名称吗?


有可能吗?如果是的话怎么样?如果没有,那么任何解决方法吗?


提前致谢


问候,

Mahesh Devjibhai Dhola

If you need to do different things depending on the event, why not have different event handlers? You can place common code into a method that both event handlers can call.

I understand what you are saying, but it just doesn''t seem like a big deal. It is very easy to work around, and most of the time it is not even an issue because you either need different code in the handler anyway, or the events have different signatures, etc.
"Mahesh Devjibhai Dhola [MVP]" <dh*********@hotmail.com> wrote in message news:uL**************@TK2MSFTNGP14.phx.gbl...
Hi,
I have added few of the events in some control, example code is:
btnControl.GotFocus +=new EventHandler(EventHandlingMethod);
btnControl.Click +=new EventHandler(EventHandlingMethod);
lblControl.Click +=new EventHandler(EventHandlingMethod);

private void EventHandlingMethod(object sender, EventArgs e)
{
.......
}

btnControl = Button object, lblControl = Lable object

Now the problem is: I am adding the same event handler method in all events for btnControl and lblControl too.

now in EventHandlingMethod method, i can find which control is using sender but now if i want to put some switch cases in the method for Event Name then I have no any such information in the eventargs or anyother way....

Can someone help/guide me to get the event name?

Is it possible? if yes then how? If no, then any work around?

Thanks in Advance

Regards,
Mahesh Devjibhai Dhola


这篇关于EventHandling问题:Java有解决方案,但微软呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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