2

当 Visual Studio Designer 将以下行添加到代码中时,我的应用程序 UI 会发生一些不良位移。

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

我该如何预防?


版:

我创建了一个只有两个嵌套SplitContainer的新简单项目,并遇到了同样的问题。

►<em>问题:

如下代码所示,SplitterWidthofsplUpperSection保持不变!如果你删除BeginInitEndInit方法,这个属性(SplitterWidth)将被改变! 难道是农事BUG???

InitializeSplitContainers方法包含 Visual Studio 设计器自动生成的代码。您也可以简单地创建一个新表单并向其中添加两个嵌套的拆分容器,它们的 aSplitterWidth为 1 以轻松解决问题。

►<em>代码:

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);
        }
    }
}

►<em>解决方法:

public Form1()
{
    //
    // Initializing components including split-containers..
    //
    InitializeComponent();
    {
        //
        // keeping initializing on..
        //
        splBase.SplitterWidth = 1;
        splUpperSection.SplitterWidth = 1;
    }
}
4

2 回答 2

1

这些调用需要通知this.splitContainer1对象所有初始化都已完成,以避免必须按指定顺序输入对象属性值。

只有当您调用 EndInit 时,才会评估属性的值。

也就是说,如果您在对象属性上设置的值不会取代对象,那么无论如何这都不应该取代您的 UI。

编辑:调用 EndInit() 时唯一发生的事情是容器执行以下方法:

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);
}

所以你的问题必须与这三个属性中的一个或多个有关。

于 2012-04-26T10:43:41.987 回答
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.

于 2018-01-06T13:09:45.407 回答