2011-05-16 86 views

回答

9

尽管这已经回答了,我发现下面是我所需要的部分基于smoore的答案。

创建一个新的控件。编辑代码如下所示:

public partial class Line : Label 
{ 
    public override bool AutoSize 
    { 
     get 
     { 
      return false; 
     } 
    } 

    public override Size MaximumSize 
    { 
     get 
     { 
      return new Size(int.MaxValue, 2); 
     } 
    } 

    public override Size MinimumSize 
    { 
     get 
     { 
      return new Size(1, 2); 
     } 
    } 

    public override string Text 
    { 
     get 
     { 
      return ""; 
     } 
    } 

    public Line() 
    { 
     InitializeComponent(); 
     this.AutoSize = false; 
     this.Height = 2; 
     this.BorderStyle = BorderStyle.Fixed3D; 
    } 
} 

替换你想控制的类名行。这将放置一个分隔符,允许您在设计器中调整大小,并禁用添加文本,更改autosize强制大小的高度为2,宽度为任何您想要的,并禁用添加文本。

4

它实际上并没有包含在标准的控件集合中(很确定它曾经在当天回来!),但是通过使用没有文本和高度为1px的GroupBox,您可以轻松创建自己的或作弊。

用户控件提供同样的事情:(由我不写,来源:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/0d4b986e-3ed0-4933-a15d-4b42e02005a7/

public partial class LineSeparator:UserControl 
{ 

    public LineSeparator() 
    { 
     InitializeComponent(); 
     this.Paint += new PaintEventHandler(LineSeparator_Paint); 
     this.MaximumSize = new Size(2000, 2); 
     this.MinimumSize = new Size(0, 2); 
     this.Width = 350; 
    } 

    private void LineSeparator_Paint(object sender, PaintEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     g.DrawLine(Pens.DarkGray, new Point(0, 0), new Point(this.Width, 0)); 
     g.DrawLine(Pens.White, new Point(0, 1), new Point(this.Width, 1)); 
    } 
} 
+1

让你想知道为什么2000年的限制,这甚至不足以涵盖双屏设置。让我想起所罗门的劣质节目。 – Blindy 2011-05-16 19:19:24

+0

我认为懒惰;) – 2011-05-16 19:22:45

16

如果我没有记错的话,这只是一个线路的控制,但我不认为存在控制了。 Here是一种解决方法。

label1.AutoSize = False 
label1.Height = 2 
label1.BorderStyle = BorderStyle.Fixed3D