code in InitializeComponent vanishes. Very frustrating

D

David Cho

I've noticed that my code in InitializeComponent where I am linking
event handlers to events vanishes from time to time.

One of the things that triggers the disappearance is when I do something
wrong the the aspx page, causing an "Error Creating Control" error.
It's not always reproducible, but when I do that, some of the lines of
code in InitializeComponent vanish without warning.

Is this a "feature" from Microsoft? Is there a way to prevent that if
that is indeed a "feature"?

It is beyond frustrating because often it happens after my code has been
deployed and customers complain about buttons failing to respond. There
is no way I can audit every line of code in InitializeComponent to make
sure it has remained intact.
 
S

Steve C. Orr [MVP, MCSD]

When you create a form with Visual Studio.NET, did you ever notice the
following comment that is added before the InitializeComponent Sub?
'NOTE: The following procedure is required by the Form Designer

'Do not modify it using the code editor.



You are doing exactly what it tells you not to do and then you are
frustrated by the outcome.

I suggest you move your custom code into some other place such as Page_Init
or Page_Load.

To be fair, you aren't the only one that's been confused by this and that's
why Microsoft is moving all that "no-touch" stuff into separate files in
Visual Studio 2005, so people won't be so tempted to muck with it and then
end up frustrated like you.
 
D

David Cho

Wait, hold on here, Steve.

I have not modified anything by hand. After creating a button by
dragging and dropping it into the form, I click on the button which
opens the code editor taking you to the click event handler auto
generated by VS.NET.

I have not done anything by hand in InitializeComponent. VS.NET
generates all the code that links event handlers to events.

At times even

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

vanishes.

I am not understanding your point. Microsoft generates the event
handlers and links them to events, but the code vanishes because....?
 
S

Steve C. Orr [MVP, MCSD]

OK, so you didn't modify the code in there. Good.

Many of us have been frustrated by C#'s bad habit of deleting such event
handlers. I'ts one of the reasons I prefer VB.NET The only time I've seen
such a problem in VB.NET is when I cut a control and then paste it. The
event handler is no longer linked up. I believe this also happens in C#,
but I think it can happen in C# for other mysterious reasons too. Source
control can help somewhat.
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top