Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manually modifying Windows Form Designer generated code

Status
Not open for further replies.

ddiamond

Programmer
Apr 22, 2005
918
US
I often find it very convenient to modify the "Windows Form Designer generated code". But I always wonder about the ominous comment at the top:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

So is modifying this code with the code editor considered bad practice? Specifically, I often add objects to the InitializeComponent() method. This makes them magically appear on my form, and I haven't found any other way to do that.
 
It's possible, but dangerous.

VS.NET 2003 is much better with regards to the stability of making your own changes to this region than 2002 was. I haven't tried in 2005 yet.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
The problem I've seen is that changes made in the visual designer after the fact sometimes cause that region to be regenerated, wiping out the customized code.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me
 
Thanks everyone for your tips.
The problem I've seen is that changes made in the visual designer after the fact sometimes cause that region to be regenerated, wiping out the customized code.
I haven't run into that, but it sounds a little bit scary. The main reason I've added objects to the InitializeComponent() method is so that I can select the object from the properties box, and then click on the lightening bolt to generate an event handler. If I don't declare the object in InitializeComponent and add comments for it, it does not appear on the properties dropdown. Does anyone know a safer way to do this other than manually adding the declaration to the InitializeComponent as I have done?
 
I am constantly having my code erased by VS 2003. Its such a pain. I ended up taking a lot of control customization code and putting it into a separate method.

public myclass()
{
InitializeComponents(); <-- Don't touch
CustomizeComponents(); <-- Make your changes here
}
 
I do the same thing that JurkMonkey does. I just make my own method that I call alongside the InitializeComponents()

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
I think I see what is going on. I had added the following code to InitializeComponents():
Code:
this.LoginDialog = new PoolMaintenance.frmLogin();
and
Code:
[green]// 
// LoginDialog
//[/green]
Because of that comment block, Visual Studio recognizes LoginDialog as a design element of the form, and regenerates the custome code instead of wiping it.

The next time visual studio regenerated the InitializeCompents() method, it added the following code below my comment block:
Code:
[green]// 
// LoginDialog
//[/green]
this.LoginDialog.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.LoginDialog.ClientSize = new System.Drawing.Size(272, 85);
this.LoginDialog.Location = new System.Drawing.Point(66, 66);
this.LoginDialog.Name = "LoginDialog";
this.LoginDialog.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.LoginDialog.Text = "frmLogin";
this.LoginDialog.Visible = false;
 
be aware that if you do add custom controls to your projects like this, this bug may cause the ide to delete them out again at random.

it's very galling.

mr s. <;)

 
Has anyone played with 2005? Is it any more reliable?
 
2005 has introduced Partial Classes. This allows the MS code to be generated in one partial class, and your code to be developed in a separate partial class.

This reduces the temptation to change designer code. Remeber: The designer is a tool to make GUI Layout easier. You have to follow the rules of the tool or it will make your life "fun".
 
I recall Delphi 7 having a nice feature that allowed you to add event handlers.

Not just for the main code block but pre form initialization, during startup, after startup and during deconstruction. A very useful feature that allowed me to add custom controls and other features only available during runtime (to stop anything from breaking up a hidden area of the form I simply placed a panel or box and locked it so it was not visible).

I am not sure how the C# does this as I have not yet looked into it but I am sure there must be a method to create a OnInitialize event similar to Delphi that lets you add code so things happen before the app is visible or initialized.

Just a thought..

Fz
 
I guess my real question is how to get Visual Studio to put an object that has events in the properties dropdown list so that events can be select using the lightening bolt. Adding the comments listed above to the InitializeComponents() method is one way to do this. Does anyone know a safer way? Of course I could just manually code the event handler and add it to the listener, but I'm trying to get Visual Studio to do some of the work for me.
 
ummm, normally you would just declare your event as public - recompile, re-open the control and it would show up under the lightning bolt.

public event EventHandler DoStuff;
 
When you add a timer or a Data component to your form, it appears below your form. I imagine those classes extend some other base class. If your control could extend this base class as well it would show up under the form and you would be able to access its properties.
 
That's a good thought. I'll have to look into it. I'm hoping it is an interface.
 
I just remembered I did a similar thing in Delphi.

I had created a class that performed various functions on the form like displaying co-ordinates and other dynamic information but I wanted this to be available to other forms from the IDE so I turned the class into a component and simply added this to any form I was working with and voila, I had access to the class and the associated events showed up.

I just had a breif look into C# and I think the rules are virtually the same. I am pretty sure all you need to do is create your own component or add-in and then add this to the form exposing the events.

Good luck.

Fz
 
Thanks for the suggestion Fz.

Does anyone know if a component in C# can contain a form?
 
sure no problem.

set the forms toplevel to false and then add it to the controls collection of your component.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top