用于User Help的所有控件的全局事件处理程序 [英] Global event handler for all controls for User Help

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

问题描述

HI,我扩展了一个用C#编写的Windows应用程序,以便在按下F1键时向用户(在集中控制的上下文中)提供帮助。



我想做的是使用Control.HelpRequested事件,但我不知道如何扩展所有的控件来处理这个事件。
http:// msdn。 microsoft.com/en-us/library/system.windows.forms.control.helprequested.aspx



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



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



这只是弥补,但我几乎觉得我应该能够写这样的事情

 [HandleEvent Control.HelpRequested,ApplyTo Typeof(Control)] 
void MyEventHandler(object sender,EventArgs e)
{
//代码来处理事件...
}

任何对不同方法的建议或者想法都非常感激 - 谢谢

解决方案

此示例( http://www.codeproject.com/KB/ cs / ContextHelpMadeEasy.aspx )显示了如何在WndProc中捕获F1键,然后从一个方法显示帮助。



该文章中的想法是实现一个暴露控件ID的界面,然后根据该id显示上下文帮助。然后,F1处理程序检查您的控件是否实现该接口,如果不是,则它检查控件的父级,直到找到该接口的实现。



但是更简单方法(如果您不想为每个控件添加一个ID)是修改F1处理程序以显示基于静态类型字典(例如Dictionary)的上下文帮助,其中将包含每个支持的控件的主题ID。所以,只要您需要将主题与指定的控件相关联,就可以更新字典。



再次,通过添加更多的抽象来增加更多的抽象度某种类型的提供者(委托或接口)到该字典。例如,您可能需要其他逻辑来根据控件的类型,名称或某些其他属性显示主题。


HI, 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.

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

解决方案

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.

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.

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.

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

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