关于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 ...
随机推荐
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- AngularJS应用开发思维之1:声明式界面
这篇博客之前承接上一篇:http://www.cnblogs.com/xuema/p/4335180.html 重写示例:模板.指令和视图 AngularJS最显著的特点是用静态的HTML文档,就可以 ...
- php中utf8 与utf-8
原文:php中utf8 与utf-8 相信很多程序员刚开始也会有这样的疑惑,如题,我也是. 其实,他们可以这样来区分. 一.在php和html中设置编码,请尽量统一写成“UTF-8”,这才 ...
- IOS中TableView的用法
一.UITableView 1.数据展示的条件 1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITable ...
- openstack shelve/unshelve/stop浅析
声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! stop的虚拟机仅仅是将虚拟 ...
- android 当目录路径从n层按back键退回到n-19层的时候,file manager自己主动退出
当目录路径从n层按back键退回到n-19层的时候,file manager自己主动退出,比方在63层按back 键退回到44层的时候,file manager自己主动退出. 1.FileMana ...
- C#实现Web文件上传的两种方法
1. C#实现Web文件的上传 在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛. 那么使用C#如何实现文件上传的功能呢?下面笔 ...
- CORS(跨域资源共享)跨域问题及解决
当使用ajax跨域请求时,浏览器报错:XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin.肯 ...
- sugarcrm关于邮件设置几个不好理解的地方
陈沙克日志 把我的过程记录下来,以免以后忘了 2008-06-11 12:32 sugarcrm关于邮件设置几个不好理解的地方 最近看sugarcrm的使用,别的基本使用,没有什么问题,几天就 ...
- 查看mysql状态常用命令
最近服务器上mysql有些奇奇怪怪的问题,可惜我不是专业的dba,为了加深自己对mysql的了解,先从基础的查看mysql状态命令看起吧. 命令: show status; 命令: show stat ...