关于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
还原工具栏布局
关于ToolStrip设置Location无效的问题的更多相关文章
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- 通过inflate获取布局,设置layoutparams无效
给ll——addtiem当设置layoutparams无效时,试着修改上一个布局的属性
- mysql datetime设置now()无效,直接用程序设置默认值比较好
mysql datetime设置now()无效的,没有此用法,datetime类型不能设置函数式默认值,只能通过触发器等来搞.想设置默认值,只能使用timestamp类型,然后默认值设置为:CURRE ...
- 用css3动画 @keyframes里设置transform:rotate(); 控制动画暂停和运动用属性:animation-play-state:paused暂停,在微信和safari里设置paused无效,在QQ里是正常的
这几天遇到了两个很奇葩的问题,终于找到原因,趁还记得解决方法,赶紧记下来: 用css3动画 @keyframes里设置transform:rotate(); 控制动画暂停和运动可以用属性:animat ...
- WebStorm在Font设置FontSize无效解决方法
我的WebStorm设置了主题.所以直接在File-Settings-Editor-Font设置了无效.它会提醒你要在主题里面改.主题在哪里呢 找到File-Settings-Editor-Color ...
- bootstrap-multiselect 设置单选无效(设置单选依然是复选框)
bootstrap-multiselect 的使用介绍:https://www.cnblogs.com/landeanfen/p/5013452.html bootstrap-multiselect ...
- 微信小程序的button按钮设置宽度无效
亲,你是不是也遇到了微信小程序的button按钮设置宽度无效.让我来告诉你怎么弄 方法1. 样式中加入!important,即:width: 100% !important; wxss代码示例 1 2 ...
- border在IE6设置transparent无效
在ie6下给border设置transparent是无效的,解决办法如下: _border-color:tomato; /*For IE6-*/ _filter:chroma(color=tomato ...
- 表格单元格td设置宽度无效的解决办法 .
http://zzstudy.offcn.com/archives/11366 在做table页面时,有时对td设置的宽度是无效的,td的宽度始终有内部的内容撑开,可以设置padding,但直接设置w ...
随机推荐
- Largest Rectangular Area in a Histogram
题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...
- MVC验证04-自定义验证规则、日期范围验证
原文:MVC验证04-自定义验证规则.日期范围验证 本文体验范围验证.与本文相关的包括: MVC验证01-基础.远程验证 MVC验证02-自定义验证规则.邮件验证 MVC验证03-自定义验证规 ...
- .NET源代码的内部排序实现
使用JetBrains的DotPeek工具能够方便地查看.net的部分源代码.于是看了一下.NET的内部是怎样实现排序的算法. 在System.Collections.Generic 命名空间下能够看 ...
- Android在WebView上构建Web应用程序
原文链接:http://developer.android.com/guide/webapps/webview.html reference:http://developer.android.com/ ...
- HDU1024 Max Sum Plus Plus(DP)
状态:d(i,j)它代表前j划分数i部并且包括第一j最佳结果时的数.g(i,j)表示前j划分数i最好的结果时,段,g(m,n)结果,需要. 本题数据较大.需採用滚动数组.注意:这题int类型就够用了, ...
- 校验、AJAX与过滤器
随笔- 65 文章- 1 评论- 343 ASP.Net MVC开发基础学习笔记:四.校验.AJAX与过滤器 一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations( ...
- linux下搭建SVN服务器完全手册-很强大!!!!!
系统环境 RHEL5.4最小化安装(关iptables,关selinux) + ssh + yum 一,安装必须的软件包. yum install subversion ( ...
- nginx启动,重启,关闭命令
nginx启动,重启,关闭命令 停止操作停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的步骤1:查询nginx主进程号ps -ef | grep nginx在进程列表 ...
- Spring.NET学习
Spring.NET学习笔记——目录(原) 目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) ...
- C#程序员阅读的书籍
推荐几本C#程序员阅读的书籍 楼主这些年一直追随微软技术,也看了不少书籍,整理出一些个人认为不错的经典,推荐给各位阅读,以共同进步. 推荐顺序是由浅入深,深入浅出. <Professiona ...