2

ビジュアル スタジオ デザイナーが次の行をコードに追加すると、アプリの UI に望ましくない変位が発生します。

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

どうすれば防ぐことができますか?


版:

ネストされた が 2 つだけの新しい単純なプロジェクトを作成したSplitContainerところ、同じ問題が発生しました。

►<em>問題:

次のコードでマークされているように、SplitterWidthofsplUpperSectionは変更されません! BeginInitandメソッドを削除するとEndInit、このプロパティ ( SplitterWidth) が変更されます。 農作業バグですか???

このInitializeSplitContainersメソッドには、Visual Studio デザイナーが自動的に生成するコードが正確に含まれています。また、単純に新しいフォームを作成し、ネストされたSplitterWidth1 の 2 つの分割コンテナーを追加して、問題に簡単に対処することもできます。

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

したがって、問題はこれら 3 つのプロパティの 1 つ以上に関連している必要があります。

于 2012-04-26T10:43:41.987 に答える