检测SplitContainer在运行时的方向变化

时间:2017-03-11 15:01:27

标签: c# winforms

有没有办法在运行时检测Orientation System.Windows.Forms.SplitContainer的变化?

我找不到任何可以覆盖的事件处理程序或虚函数。

最终用户可以通过UI元素(例如:切换按钮)更改

Orientation。但运行时可能是一种过度杀伤力。为了简化,在C#中开发UI类库时,我的库中的一个控件,当置于SplitContainer时,需要在设计模式下响应这样的事件,以重新调整自己的布局和子项。所以我没有从SplitContainer得到的奢侈。

1 个答案:

答案 0 :(得分:0)

我认为你想要跟随

这样的事情

<强>更新 Splitter移动排除

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

namespace WindowsFormsApplication1
{
    [Docking(System.Windows.Forms.DockingBehavior.Ask)]
    public partial class MyUserControl : UserControl
    {
        TextBox textBox1;
        TextBox textBox2;

        public MyUserControl()
        {
            BackColor = Color.White;
            this.textBox1 = new TextBox();
            Controls.Add(this.textBox1);
            this.textBox2 = new TextBox();
            Controls.Add(this.textBox2);
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);

            if (Parent is Panel)
            {
                if ((Parent as Panel).Parent is SplitContainer)
                {
                    Parent.Resize += Parent_Resize;
                    AlignChildren();
                }
            }
        }

        SplitContainer SplitContainer
        {
            get
            {
                return Parent.Parent as SplitContainer;
            }
        }

        Orientation lastOrientation;

        void Parent_Resize(object sender, EventArgs e)
        {
            if (Parent == null || IsDisposed)
            {
                Parent.Resize -= Parent_Resize;
                return;
            }

            if (lastOrientation != SplitContainer.Orientation)
            {
                AlignChildren();
            }
            lastOrientation = SplitContainer.Orientation;
        }

        void AlignChildren()
        {
            switch (SplitContainer.Orientation)
            {
                case Orientation.Horizontal:
                    this.textBox1.Location = new Point(10, 10);
                    this.textBox2.Location = new Point(this.textBox1.Right + 5, 10);
                    break;
                case Orientation.Vertical:
                    this.textBox1.Location = new Point(10, 10);
                    this.textBox2.Location = new Point(10, this.textBox1.Bottom + 5);
                    break;
            }
        }
    }
}

将此控件放在Designer中的SplitContainer上。更改SplitContainer的方向属性。同样在运行时尝试切换按钮。

更新2

您也可以创建自己的SplitContainer

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MySplitContainer : System.Windows.Forms.SplitContainer
    {
        public new Orientation Orientation
        {
            get
            {
                return base.Orientation;
            }
            set
            {
                if (base.Orientation != value)
                {
                    base.Orientation = value;
                    OnOrientationChanged();
                }
            }
        }

        protected virtual void OnOrientationChanged()
        {
            if (OrientationChanged != null)
            {
                OrientationChanged(this, EventArgs.Empty);
            }
        }

        public event EventHandler OrientationChanged;
    }
}