Sam Pearson

I don't have a File --> Exit menu or anything, just the 'x' in the top right corner. When I try to manually add a FormClosing event, it gets removed whenever I modify the GUI. Where can I add it so that it won't go away



Re: Visual C# General How do I add FormClosing event?

boban.s

It's common thing to lose event handlers connection with events, if you cut and paste controls on the form instead of moving them, but to lose form event handler connection, I didn't see yet. I'm also very confused with your 'manually' word, and have no idea what you mean by that. Can you explain the procedure of attaching handler method to FormClosing event and what you want to do on that event.






Re: Visual C# General How do I add FormClosing event?

Sam Pearson

I'm trying to modify InitializeComponent(). Here's what I'm adding:

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(HMI_FormClosing);

I've created the HMI_FormClosing method already. This works fine, until I change things on the GUI with the Visual Studio Designer. Then the above line of code disappears. How can I prevent that from happening





Re: Visual C# General How do I add FormClosing event?

boban.s

That is very strange behavior.
You can try adding event handler from design like this: Open form in design view, open properties window or press F4, click event toolbar button to view events on Form object, find FormClosing event in Behavior group, and double click it.
Also try the same thing on new form and also on new form in empty project.
If you still have this problem then that is surelly problem of your VS installation.






Re: Visual C# General How do I add FormClosing event?

Sam Pearson

Aha!

I was using

private void HMI_FormClosing(object sender, EventArgs e)

rather than

private void HMI_FormClosing(object sender, FormClosingEventArgs e)

Thank you very much for your help!! Smile