全局事件处理程序对用户帮助所有控件 [英] Global event handler for all controls for User Help

查看:122
本文介绍了全局事件处理程序对用户帮助所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是延长用C#编写Windows应用程序中提供帮助给用户(在集中控制的情况下),当他们打F1键。

I'm extending a windows application written in C# to provide help to the user (in the context of the focused control) when they hit the F1 key.

我想要做的是利用Control.HelpRequested事件,但我不知道如何扩展所有控件处理此事件。
http://msdn.microsoft .COM / EN-US /库/ system.windows.forms.control.helprequested.aspx

What I’d like to do is make use of the Control.HelpRequested event but I’m not sure how to extend all the controls to handle this event. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.helprequested.aspx

这不是真的可行更新每个控制的手来处理这个事件,我真的不喜欢通过所有控件的形式循环(如表格打开)事件处理程序相关联的想法。

It’s not really feasible to update each control "by hand" to handle this event and I really don’t like the idea of looping through all the controls in a form (as the form opens) to associate the event handler.

是否有一个整洁的方式来扩展形式的所有控件来处理特定的事件?

Is there a neat way to extend all controls of a form to handle a specific event?

这是刚做了,但我几乎觉得我应该可以写的东西像这样

This is just made up but i almost feel like i should be able to write something like this

[HandleEvent Control.HelpRequested, ApplyTo Typeof(Control)]
void MyEventHandler(object sender, EventArgs e)
{
// code to handle event...
}

任何建议或者对不同的做法想法大加赞赏 - 感谢

Any suggestions or perhaps ideas on a different approach are much appreciated - Thanks

推荐答案

这个例子(的 http://www.codeproject.com/KB/cs/ContextHelpMadeEasy.aspx )显示的WndProc F1键,然后如何捕获显示来自只有一个方法的帮助。

This example (http://www.codeproject.com/KB/cs/ContextHelpMadeEasy.aspx) shows how to trap the F1 key in WndProc and then show the help from one method only.

这一篇文章中这样做是为了实现一个接口暴露控件的ID,然后显示基于ID上下文帮助。 F1的处理程序,然后检查你的控制实现该接口,如果没有,那么它检查的控件的父级,直到找到接口的实现。

The idea in that article is to implement an interface exposing control's ID and then show context help based on that id. The F1 handler then checks if your control implements that interface, and if not, then it check's the control's parent until it finds an implementation of that interface.

但是,一个更简单方法(如果你不希望将ID添加到每个控件)是修改F1处理器基于静态类型的词典(例如字典),其中将包括主题ID为每一位支持控件显示上下文帮助。所以,当你需要一个话题指定控件关联,你会更新字典。

But an even simpler approach (if you don't want to add an ID to each control) is to modify the F1 handler to show context help based on a static type dictionary (e.g. Dictionary), which would contain Topic IDs for every supported control. So, whenever you need to associate a topic with a specified control, you would update the dictionary.

再次这将是明智通过添加更多抽象到这个方法一些供应商(委托或接口)的字典。例如,您可能需要额外的逻辑,基于控件的类型,名称,或其他一些属性来显示主题。

Again, it would be wiser to add more abstraction to this approach by adding some sort of a provider (delegate or interface) to that dictionary. For example, you might need additional logic to show topics based on control's type, name, or some other property.

这篇关于全局事件处理程序对用户帮助所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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