开发者

Windows Forms Separator Control

Where in VS2010 can I find a horizontal separator control, as can be found in Outlook settings (screenshots below)?

https://jira.atlassian.com/secure/attachment/14933/outlook+settings.jpg http://www.keithfimreite.com/Ima开发者_Go百科ges/OutlookSettings3.gif

Note: VB.NET preferred, but C# answers okay.


If I'm not mistaken, that's just a Line control, but I don't think that control exists anymore. Here is a workaround.

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


Even though this has been answered, I found the following to be what I need based partly on smoore's answer.

Create a new control. Edit the code to be the following:

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;
    }
}

Replace Line with the control's class name you want. This will put a separator that will allow you to resize in the designer and disables adding text, changing the autosize forces the size's height to be 2 and width to be whatever you want, and disables adding text.


It's not actually included in the standard set of controls (pretty sure it used to be back in the day!) but you can easily create your own or cheat by using a GroupBox with no text and a height of 1px.

UserControl to provide the same thing: (Not written by me, source: 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));
    }
}


I wrote a custom control just for this purpose. It supports both vertical and horizontal modes. Just install my [small] control suite and drag the separator control onto the form and place it anywhere you want.

Install-Package ALMSTWKND -Version 1.0.0

The controls will show up in the Toolbox pane after installation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜