Win Form 的 Splitter 使用心得与技巧

 更新时间:2007年04月13日 00:00:00   作者:  
今天作个分析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# 中 List 与 List 多层嵌套不改变原值的实现方法(深度复制)

    C# 中 List 与 List 多层嵌套不改变原值的实现方法(深度复制)

    这篇文章主要介绍了C# 中 List 与 List 多层嵌套不改变原值的实现方法,使用 BinaryFormatter 将原始 List 序列化为字节流,然后再反序列化得到新的 List,实现了深度复制,需要的朋友可以参考下
    2024-03-03
  • C#实现Winform小数字键盘模拟器

    C#实现Winform小数字键盘模拟器

    本文主要介绍了C#实现Winform小数字键盘模拟器,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C# TrackBar拖动条改变滑块颜色

    C# TrackBar拖动条改变滑块颜色

    这篇文章主要为大家详细介绍了C# TrackBar拖动条改变滑块颜色,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C# 调用exe传参,并获取打印值的实例

    C# 调用exe传参,并获取打印值的实例

    这篇文章主要介绍了C# 调用exe传参,并获取打印值的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#获取客户端相关信息实例总结

    C#获取客户端相关信息实例总结

    这篇文章主要介绍了C#获取客户端相关信息的方法,以实例形式总结了C#获取客户端IP地址、网络连接、硬件信息等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • DevExpress实现根据行,列索引来获取RepositoryItem的方法

    DevExpress实现根据行,列索引来获取RepositoryItem的方法

    这篇文章主要介绍了DevExpress实现根据行,列索引来获取RepositoryItem的方法,需要的朋友可以参考下
    2014-08-08
  • C#中System.IO.Pipelines库的使用详解

    C#中System.IO.Pipelines库的使用详解

    System.IO.Pipelines 是一个库,旨在使在 .NET 中执行高性能 I/O 更加容易,本文主要为大家详细介绍了System.IO.Pipelines具体使用方法,感兴趣的可以了解下
    2023-12-12
  • Unity UGUI教程之实现滑页效果

    Unity UGUI教程之实现滑页效果

    使用UGUI提供的ScrollRect和ScrollBar组件实现基本滑动以及自己控制每次移动一页来达到滑页的效果。具体实现思路请参考下本教程
    2016-04-04
  • C#比较时间大小的方法总结

    C#比较时间大小的方法总结

    在本篇内容里小编给大家分享的是关于C#比较时间大小的方法总结,对此有需要的朋友们可以学习下。
    2018-12-12
  • Unity3D实现物体旋转缩放移动效果

    Unity3D实现物体旋转缩放移动效果

    这篇文章主要为大家详细介绍了Unity3D实现物体旋转缩放移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02

最新评论