Search code examples
.netvb.netwinformsvisual-studiodesigner

Can layout-adjusting statements be re-arranged in code generated by Designer?


In order to easily merge changes in *.Designer.vb files (the same applies to *.Designer.cs files), I need to sort content of certain code sections in files generated by Designer because Visual Studio is sometimes reordering them randomly. I can see 16 merge conflicts before rearranging, zero conflicts after.

I have no doubt on sorting declarations etc. But my doubt is about ResumeLayout(false) and PerformLayout() calls at end of InitializeComponent() method. Despite some learning, I don't fully understand yet how they work (practical details found here) and therefore I'm seeking an answer whether it is safe to re-arrange the order of objects they are called upon. Testing with Designer did not show problems so far but I'm asking to be sure.

Original order in Form1.Designer.vb

Private Sub InitializeComponent()

    '...initializations of controls are left out

    '
    'Form1
    '
    Me.ClientSize = New System.Drawing.Size(784, 562)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Name = "Form1"
    Me.Text = "Form1 Title"

    'original order created by Designer
    CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.TableLayoutPanel1.ResumeLayout(False)
    Me.TableLayoutPanel1.PerformLayout()
    Me.page2.ResumeLayout(False)
    Me.page2.PerformLayout()
    Me.page1.ResumeLayout(False)
    Me.TableLayoutPanel2.ResumeLayout(False)
    Me.TableLayoutPanel2.PerformLayout()
    Me.TabCtl1.ResumeLayout(False)
    CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
    'end of section in question

    Me.ResumeLayout(False)

End Sub

Modified order in Form1.Designer.vb

Private Sub InitializeComponent()

    '...initializations of controls are left out

    '
    'Form1
    '
    Me.ClientSize = New System.Drawing.Size(784, 562)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Name = "Form1"
    Me.Text = "Form1 Title"

    'the following lines (or pairs) were arranged alphabetically - is it OK?
    CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).EndInit()
    CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.page1.ResumeLayout(False)
    Me.page2.ResumeLayout(False)
    Me.page2.PerformLayout()
    Me.TabCtl1.ResumeLayout(False)
    Me.TableLayoutPanel1.ResumeLayout(False)
    Me.TableLayoutPanel1.PerformLayout()
    Me.TableLayoutPanel2.ResumeLayout(False)
    Me.TableLayoutPanel2.PerformLayout()
    'end of sorted groups

    Me.ResumeLayout(False)

End Sub

Is such a change always safe? Or should the commands be arranged with respect to tree hierarchy of controls (parent listed first) instead of alphabetical ordering?

EDIT: some further research shows me, that form designer puts ancestors first, but enforces no other particular ordering. Order of controls is determined only by order of Control.Add() calls. Maybe I can improve sorting method to reflect this and I should be safe.


Solution

  • My own answer is maybe not. In my latest solution I avoided being depended on knowing exact answer.

    I've got around the problem by sorting objects as a tree (putting parent objects first + sorting alphabetically within each node) instead of dumping the objects in flat alphabetical order. This preserves the logic how Visual Studio designer is ordering objects while also fixing Visual Studio "randomness" by applying order which is always deterministic.

    The tool rearranging content of *.Designer.vb is doing its job fine. Maybe I'll publish it as open source if there is some interest.