Boris
Boris

Reputation: 8931

Sorting designer code on save

When opening a Form in the Visual Studio Designer, the generated designer files' contents get mixed up randomly. This includes the files

When using a version control system this is a real nightmare. Is there a way (extension?) that sorts and cleans up all designer files before saving? This would solve most of my VCS related issues with WinForms, as it reverses all the shuffling the designer does.

Upvotes: 1

Views: 883

Answers (2)

Kasper van den Berg
Kasper van den Berg

Reputation: 9526

This is not a tip about automatically sorting Form.Designer.cs; however, it does help with avoiding merge help with all modifications made to Form.Designer.cs-files by Visual Studio.

Instead of (or in addition to) changing your work practices (by Jonathon Lee) and especially the constricting "Limit access to a form to 1 developer at a time" do:

Ensure the Form.Designer.cs-file is organized according to Visual Studio in a separate commit before you make the real changes.

  • Trigger a reorganization of the Form.Designer.cs-file by Visual Studio
    (For me, moving a control from one cell in a TableLayoutPanel to an other and back again did the trick.)
  • Commit Visual Studio's changes and mark them as nothing changed.
  • Make your modifications to the Form
  • Trigger a reorganization of the Form.Designer.cs-file by Visual Studio
  • Commit your changes and describing your work.

Results:

  • This helps reviewers to distinguish changes to review from noise.
  • This eases merging:
    • either, your version control system detects that two commits have the same effect and one is enough and it can merge without conflicts;
    • or, you manually resolve the conflict by applying just one of the cleanup commits and discard the other.

Upvotes: 0

Jonathon Lee
Jonathon Lee

Reputation: 211

This problem sure does make merging difficult - I understand your pain.

Read this previous SO post: "Why does C# designer-generated code (like Form1.designer.cs) play havoc with Subversion?"

Basically you could create a tool to sort all the code alphabetically to give order to the random placement of code, but it is a hack and could involve a lot of pain itself. Personally I recommend changing your work practices to reduce this occurring.

  • Reduce time between merges
  • Limit access to a form to 1 developer at a time.
  • Merge under the guidance of the developer who made the change, as they will know better what looks ok.
  • Don't open the designer, if you are only making a "code change", ie nothing changes visually.
  • Undo changes to the designer file before merges, if you are 100% sure that you didn't change anything.

Upvotes: 4

Related Questions