20

I want to create a simple 3D line in a WinForms application to improve visual arrangement of my form layout. This line is exacly like the line in About Windows dialog (can be opened in Windows Explorer -> Help -> About Windows).

An example be checked

about

The last line (3D) is the one I want, not the first one.

How can this be done in C# or Visual Basic (.NET)?

1
  • Please use Imgur for hosting images as supported by he edit functionality. Also note that hosts other than stack.imgur.com might be blocked by many organizations. Sep 27, 2022 at 12:40

6 Answers 6

63

Add a Label control with a 3D border and with no text then set the height to 2.

var separator = new Label();
separator.BorderStyle = BorderStyle.Fixed3D;
separator.Height = 2;
0
9

If you use SysInternals' ZoomIt utility, you can see that this is simply two lines. A dark gray one above a white one. Drawing lines is simple enough with Graphics.DrawLine(), you just need to make sure you pick a dark color that work well with the form's BackColor. That isn't always battleship gray if the user selected another theme. Which makes the GroupBox trick fall flat.

This sample code is serviceable:

    protected override void OnPaint(PaintEventArgs e) {
        Color back = this.BackColor;
        Color dark = Color.FromArgb(back.R >> 1, back.G >> 1, back.B >> 1);
        int y = button1.Bottom + 20;
        using (var pen = new Pen(dark)) {
            e.Graphics.DrawLine(pen, 30, y, this.ClientSize.Width - 30, y);
        }
        e.Graphics.DrawLine(Pens.White, 30, y+1, this.ClientSize.Width - 30, y+1);
    }

Note the use of button1 in this code, there to make sure the line is drawn at the right height, even when the form is rescaled. Pick your own control as a reference for the line.

4

One way is to create a group box with no label and height 0 (or is it 1, don't quite remember) - I know I've used that trick before, even if it feels a bit hacky :-)

1
  • 1
    Hi @Tor, I tried that once, but if user uses skins in Windows, this can make the line look very weird!!! I`m looking for and alternative to this technique.
    – RHaguiuda
    Jun 21, 2010 at 18:26
4

I too have used the GroupBox hack and it's got the benefit of styling itself based on the OS border theme.

There is also a Line class in the VB Power Packs control collection. There's a few other goodies in there that we've used too.

Edit: Here's my Seperator class for drawing horizontal line using the method mentioned above.

public class Separator : GroupBox
{
    // Methods
    protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
    {
        base.SetBoundsCore(x, y, width, 3, specified);
    }

    // Properties
    [DefaultValue("")]
    public override string Text
    {
        get
        {
            return string.Empty;
        }
        set
        {
        }
    }
}
3

You can get a line separator effect by adding a Label and setting its text as underscores "_"

0

I wrote a custom control just for this purpose. You can install the control suite from from NuGet:

Install-Package ALMSTWKND -Version 1.0.0

After installation, it will be added to the Toolbox pane.

1
  • This is basically a URL-only answer and it is considered a low-quality post and might be deleted., Please add more details and a summary in order to help other users. Sep 27, 2022 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.