Win Form 的 Splitter 使用心得与技巧

所属分类: 软件编程 / C#教程 阅读数: 64
收藏 0 赞 0 分享
今天作个分析html代码,然后再批量下载的程序,其中用到 Splitter (分割条),编译程序后,发现分割条不起作用,拖动分割条的时候,相邻的两个 Panel 没有变换大小。为这个几乎花了一天时间,也没找到原因。包括到其他机子上测试。
后来,再次作一个完全独立的测试项目,发现 Splitter 的使用有个算是 bug 的问题,如果你首先放两个 Panel ,然后再放一个 Splitter 。(注意这时候的次序)就会产生我上面出现的问题。这时候代码中的 InitializeComponent 函数部分代码如下:
复制代码 代码如下:

private void InitializeComponent() 

// 
// ... 其他代码 
// 
this.panel1 = new System.Windows.Forms.Panel(); 
this.panel2 = new System.Windows.Forms.Panel(); 
this.splitter1 = new System.Windows.Forms.Splitter(); 
this.panel2.SuspendLayout(); 
this.SuspendLayout(); 
// 
// ... 其他代码 
// 
//  
// panel1 
//  
this.panel1.Dock = System.Windows.Forms.DockStyle.Left; 
this.panel1.Location = new System.Drawing.Point(0, 42); 
this.panel1.Name = "panel1"; 
this.panel1.Size = new System.Drawing.Size(120, 209); 
this.panel1.TabIndex = 6; 
this.panel1.Resize += new System.EventHandler(this.panel2_Resize); 
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint); 
//  
// panel2 
//  
this.panel2.Controls.Add(this.splitter1); 
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill; 
this.panel2.Location = new System.Drawing.Point(120, 42); 
this.panel2.Name = "panel2"; 
this.panel2.Size = new System.Drawing.Size(328, 209); 
this.panel2.TabIndex = 7; 
this.panel2.Resize += new System.EventHandler(this.panel2_Resize); 
this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint); 
//  
// splitter1 
//  
this.splitter1.BackColor = System.Drawing.SystemColors.Desktop; 
this.splitter1.Location = new System.Drawing.Point(0, 0); 
this.splitter1.Name = "splitter1"; 
this.splitter1.Size = new System.Drawing.Size(3, 209); 
this.splitter1.TabIndex = 0; 
this.splitter1.TabStop = false; 
//  
// Form1 
//  
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
this.ClientSize = new System.Drawing.Size(448, 273); 
this.Controls.Add(this.panel2); 
this.Controls.Add(this.panel1); 
this.Controls.Add(this.toolBar1); 
this.Controls.Add(this.statusBar1); 
this.Name = "Form1"; 
this.Text = "站点下载工具 2003年9月21日"; 
this.panel2.ResumeLayout(false); 
this.ResumeLayout(false); 


注意:这时候的代码中的顺序。这时候,程序的执行是有问题的。分隔条会不起作用。
但是如果你把这三个控件放入顺序修改为下面的顺序就没有问题了。
1、放入一个 Panel 比如:panel1 然后设置他的 Dock 属性为:Left; 
2、放入一个 Splitter 比如:splitter1 设置它的背景颜色为一个特殊的颜色,便于看执行效果;
3、放入一个 Panel 比如:panel2 然后设置他的 Dock 属性为:Fill; 
4、编译执行程序,这时候就没有问题了。
这时候正确的代码应该是:( InitializeComponent 函数部分) 
 
复制代码 代码如下:

private void InitializeComponent() 

// 
// ... 其他代码 
// 
this.panel1 = new System.Windows.Forms.Panel(); 
this.panel2 = new System.Windows.Forms.Panel(); 
this.splitter1 = new System.Windows.Forms.Splitter(); 
this.panel2.SuspendLayout(); 
this.SuspendLayout(); 
// 
// ... 其他代码 
// 
//  
// panel1 
//  
this.panel1.Dock = System.Windows.Forms.DockStyle.Left; 
this.panel1.Location = new System.Drawing.Point(0, 42); 
this.panel1.Name = "panel1"; 
this.panel1.Size = new System.Drawing.Size(200, 209); 
this.panel1.TabIndex = 6; 
//  
// panel2 
//  
this.panel2.Controls.Add(this.splitter1); 
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill; 
this.panel2.Location = new System.Drawing.Point(200, 42); 
this.panel2.Name = "panel2"; 
this.panel2.Size = new System.Drawing.Size(248, 209); 
this.panel2.TabIndex = 7; 
//  
// splitter1 
//  
this.splitter1.BackColor = System.Drawing.SystemColors.Desktop; 
this.splitter1.Location = new System.Drawing.Point(0, 0); 
this.splitter1.Name = "splitter1"; 
this.splitter1.Size = new System.Drawing.Size(3, 209); 
this.splitter1.TabIndex = 0; 
this.splitter1.TabStop = false; 
//  
// Form1 
//  
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
this.ClientSize = new System.Drawing.Size(448, 273); 
this.Controls.Add(this.panel2); 
this.Controls.Add(this.panel1); 
this.Controls.Add(this.toolBar1); 
this.Controls.Add(this.statusBar1); 
this.Menu = this.mainMenu1; 
this.Name = "Form1"; 
this.Text = "站点下载工具 2003年9月21日"; 
this.Load += new System.EventHandler(this.Form1_Load); 
this.panel2.ResumeLayout(false); 
this.ResumeLayout(false); 

更多精彩内容其他人还在看

C#中Datetimepicker出现问题的解决方法

这篇文章主要给大家介绍了关于C#中Datetimepicker出现问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C# SQLite数据库入门使用说明

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#实现批量下载图片到本地示例代码

这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何获取C#中方法的执行时间以及其代码注入详解

这篇文章主要给大家介绍了关于如何获取C#中方法的执行时间以及其代码注入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
收藏 0 赞 0 分享

C#中通过LRU实现通用高效的超时连接探测

这篇文章主要介绍了c#中通过LRU实现通用高效的超时连接探测,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#程序启动项的设置方法

这篇文章主要为大家详细介绍了C#程序启动项的设置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

c#爬虫爬取京东的商品信息

这篇文章主要给大家介绍了关于利用c#爬虫爬取京东商品信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#随机数生成字母金字塔

这篇文章主要为大家详细介绍了C#随机数生成字母金字塔,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现窗体中的悬浮按钮

这篇文章主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多