Issues with adding/removing form load and closed events.

M

moondaddy

I'm running VS2005 Team Arch. and winfx beta 2. I have a c# project windows
application so I assume this is the right this is the correct forum rather
than a vista forum.

I added a windows form to the project and double clicked on it. It opened
the code window and added the Load event. I tried to find the form Closed
event from the methods dropdown list in the top right hand corner, but
didn't see it. I'm used to seeing such events in vs 2003 vb projects. so I
added it by hand like this:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{

}

Now I deleted the Form_Load event and built the project, but I got a compile
error saying it could not find the load event. this compile error was in
the Form1.designer.cs file:

this.Load += new System.EventHandler(this.Form1_Load);

with a blue squiggly under Form1_Load. (obviously this line should have been
cleaned out when I deleted the event)

Additionally, I noticed that there was no code in Form1.designer.cs for the
Form1_FormClosed event. (it wasnt auto generated) there should be a line
like this:

this.FormClosed += new
System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);

Review:
1) What's happening is double clicking on the form auto generates the form
load event. But when I go into Form1 Partial class and remove the load
event, its not being automatically removed from the form's hidden code
behind.

2) I don't see any way to auto generate the form closed event so no code is
being added to the form's hidden code behind, and there should be code there
such as:
this.FormClosed += new
System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);


I never experienced this type of behavior in VB and I'm new to c# as well as
working with the winfx beta products so I don't know if this is a bug, or if
c# works this way, or if something in winfx beta 2 corrupted vs 2005.

any help would be great.

Thanks.
 
D

dtb5571

Howdy moondaddy,

You should be able to go into the designer and click on the events
button (lightning bolt) in the properties for your form. Scroll
through the list, and double-click on any event you want to create a
handler for. If you have an event you want to remove, just delete it
out of the list.

In VS 2005, the auto-generated code is now put in a separate file so
you could go manually move the event from there, but that's the purpose
of the designer.

Dan
 
D

DBxGlock

Howdy moondaddy,

You should be able to go into the designer and click on the events
button (lightning bolt) in the properties for your form. Scroll
through the list, and double-click on any event you want to create a
handler for. If you have an event you want to remove, just delete it
out of the list.

In VS 2005, the auto-generated code is now put in a separate file so
you could go manually move the event from there, but that's the purpose
of the designer.

Dan
 
M

moondaddy

Thanks. I didnt know about the lightningbolt thing. I'm usd to selecting
form events in the code window and then having a list of all possible events
in a dropdown list. Maybe this is a but that MS should know about, but if I
remove the event in the partial class, or delete it from the lightning bolt
list, its still in the auto-generated code in Form1.Designer.cs file. Auto
generated code should also be auto removed code.
 
J

Jeffrey Tan[MSFT]

Hi Moondaddy,
Thanks for your post.

VC# project designer has different behavior with VB.net project designer.
In VB.net project, we add the event handler through the top-right dropdown
list, while in VC#, we always use the lightning bolt in propertybrowser to
add the event handler.(This is also true in VS.net2003)
Actually, when deleting event handler from lightning bolt, there is no way
for the designer to know if the developer wanted to delete the entire event
handler or not. For example, we have an event handler with a large code
block, if we remove this event handler in PropertyBrowser lightning bolt,
this large block is also removed. It is hard to say this is what you want,
maybe the code block is really precious and you want to attach to some
other event.

So VS.net IDE simply leaves event handler undeleted.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

DBxGlock

Jeffrey,

Your comment about not removing auto-generated event handlers when the
event is removed from the property browser is dead-on. Out of fear of
this happening, I almost never put real code in an auto-generated event
handler, but write my own event handler and call it from the
auto-generated event handler. I guess I'm just paranoid that the
auto-generated code may get deleted by the designer.

I am glad that the events handlers are not deleted though, even when
the control they were added for is deleted. Just yesterday, we
replaced our mainmenu in an application with a new toolstripmenu. It
was very nice that all our old event handlers were still there, and
still work with the new toolstripmenu! It certainly made it a lot
easier to just delete the old menu, put the new one on and attach the
click event to the appropriate pre-existing event handler via the
designer.

Dan
 
J

Jeffrey Tan[MSFT]

Hi Dan,

Thank you for sharing your experience with the community. I think your
reply is a good sample and demo for moondaddy :)

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top