Win Form 的 Splitter 使用心得与技巧_C#教程

今天作个分析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); 

时间: 2024-08-03 07:47:02

Win Form 的 Splitter 使用心得与技巧_C#教程的相关文章

C#中使用Interlocked进行原子操作的技巧_C#教程

什么是原子操作? 原子(atom)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operation)意为"不可被中断的一个或一系列操作" .在C#中有多个线程同时对某个变量进行操作的时候,我们应该使用原子操作,防止多线程取到的值不是最新的值. 例如:int result = 0; 多线程A正在执行 result(0)+1 多线程B同时执行 result(0)+1 那么最终result的结果是1还是2呢,这个就很难说了.如果在CPU中2个线程同时计算

C#的四个基本技巧_C#教程

1.如果可能尽量使用接口来编程  .NET框架包括类和接口,在编写程序的时候,你可能知道正在用.NET的哪个类.然而,在这种情况下如果你用.NET支持的接口而不是它的类来编程时,代码会变得更加稳定.可用性会更高.请分析下面的代码:  private void LoadList (object [] items, ListBox l)  {  for (int i = 0; i < items.Length;i++)  l.Items.Add (items[i].ToString ());  } 

一些使用THinkPHP的心得和技巧

找了一些使用THinkPHP的心得和技巧,分享给大家甚至一些手册上没提到的,这里也有 约定:1.所有类库文件必须使用.class.php作为文件后缀,并且类名和文件名保持一致2.控制器的类名以http://www.aliyun.com/zixun/aggregation/16339.html">Action为后 缀3.模型的类名以Model为后缀,类名第一个字母须大写4.数据库表名全部采用小写, 如:数据表名: 前缀_表名模型类名: 表名Model 注:这里的表名第一个字母要大写创建对象:

分享有关微博推广的心得和技巧

摘要: 很多做网站优化的站长们都会感觉到,最近一段时间百度搜索引擎的巨大变化.自从淘宝商城动乱后,百度搜索引擎就开始收录淘宝店铺了.不过从最近收录的结果和排名来看,百度的 很多做网站优化的站长们都会感觉到,最近一段时间百度搜索引擎的巨大变化.自从"淘宝商城动乱"后,百度搜索引擎就开始收录淘宝店铺了.不过从最近收录的结果和排名来看,百度的此举应该属于试运行阶段,而且所给予淘宝店铺的权重也是非常低的. 百度搜索引擎的另外一个大变动,那就是开始大量收录微博平台了.新浪,腾讯,网易,搜狐等大型

Win 7/8.10使用命令行配置IP教程

  在Windows系统修改IP地址可以在系统自带的"网络和共享中心"实现,方法很多,不过有些时候可能会因为种种原因而无法进入相应界面,比如各种软件问题或者中毒.在一些情况下用户需要配置静态IP才能够实现正常的网络通讯,在Windows系统中通过命令行修改IP地址是万能的,因此小编这里介绍下使用命令行配置IP方法. Win 7/8.10使用命令行配置IP教程方法不难,适用于Win7/Win8.1/Win10等系统,只需要记住一组命令即可,步骤如下: 1.打开命令提示符,输入netsh后

Win 7系统组策略的几个技巧

第一.限制.隐藏磁盘分区 为了使用方便,我们有时也在多人共用的公用电脑中存放一些属于个人隐私级别的数据资料,为了保证这些数据的安全,可以通过对磁盘分区进行限制或隐藏来实现对数据的保护. 在打开的"组策略"对话框的左侧窗格中依次单击"本地计算机策略→用户配置→管理模板→Windows组件→Windows资源管理器",然后到右边的窗格中双击"防止从'我的电脑'访问驱动器"项,接着在打开的"防止从'我的电脑'访问驱动器的属性"设置窗

关于JavaScript命名空间的一些心得_javascript技巧

最近重构东西,碰到命名空间的设定,搜了一些知识,请教了一些高手,把自己的心得写下来相信大家都知道,window是顶级的,这里就不写window了,先忽略 1:关于顶级 复制代码 代码如下: var ns = ns || {}; 可以看到,其实就是如果发现没有这个对象,就自动创建new Object();如果有,就直接使用这个对象,这样就不会覆盖.2: 第二级,当然也可以在顶级的ns下创建第二级,即 复制代码 代码如下: ns.ModuleClass = {}; 可以看到,在ns下创建了一个类,当

关于编写推广邮件的心得和技巧

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 经过前段时期的邮件推广,已经小有成色,自己的努力得到回报,我看着推广的一个个会员,虽然还不是特别的多,我还是非常的高兴.接下来,我来向大家分享下我的推广邮件编写的心得,和我自己总结的小技巧,希望能对大家有所帮助. 首先是收件人的选择问题,我认为先从自己的QQ好友开始是一件很好的事情,因为这是你手里可以立即就利用的资源.而且,相互之间有相熟性,

Win 2000的五则超酷技巧(1)

技巧 相对于Windows 98系统Windows 2000在系统的稳定性方面大大加强了,相对于Windows XP操作系统Windows 2000在硬件方面的要求没有那么高,但是这款操作系统也有不足之处,本文中我们给大家介绍了Windows 2000的五个应用技巧,对你使用Windows 2000系统有不小的帮助. 1.让Windows 2000也使用"Msconfig" 使用NT内核的Windows 2000没有提供Windows 98/XP中的"系统配置实用程序(Msc