1

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.

esskar
  • 10,638
  • 3
  • 36
  • 57
  • In other words, you want to instantiate a class derived from a Form, but prevent its constructor from attaching event handlers to its controls? – vgru Sep 07 '11 at 16:08
  • See http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control – stuartd Sep 07 '11 at 16:09
  • seems not to work for me, it does not remove the original behaviors: like preventing a combobox from droping down its items and so on ... – esskar Sep 08 '11 at 09:41

2 Answers2

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Wow! I had no clue that's how the Designer worked. I'm not judging M$ for the design decisions they have made but that just feels like the Forms Designer was cobbled together with duct-tape and toilet paper. My developer world is a little less shiny now. :( – RLH Sep 07 '11 at 17:15
  • i actually had the transparent control in mind! thanks for the answer! – esskar Sep 07 '11 at 20:23
0

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.

Arun
  • 2,493
  • 15
  • 12