Remove original event behaviour of WinForm Control

心已入冬 提交于 2019-12-11 06:17:13

问题


I would like to remove the original event behavior of controls within a form (similar to design mode). So, when the user clicks on the button, i only want to capture that event. I do not want the original button event to be fired. Is this somehow possible? I am looking for a generic solution. So it should work with any form and any control within the form.

Reason: I wrote a form validation rules designer. It uses reflection to enumerate all form-types in the entry assembly. The user can then select a form type, the designer creates that form, enumerates the controls, and embedds the form in the designer panel. clicking on a control, opens a formular designer panel, and the user can now create a formular for that control and saves the formular to a DB. When the form is then opened in the normal "runtime" mode, it loads its validation formulars.


回答1:


Events are not in fact disabled in the Winforms designer. The designer executes the constructor of the form through Reflection, everything in the InitializeComponent() method executes, including the event subscriptions. Wherever this might cause a problem, the controls check the DesignMode property (prevents a Timer from starting for example) or by custom designers. The form is displayed underneath a transparent layered window on top of which the selection rectangle and drag handles are painted. Which prevents issues with mouse clicks and keyboard focus.

You probably ought to look at this magazine article to get this working for you.




回答2:


From what I understand from your question, I guess, you can still use the "DesignMode" property for this as well. In your event handling routine, you may want to bypass execution by checking on this property:

        if (this.DesignMode) return;

as the first statement in your event handling block of code.



来源:https://stackoverflow.com/questions/7336999/remove-original-event-behaviour-of-winform-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!