问题现象

当多个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. springMVC 获取本地项目路径 及后整理上传文件的方法

    String path=request.getSession().getServletContext().getRealPath("upload/img/product"); // ...

  2. 【solr这四个主题】在Tomcat 部署Solr4.x

    1.安装Tomcat (1)下载并解压缩到/opt/tomcat在 # cd /opt/jediael # tar -zxvf apache-tomcat-7.0.54.tar.gz # mv apa ...

  3. 【Java&amp;Android开源库代码分析】のandroid-async-http の开盘

          在<[Java&Android开源库代码剖析]のandroid-smart-image-view>一文中我们提到了android-async-http这个开源库,本文正 ...

  4. :link,:visited,:focus,:hover,:active详解

    原文::link,:visited,:focus,:hover,:active详解 CSS 又名 层叠样式表,所谓层叠,就是后面的样式会覆盖前面的样式,所以在样式表中,各样式排列的顺序很有讲究.   ...

  5. WebView Android 调用js且须要获取返回结果

    Android webView调用js方法非常easy, webView.loadUrl("javascrpt:yourFunction()"); 可是此方法没有办法获取返回结果 ...

  6. 使用 C# 进行 Outlook 2003 编程

    原文:使用 C# 进行 Outlook 2003 编程 摘要: 本文介绍了 Microsoft Outlook 2003 对象模型介,并探讨了如何使用 C# 编程语言生成 Outlook 识别的应用程 ...

  7. c# 委托详解

    1.委托声明 2.委托入门实例 namespace ConsoleApplication1 { public delegate void methodDelegate(string str); cla ...

  8. iSwifting社区【www.iSwifting.com】招聘版主

    iSwifting社区[www.iSwifting.com]昨天找来几个主持人.我已经被任命为他们的任务领域. 他们有百度的project噢,还有cocoaChina问答区的活跃人物! 欢迎大家一起学 ...

  9. HTML 5 在Web SQL 使用演示样本

    Web sql 这是一个模拟数据库浏览器.可以使用JS操作SQL完成数据读取和写入,但是这件事情并不多,现在支持的浏览器,而其W3C规格已经停止支持.外形似它的前景不是很亮. W3C 规范:http: ...

  10. Android-异步图像装载机

    在ListView加载图像是非常常见的场景,图像加载几个要求满足以下的: (1)是否画面位于网络或本地上,装载不应同步.但应该异步加载,例如,使用AsyncTask. (2)为了避免重复下载图片和网页 ...