问题现象

当多个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

还原工具栏布局

关于ToolStrip设置Location无效的问题的更多相关文章

  1. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  2. 通过inflate获取布局,设置layoutparams无效

    给ll——addtiem当设置layoutparams无效时,试着修改上一个布局的属性

  3. mysql datetime设置now()无效,直接用程序设置默认值比较好

    mysql datetime设置now()无效的,没有此用法,datetime类型不能设置函数式默认值,只能通过触发器等来搞.想设置默认值,只能使用timestamp类型,然后默认值设置为:CURRE ...

  4. 用css3动画 @keyframes里设置transform:rotate(); 控制动画暂停和运动用属性:animation-play-state:paused暂停,在微信和safari里设置paused无效,在QQ里是正常的

    这几天遇到了两个很奇葩的问题,终于找到原因,趁还记得解决方法,赶紧记下来: 用css3动画 @keyframes里设置transform:rotate(); 控制动画暂停和运动可以用属性:animat ...

  5. WebStorm在Font设置FontSize无效解决方法

    我的WebStorm设置了主题.所以直接在File-Settings-Editor-Font设置了无效.它会提醒你要在主题里面改.主题在哪里呢 找到File-Settings-Editor-Color ...

  6. bootstrap-multiselect 设置单选无效(设置单选依然是复选框)

    bootstrap-multiselect 的使用介绍:https://www.cnblogs.com/landeanfen/p/5013452.html bootstrap-multiselect ...

  7. 微信小程序的button按钮设置宽度无效

    亲,你是不是也遇到了微信小程序的button按钮设置宽度无效.让我来告诉你怎么弄 方法1. 样式中加入!important,即:width: 100% !important; wxss代码示例 1 2 ...

  8. border在IE6设置transparent无效

    在ie6下给border设置transparent是无效的,解决办法如下: _border-color:tomato; /*For IE6-*/ _filter:chroma(color=tomato ...

  9. 表格单元格td设置宽度无效的解决办法 .

    http://zzstudy.offcn.com/archives/11366 在做table页面时,有时对td设置的宽度是无效的,td的宽度始终有内部的内容撑开,可以设置padding,但直接设置w ...

随机推荐

  1. Oracle 免费的数据库

    Oracle 免费的数据库--Database 快捷版 11g 安装使用与"SOD框架"对Oracle的CodeFirst支持 一.Oracle XE 数据库与连接工具安装使用 O ...

  2. 数据库数据导出成XML文件

    在数据库中,怎样把库中的数据导出XML文件, sql语句如下: SELECT *  FROM 表名 FOR XML AUTO, ELEMENTS

  3. Cocos2d-xvision3.0加载失败,和,Vs2012环境搭建

    1.安装好VS2012,下载Cocos2d-x3.0 双击击win32 sln运行VS2012 如果加载失败点击程序运行,输入devenv.exe /resetuserdata 回车,然后再进入VS, ...

  4. log4e插件的安装和使用

    1.首先下载log4e小工具.放入myeclipse10安装文件夹D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropin ...

  5. jqgrid-asp.net-mvc

    jqgrid-asp.net-mvc 你是否使用jqgrid? 你是否想在C#/asp.net mvc中使用jqgrid? 那你很可能曾经为了分析jqgrid的request url用fiddler忙 ...

  6. 4行代码实现js模板引擎

    在平时编码中,经常要做拼接字符串的工作,如把json数据用HTML展示出来,以往字符串拼接与逻辑混在在一起会让代码晦涩不堪,加大了多人协作与维护的成本.而采用前端模板机制就能很好的解决这个问题. 精妙 ...

  7. 快速构建Windows 8风格应用23-App Bar概述及使用规范

    原文:快速构建Windows 8风格应用23-App Bar概述及使用规范 本篇博文主要介绍App Bar概述.App Bar命令组织步骤.App Bar最佳实践.   App Bar概述 Windo ...

  8. MVC 过滤器1

    ASP.NET MVC 过滤器(一) 前言 前面的篇幅中,了解到了控制器的生成的过程以及在生成的过程中的各种注入点,按照常理来说篇幅应该到了讲解控制器内部的执行过程以及模型绑定.验证这些知识了.但是呢 ...

  9. [Java]利用拦截器和自定义注解做登录以及权限验证

    1.自定义注解 需要验证登录的注解 package com.etaofinance.wap.common; import java.lang.annotation.Documented; import ...

  10. GridView使用技巧

    http://yushuir.blog.163.com/blog/static/4346713820081023103937681/