4

I've gone through multiple resources on trying to find the use cases of when to manually add code to InitializeComponent, but haven't found anything concrete. This suggests we shouldn't do it - The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified

In the past, I had always used the form designer and never needed to manually make changes to InitializeComponent(). However at my new firm, the tech lead has completely ignored the form designer and manually added most UI elements within InitializeComponent (i.e. it has either been heavily edited, or completely re-written to the extent that the Designer can't recreate the GUI in VS, but the GUI is visible and fully functional at when the code is executed)

What is the advantage of not using the form designer? Is it just a matter of preference? Is manually editing/rewriting InitializeComponent for simple UI elements a good practice?

Thanks!

Community
  • 1
  • 1
user2635088
  • 1,598
  • 1
  • 24
  • 43
  • 1
    As far as I can tell, two major downsides: you lose the Design view hence don't really know how the components will look like, and even worse, any Rebuild of the code might clean away all the custom changes. IMO it's really a bad choice by someone who don't really know or understand the consequences – Shadow The GPT Wizard Oct 26 '15 at 15:03
  • I wouldn't do that. Wondering what were the reasons they did that. Basically through away your designer. – Marius Bancila Oct 26 '15 at 15:03
  • 1
    It can be quicker to edit the code. Editing it to a point that the designer cannot parse it correctly anymore is, well, rough on everybody else in the team that prefers to use the designer. This is something your team has to fix, nobody here can help you with this. – Hans Passant Oct 26 '15 at 15:12
  • Thanks guys, this was very helpful. Not sure why I was voted negative - I'm actually incentivized to delete this post because of the negative vote. Isn't there always a chance that someone without a lot of experience might find this helpful? I certainly did :S – user2635088 Oct 26 '15 at 19:15

1 Answers1

4

There are only a few reasons why one might need to edit InitializeComponent, such as:

  1. To correct bugs introduced by a bad toolbox item designer.
  2. To remove all traces of a control that cannot be deleted from the VS designer. (This has to be done carefully.)
  3. To make quick "designer friendly" tweaks: changing a property value, adding a property setting, or removing a property setting. When adding a setting, I make sure it looks just like the designer-generated code.

After editing InitializeComponent, I always save & switch back to Designer mode to make sure I didn't break anything.

Any manual initialization code should be added outside InitializeComponent, e.g. OnLoaded() in a WinForms form or user control. As for fixing existing forms, it may be simple or nearly impossible depending on how complicated the form is, especially if controls have been added manually and required initialization methods (such as SuspendLayout, BeginInit) aren't being called.

tremas317
  • 81
  • 1