I believe VS.NET is removing my code!!!!!!

  • Thread starter Thread starter xiko tripa
  • Start date Start date
X

xiko tripa

I believe VS.NET is removing my code. I have a button control on a
web form.
The button control raises an event, and my web form handles the event.

The code that I add to the web form to handle the user control's event
is shown below.

Sometimes VS.NET has nuked my code, clipping the line code where the
handler event is. When it hasn't, the code works just like I want it
to.

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
//the line below disappear occasionally so I have to retype it.
this.SearchButton.Click =
System.EventHandler(this.SearchButton_Click);
}

Does anyone knows some fix for this missbehavior?

VS.net 2003
framework 1.1
Windows XP Pro


Thanks in advance.
 
I believe this is a known bug. I recommend you save regularly and use some
sort of source control.
 
I had a similary problem with 3rd party controls. With win controls, it
kept nuking my dialogresult setting. So I created a function called
something like Startup(), which is called in the constructor right after
InitilizeComponent(). Any code that i need at startup that I didn't create
via the designer i put in this function so that VS.net can't touch it.

Nick Harris, MCSD
http://www.VizSoft.net
 
Xiko,

this is because you are placing your code in initializeComponent() method.
initializecomponent is a method that is generated by VS for Windows Form
Designer Support. What you need to do is put your code in the constructor
AFTER the call to initializeComponents, or on the FormLoad event. see sample
below

Hope This helps

Marco

IE:

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//


this.SearchButton.Click = System.EventHandler(this.SearchButton_Click);


}
 
Hi,

It had happened to me I think, but I cannot be precise of it was using 2002
or 2003 , it haven't happen in several months now, I don't know if it's
because I haven't use the designer in a long time or because it was
corrected on the 2003 version.


cheers,
 
my solution was to abandon designer. it was pretty stupid anyhow (modifying
my static controls for example).
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

It had happened to me I think, but I cannot be precise of it was using 2002
or 2003 , it haven't happen in several months now, I don't know if it's
because I haven't use the designer in a long time or because

I think maybe it's one way to avoid this annoying thing
it was corrected on the 2003 version.

no, it wasn't... :>(

thanks
 
I believe VS.NET is removing my code. I have a button control on a
web form.
The button control raises an event, and my web form handles the event.

The code that I add to the web form to handle the user control's event
is shown below.

Sometimes VS.NET has nuked my code, clipping the line code where the
handler event is. When it hasn't, the code works just like I want it
to.

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
//the line below disappear occasionally so I have to retype it.
this.SearchButton.Click =
System.EventHandler(this.SearchButton_Click);
}

Does anyone knows some fix for this missbehavior?

VS.net 2003
framework 1.1
Windows XP Pro


Thanks in advance.


As far as I'm aware, this is a feature and not a bug.
According to Chris Sells (Windows Forms Programming in C#)...

"...It may look like your favourite programming language, but
InitializeComponent is actually the serialized form of the object
model that the designer is using to manage the design interface.
Although you can make minor changes to this code, such as changing the
Text property on the new button, major changes are likely o be ignored
- or worse, thrown away. Feel free to experiment with just how far you
can go by modifying this serialization format by hand, but don't be
surprised when your work is lost. I recommend putting custom form
initialization into the form's constructor, AFTER the call to
InitializeComponent, giving you confidence that your code wil be safe
from the Designer..."
 
As far as I'm aware, this is a feature and not a bug.
According to Chris Sells (Windows Forms Programming in C#)...

"...It may look like your favourite programming language, but
InitializeComponent is actually the serialized form of the object
model that the designer is using to manage the design interface.
Although you can make minor changes to this code, such as changing the
Text property on the new button, major changes are likely o be ignored
- or worse, thrown away. Feel free to experiment with just how far you
can go by modifying this serialization format by hand, but don't be
surprised when your work is lost. I recommend putting custom form
initialization into the form's constructor, AFTER the call to
InitializeComponent, giving you confidence that your code wil be safe
from the Designer..."

Also I forgot to add...

That's probably the reason for the comment:

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

In all fairness, perhaps MS should allow a way to "freeze" bits of
code and make it such that any modification is only allowed once that
section is explicitly "unfrozen" by the developer. Of course the
designer should still be allowed to access this code.

This could be useful for bits of code we're already happy with too so
that we don't inadvertently change it by mistake.

I'm not aware that anything like this exists?
 
I affectionally call this the 'wipe bug' and add a region above the
InitializeComponent with the event assignments in a comment block. This way
I can paste them all back when the bug strikes. The bug generally happens
when controls are cut and pasted around in the form. I generally avoid the
designer as much as poss for this reason (dynamic controls are the way to
go), and also the fact its HTML formatting really sucks :o(

eg -

#region Wipe Bug
/*
this.Load += new System.EventHandler(this.Page_Load);
this.SearchButton.Click = System.EventHandler(this.SearchButton_Click);
*/

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor unless you want to keep
your events :o)
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.SearchButton.Click = System.EventHandler(this.SearchButton_Click);
}

Simon
 
I have the same problem in my code behind files. Wish this would have
been solved in V2003. I do the same thing, putting all the event
handlers in a method I call from the InitilizeComponent() method. A
pain but it works...

Earl
 
Adding the event handlers is great for C# users, but I'm using Visual Basic .NE
It's removing "Handles myControl.whatever_event" from the end of my handler.
How do I fix this?
I must say this is the stupidest bug I have ever come across, and it seems like something that should have been a priority for the VS 2003 release.
I never touch the Windows Code Designer area, so that's not the problem. Rearranging controls on a form shouldn't cause your program to crash.

----- Simon Storr wrote: ----

I affectionally call this the 'wipe bug' and add a region above th
InitializeComponent with the event assignments in a comment block. This wa
I can paste them all back when the bug strikes. The bug generally happen
when controls are cut and pasted around in the form. I generally avoid th
designer as much as poss for this reason (dynamic controls are the way t
go), and also the fact its HTML formatting really sucks :o

eg

#region Wipe Bu
/
this.Load += new System.EventHandler(this.Page_Load)
this.SearchButton.Click = System.EventHandler(this.SearchButton_Click)
*

/// <summary
/// Required method for Designer support - do not modif
/// the contents of this method with the code editor unless you want to kee
your events :o
/// </summary
private void InitializeComponent(

this.Load += new System.EventHandler(this.Page_Load)
this.SearchButton.Click = System.EventHandler(this.SearchButton_Click)


Simo
 
Back
Top