关于ToolStrip设置Location无效的问题

问题现象

当多个ToolStrip使用ToolStripContainer布局时,可以让用户自己拖动工具栏,所以在程序关闭时必须保存用户拖动工具栏的位置,但是在再次打开程序后,还原回来的工具栏位置会有问题,虽然设置的与原来一致,但是起不了效果,每次位置都不确定

问题原因

产生问题的原因是在设置ToolStrip.Location时,没有没有挂起容器的布局,导致位置不确定

解决方法

在设置Location前要添加

            container.SuspendLayout();
            container.TopToolStripPanel.SuspendLayout();
            container.LeftToolStripPanel.SuspendLayout();
            container.RightToolStripPanel.SuspendLayout();
            container.BottomToolStripPanel.SuspendLayout();

完成之后添加

            container.TopToolStripPanel.ResumeLayout();
            container.LeftToolStripPanel.ResumeLayout();
            container.RightToolStripPanel.ResumeLayout();
            container.BottomToolStripPanel.ResumeLayout();
            container.ResumeLayout();

附赠代码

    [Serializable]
    public class ToolStripLayoutCollection
    {
        private List<ToolStripLayout> _Items = new List<ToolStripLayout>();
        public List<ToolStripLayout> Items
        {
            get
            {
                return _Items;
            }
        }
        //
        public ToolStripLayout GetItemByName(string name)
        {
            foreach (ToolStripLayout item in _Items)
            {
                if (item.Name == name)
                {
                    return item;
                }
            }
            return null;
        }
        public void From(ToolStripContainer container)
        {
            _Items.Clear();
            foreach (Control control in container.TopToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    ToolStripLayout item = new ToolStripLayout();
                    item.Name = control.Name;
                    item.Type = LocationTypes.Top;
                    item.Location = control.Location;
                    _Items.Add(item);
                }
            }
            foreach (Control control in container.LeftToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    ToolStripLayout item = new ToolStripLayout();
                    item.Name = control.Name;
                    item.Type = LocationTypes.Left;
                    item.Location = control.Location;
                    _Items.Add(item);
                }
            }
            foreach (Control control in container.BottomToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    ToolStripLayout item = new ToolStripLayout();
                    item.Name = control.Name;
                    item.Type = LocationTypes.Bottom;
                    item.Location = control.Location;
                    _Items.Add(item);
                }
            }
            foreach (Control control in container.RightToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    ToolStripLayout item = new ToolStripLayout();
                    item.Name = control.Name;
                    item.Type = LocationTypes.Right;
                    item.Location = control.Location;
                    _Items.Add(item);
                }
            }

        }
        public void To(ToolStripContainer container)
        {
            container.SuspendLayout();
            container.TopToolStripPanel.SuspendLayout();
            container.LeftToolStripPanel.SuspendLayout();
            container.RightToolStripPanel.SuspendLayout();
            container.BottomToolStripPanel.SuspendLayout();
            List<ToolStrip> tools = new List<ToolStrip>();
            foreach (Control control in container.TopToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    tools.Add(control as ToolStrip);
                }
            }
            container.TopToolStripPanel.Controls.Clear();
            //
            foreach (Control control in container.LeftToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    tools.Add(control as ToolStrip);
                }
            }
            container.LeftToolStripPanel.Controls.Clear();
            foreach (Control control in container.BottomToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    tools.Add(control as ToolStrip);
                }
            }
            container.BottomToolStripPanel.Controls.Clear();
            foreach (Control control in container.RightToolStripPanel.Controls)
            {
                if (control is ToolStrip)
                {
                    tools.Add(control as ToolStrip);
                }
            }
            container.RightToolStripPanel.Controls.Clear();
            for (int j = 0; j <= _Items.Count - 1; j++)
            {
                ToolStripLayout item = _Items[j];
                for (int i = 0; i < tools.Count; i++)
                {
                    ToolStrip tool = tools[i];
                    if (tool.Name == item.Name)
                    {
                        tool.Location = item.Location;
                        if (item.Type == LocationTypes.Top)
                        {
                            container.TopToolStripPanel.Controls.Add(tool);
                        }
                        else if (item.Type == LocationTypes.Bottom)
                        {
                            container.BottomToolStripPanel.Controls.Add(tool);
                        }
                        else if (item.Type == LocationTypes.Left)
                        {
                            container.LeftToolStripPanel.Controls.Add(tool);
                        }
                        else if (item.Type == LocationTypes.Right)
                        {
                            container.RightToolStripPanel.Controls.Add(tool);
                        }
                        tool.Location = item.Location;
                        tools.RemoveAt(i);
                        break;
                    }
                }
            }
            if (tools.Count > 0)
            {
                container.TopToolStripPanel.Controls.AddRange(tools.ToArray());
            }
            container.TopToolStripPanel.ResumeLayout();
            container.LeftToolStripPanel.ResumeLayout();
            container.RightToolStripPanel.ResumeLayout();
            container.BottomToolStripPanel.ResumeLayout();
            container.ResumeLayout();
        }
    }
    [Serializable]
    public class ToolStripLayout
    {
        private string _Name = null;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private Point _Location = new Point();
        public Point Location
        {
            get
            {
                return _Location;
            }
            set
            {
                _Location = value;
            }
        }
        private LocationTypes _Type = LocationTypes.Top;
        public LocationTypes Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
            }
        }
        //
        public void From(ToolStripContainer container, ToolStrip toolstrip)
        {
            if (container.TopToolStripPanel.Controls.Contains(toolstrip))
            {
                _Type = LocationTypes.Top;
            }
            else if (container.LeftToolStripPanel.Controls.Contains(toolstrip))
            {
                _Type = LocationTypes.Left;
            }
            else if (container.BottomToolStripPanel.Controls.Contains(toolstrip))
            {
                _Type = LocationTypes.Bottom;
            }
            else if (container.RightToolStripPanel.Controls.Contains(toolstrip))
            {
                _Type = LocationTypes.Right;
            }
            _Location = toolstrip.Location;
        }
        public void To(ToolStripContainer container, ToolStrip toolstrip)
        {
            toolstrip.Location = _Location;
            if (_Type == LocationTypes.Top)
            {
                if (container.LeftToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.LeftToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.BottomToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.BottomToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.RightToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.RightToolStripPanel.Controls.Remove(toolstrip);
                }
                container.TopToolStripPanel.Controls.Add(toolstrip);
            }
            else if (_Type == LocationTypes.Left)
            {
                if (container.BottomToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.BottomToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.RightToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.RightToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.TopToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.TopToolStripPanel.Controls.Remove(toolstrip);
                }
                container.LeftToolStripPanel.Controls.Add(toolstrip);
            }
            else if (_Type == LocationTypes.Bottom)
            {
                if (container.LeftToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.LeftToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.TopToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.TopToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.RightToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.RightToolStripPanel.Controls.Remove(toolstrip);
                }
                container.BottomToolStripPanel.Controls.Add(toolstrip);
            }
            else if (_Type == LocationTypes.Right)
            {
                if (container.LeftToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.LeftToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.BottomToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.BottomToolStripPanel.Controls.Remove(toolstrip);
                }
                else if (container.TopToolStripPanel.Controls.Contains(toolstrip))
                {
                    container.TopToolStripPanel.Controls.Remove(toolstrip);
                }
                container.RightToolStripPanel.Controls.Add(toolstrip);
            }
            toolstrip.Location = _Location;
        }
    }
    public enum LocationTypes
    {
        Top = 0,
        Left,
        Bottom,
        Right
    }

ToolStripLayoutCollection.From

保存工具栏布局

ToolStripLayoutCollection.To

还原工具栏布局

原文地址:https://www.cnblogs.com/jiangu66/p/3217574.html