2

When visual studio designer adds the following lines to the code, my app UI undergoes some undesirable displacement.

((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
:
:
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();

How can I prevent it?


Edition:

I created a new simple project with only two nested SplitContainers and encountered the same issue.

Problem:

As it's been marked in the following code, SplitterWidth of splUpperSection remains unchanged! If you remove BeginInit and EndInit methods, this property (SplitterWidth) will be changed! Is it a farmework BUG???

The InitializeSplitContainers method contains exactly the code that Visual Studio designer generates automatically. You also can simply create a new Form and add two nested split containers to it which have a SplitterWidth of 1 to touch the problem easily.

Code:

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace TestApp
{
    public partial class Form1 : Form
    {
        //
        // SplitContainers
        //
        private SplitContainer splBase;
        private SplitContainer splUpperSection;

        /// <summary>
        /// The form has initially no child control.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            InitializeSplitContainers();
        }

        private void InitializeSplitContainers()
        {
            this.splBase = new SplitContainer();
            this.splUpperSection = new SplitContainer();
            ((ISupportInitialize)(this.splBase)).BeginInit();
            this.splBase.Panel1.SuspendLayout();
            this.splBase.SuspendLayout();
            ((ISupportInitialize)(this.splUpperSection)).BeginInit();
            this.splUpperSection.SuspendLayout();
            this.SuspendLayout();
            // 
            // splBase
            // 
            this.splBase.BackColor = Color.Red;
            this.splBase.Dock = DockStyle.Fill;
            this.splBase.FixedPanel = FixedPanel.Panel1;
            this.splBase.IsSplitterFixed = true;
            this.splBase.Location = new Point(0, 0);
            this.splBase.Name = "splBase";
            this.splBase.Orientation = Orientation.Horizontal;
            // 
            // splBase.Panel1
            // 
            this.splBase.Panel1.Controls.Add(this.splUpperSection);
            // 
            // splBase.Panel2
            // 
            this.splBase.Panel2.BackColor = Color.White;
            this.splBase.Size = new Size(400, 400);
            this.splBase.SplitterDistance = 115;
            this.splBase.SplitterWidth = 1;
            this.splBase.TabIndex = 0;
            // 
            // splUpperSection
            // 
            this.splUpperSection.BackColor = Color.Chartreuse;
            this.splUpperSection.Dock = DockStyle.Fill;
            this.splUpperSection.FixedPanel = FixedPanel.Panel1;
            this.splUpperSection.IsSplitterFixed = true;
            this.splUpperSection.Location = new Point(0, 0);
            this.splUpperSection.Name = "splUpperSection";
            this.splUpperSection.Orientation = Orientation.Horizontal;
            // 
            // splUpperSection.Panel1
            // 
            this.splUpperSection.Panel1.BackColor = Color.White;
            // 
            // splUpperSection.Panel2
            // 
            this.splUpperSection.Panel2.BackColor = Color.White;
            this.splUpperSection.Size = new Size(400, 115);
            this.splUpperSection.SplitterDistance = 25; // ←Will be set
            this.splUpperSection.SplitterWidth = 1;     // ←Won't be set (stays: 4)
            this.splUpperSection.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new Size(400, 400);
            this.Controls.Add(this.splBase);
            this.Name = "Form1";
            this.Text = "Bug Form";
            this.splBase.Panel1.ResumeLayout(false);
            ((ISupportInitialize)(this.splBase)).EndInit();
            this.splBase.ResumeLayout(false);
            ((ISupportInitialize)(this.splUpperSection)).EndInit();
            this.splUpperSection.ResumeLayout(false);
            this.ResumeLayout(false);
        }
    }
}

Workaround:

public Form1()
{
    //
    // Initializing components including split-containers..
    //
    InitializeComponent();
    {
        //
        // keeping initializing on..
        //
        splBase.SplitterWidth = 1;
        splUpperSection.SplitterWidth = 1;
    }
}
Mehdi
  • 2,194
  • 2
  • 25
  • 39
  • Can you confirm its web not winform. It looks like your CSS needs fixing not VS's designer code, this stuff happens when you edit designer code: http://stackoverflow.com/questions/4548316/prevent-auto-code-change-in-designer-cs-for-a-specific-line – Jeremy Thompson Apr 26 '12 at 11:24
  • It's a winform application and has no CSS. – Mehdi Apr 26 '12 at 11:53
  • Thanks @JeremyThompson for adding winforms tag. – Mehdi Apr 26 '12 at 12:45
  • 1
    +1 just an fyi: you could possibly try http://hawkeye.codeplex.com/ to tweak form properties at run-time, then once you figure out how to combat the undesired displacement add the necessary code after InitComponent() – Jeremy Thompson Apr 27 '12 at 01:37
  • +1 Thanks @Jeremy. What do you think about this issue? – Mehdi Apr 27 '12 at 07:29
  • which lines of code do we include or comment out to reproduce the problem with the above code? does it matter that the code is not in the designer.cs file? I've played around with it but not sure of what the problem will look like on this barebone project. – Jeremy Thompson Apr 27 '12 at 07:50
  • The question has been edited again to be more clear – Mehdi Apr 27 '12 at 10:23
  • How do you solve this problem? – Glebka Sep 27 '18 at 12:31

2 Answers2

1

These calls are required to inform to the this.splitContainer1 object that all the initialization has been done to avoid having to enter object property values in a specified order.

Only when you call EndInit the values of the properties are evaluated.

Said that, this should not displace your UI in anyway if the values you set on the object proeprties does not displace the object.

EDIT: The only thing that happens when calling EndInit() is the container executing the following methods:

if (this.newPanel1MinSize != this.panel1MinSize)
{
    this.ApplyPanel1MinSize(this.newPanel1MinSize);
}
if (this.newPanel2MinSize != this.panel2MinSize)
{
    this.ApplyPanel2MinSize(this.newPanel2MinSize);
}
if (this.newSplitterWidth != this.splitterWidth)
{
    this.ApplySplitterWidth(this.newSplitterWidth);
}

So your problem has to be related with one or more of these 3 properties.

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • But when I remove them everything becomes OK. What may the reason be? – Mehdi Apr 26 '12 at 10:47
  • Probably because without the EndInit call some property values are not being evaluated or are evaluated in a different order. Could you paste some code or imageS? – Ignacio Soler Garcia Apr 26 '12 at 10:51
  • I remove the BeginInit and EndInit simultaneously. Any other idea? – Mehdi Apr 26 '12 at 11:25
  • If an object implements the ISupportInitialize (look here: http://msdn.microsoft.com/es-es/library/system.componentmodel.isupportinitialize.aspx) means it probably needs it. Could be an issue of the control? If it's a framework control tell me which one, if not, try to decompile it by yourself to look what it does when you call EndInit. – Ignacio Soler Garcia Apr 26 '12 at 12:12
  • I added document outline pic. – Mehdi Apr 26 '12 at 13:12
  • Which .Net version are you using? SplitContainer looks like does not implement ISupportInitialize on .Net 2 & 3 – Ignacio Soler Garcia Apr 26 '12 at 13:34
  • I experienced this issue on .Net 3.5 & 4.0! – Mehdi Apr 26 '12 at 13:46
  • +1 You are right @SoMoS. I checked it again. Both `SplitterWidth` and `Panel1MinSize` have been set (changed), but after calling `EndInit`, the `SplitterWidth` won't be set while `Panel1MinSize` will be set!! Not setting the `SplitterWidth` causes a displacement. But why? Why doesn't it be set??? – Mehdi Apr 26 '12 at 15:14
  • +1 Thanks for info. I edited my question. Would you mind taking a look at it? – Mehdi Apr 27 '12 at 07:23
0

I got this error when I merged changes back from a working project which was migrated from 2010 to 2015. In the Updated Project the 2 new lines were added in the designer something like:

        ((ISupportInitialize)(this.splBase)).BeginInit();

and

        ((ISupportInitialize)(this.splBase)).EndInit();

If I ran the version running on 2015, it ran without issues.

To resolve this in VS 2010, I deleted both of these lines. After that the project has been working fine.