Manual editing of *.designer.cs file

前端 未结 5 1332
时光取名叫无心
时光取名叫无心 2020-11-28 16:29

I\'m aware, that the .designer.cs file contains data generated by the visual form designer in Visual Studio. However, I have some additional methods though, whi

相关标签:
5条回答
  • 2020-11-28 16:52

    Leaving designer.cs in peace not only prevents your changes from being overwritten, but also helps other developers by saying that nothing unexpected should come out of it. That being said, there is at least one exception I can think of and that is the one mentioned by author of the post: extension of Dispose() method. To my knowledge this code - once generated - will not be overwritten.

    However, in my opinion much better solution is to override the Dispose method and than call the base.Dispose(), so that we leave designer.cs clean.

    0 讨论(0)
  • 2020-11-28 16:53

    You should never modify .designer.cs. Period. Your changes will be overwritten without mercy.

    Update: To be a bit more helpful, C# since v3 (VS 2008) has included partial methods, which many designers will now use to let you implement custom behavior.

    0 讨论(0)
  • 2020-11-28 17:05

    Partial designer form class it's used by Visual Studio for placing all code need for build the control.

    The method InitializeComponent() can't be overwrite: it's used by designer editor for render a preview of your form! Try in a new project: resize your form, add a label and a button and rename the InitializeComponent() method + re-compile. Your form back to default size!

    If you need to call code by form loading, just override OnLoad() virtual method, if you need to call code by form showing, simple override OnShown() virtual method.

    Remember to call the base.Method() at begin of it override.

    Hope this little my experience can help!

    0 讨论(0)
  • 2020-11-28 17:16

    this instruction applies to the complete designer.cs file. As all the code written in it is automatically generated.

    You should not do any modifications in this file as it can be recreated anytime... this will remove your methods...

    If you want to keep the code separate from the form code file, then I suggest to create another file which contains a partial class where you can put all such methods...

    Hope it helps...

    0 讨论(0)
  • 2020-11-28 17:16

    I think the other answers are simplifying too much.

    First of all, I totally agree that it's almost always a bad idea to edit a .designer file, but there are a few cases where I've done so, feel it was good and proper, and didn't get burned.

    1. Say I create a label and accidentally double click. Designer creates a method in my main .cs file which I then delete:

      private void label1_Click(object sender, EventArgs e)
      {
      
      }
      

      Well, now the code won't build unless I also delete the following from my .designer file:

      this.label1.Click += new System.EventHandler(this.label1_Click);
      
    2. Less frequently, the order in which things are added to a form or panel (or menu!) matters, and it can be easier to change this order in the code than in the Designer GUI. In my experience VS 2010 always picks up on this, updates its GUI's info, and redraws its preview. Just remember to focus on the Add() methods--the order variables are declared in generally doesn't matter.

    3. Ditto if you set a property that causes a line to be added to the .designer file, deleting the line gets picked up quickly and Designer refreshes. Maybe it's wiser/safer to use the GUI to change the property, but I think deleting the line is cleaner.

    4. Code that is not inside this region, #region Windows Form Designer generated code, will only get generated once. It is safe to move, and as others have recommended elsewhere (https://stackoverflow.com/a/6527072/1593924), moving the Dispose(bool) method out actually can make a lot of sense, if you're modifying it or adding a Dispose() method that should ideally sit next to Dispose(bool).

      protected override void Dispose(bool disposing)
      {
          if (disposing && (components != null))
          {
              components.Dispose();
          }
          base.Dispose(disposing);
      

    DISCLAIMERS:

    1. That said, I've only tried VS 2010 Ultimate; your mileage may vary on 1-3 above, but 4 should be safe as long as the .designer is a partial class with Dispose(bool) outside that #region. I also make sure the latest good version of a .designer file is committed into the source repository before messing with it.

    2. By admitting to having gone along with the Dispose(bool disposing) pattern, I'm not meaning to promote that approach. There seem to be good reasons to simply use Dispose() in most cases, and only do more for unmanaged resources, each of which is encapsulated one-to-one in a dedicated Disposable object.

    0 讨论(0)
提交回复
热议问题