1

Using VS2013 Pro, WinForms, the debugger keeps deleting my event handlers. (I place them inside the Form.Designer.cs file)

I've only started noticing it recently and I'm positive I haven't changed any settings.

Can anyone else replicate this or is it just something on my system?

Steps to replicate:

  1. Create new project C# WinForms
  2. Add a split container > Inside panel two of the Base container, nest another split container.
  3. In the Form.Designer.Cs file, hook up a Click event handler to the nested panel 1 container.
  4. Run. -Click container - should work fine.
  5. Exit Debugger > Change color property of the nest panel 1.
  6. Run > Can no longer click?
  7. Inspect Code > Event Handler has been deleted?
9
  • 6
    Form.Designer.cs Is that the one that says "do not modify"? What happens if you put the code in Form.cs Commented Oct 12, 2014 at 11:39
  • Yeah I know, but I've always written code in there! Keeps it tidy. Commented Oct 12, 2014 at 11:40
  • 5
    @KidCode And you don't think heeding the warnings that the file shouldn't be manually modified would be a good idea? Commented Oct 12, 2014 at 11:40
  • 1
    Hashtag irony: the boilerplate stuff goes into the Xyz.Designer.cs files to keep the Xyz.cs files tidy. Commented Oct 12, 2014 at 11:41
  • 1
    I should have been clearer. I meant the top of the file doesn't carry the warning (like svcutil's "generated by a tool") but InitializeComponent does. Commented Oct 12, 2014 at 11:45

1 Answer 1

3

If you added your event handler manually then anytime you change something from the Designer the Form.Designer.cs is regenerated and will delete your event handlers (the ones added manually in the code)

What you need to do is to add the event handler from the Control properties.

For example if you code this in the Designer:

myControl.Click += myClickHandler; 

it will be deleted anytime you change something in the designer, because the whole .Designer.cs file is regenerated and for some reason the VS is not aware of the event handlers added manually.

You;ll see that if you add the event handler from the Control properties window (in the designer) the generated code will look like this:

myControl.Click += new EventHandler(myClickHandler);

Another workaround is to add the handler outside the .Designer.cs class, but the easy way is to add the handler from Designer :).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.