Horizontal lines

Just saw this post in a newsgroup about how to show the horizontal lines that Microsoft likes to use in their dialogs. A lot of people replied with ideas of using other controls, like panels, to achieve the desired effect – to me, a much simpler and more effective idea is to just create a control myself.

So, the specific style described by the OP seems to be this:

This is what you get by using a label set to a height of 2 and with a BorderStyle of Fixed3D. The OP said explicitely that this was what he wanted, which I find curious because the lines in my Word Options dialog, for instance, look quite different:

So, to account for both needs, I included the etched border as well as the single line border in my sample. I’m sure there are millions of other line styles out there, but these should be simple to include once the framework is there. Here’s the control source code:

  public enum HLineType {
    Etched,
    Single
  }

  public class HorizontalLine : Control {
    public HorizontalLine( ) {
      SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.FixedHeight | 
        ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | 
          ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
        lineType = HLineType.Etched;
    }

    public override System.Drawing.Size GetPreferredSize(Size proposedSize) {
      if (proposedSize == Size.Empty)
        return new Size(150, 10);
      return proposedSize;
    }

    private HLineType lineType;
    [DefaultValue(HLineType.Etched)]
    public HLineType LineType {
      get {
        return lineType;
      }
      set {
        if (lineType != value) {
          lineType = value;
            CreatePen( );
            Invalidate( );
        }
      }
    }

    public override Color ForeColor {
      get {
        return base.ForeColor;
      }
      set {
        base.ForeColor = value;
        CreatePen();
      }
    }

    Pen pen;
    void CreatePen( ) {
      if (pen != null) {
        pen.Dispose( );
        pen = null;
      }

      if (lineType == HLineType.Single)
        pen = new Pen(ForeColor);
    }

    protected override void OnPaint(PaintEventArgs e) {
      base.OnPaint(e);
      switch (lineType) {
        case HLineType.Etched:
          ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, 
            Border3DStyle.Etched, Border3DSide.Top);
          break;
        case HLineType.Single:
          e.Graphics.DrawLine(pen, 0, 0, Width - 1, 0);
          break;
      }
    }
  }

This was done in VS 2005 beta 2. You might have to make minor changes for .NET 1 if needed – I’m not 100% sure about the flags in the SetStyle call, look them up if you need to.

2 Comments on Horizontal lines

  1. The lines I was looking at can be seen at the bottom of the dialog in Visual C# Express here:http://dantup.me.uk/tmp/line.jpgYour solution is certainly more elegant than the Label! I’ll add it to my project! 🙂

    Like

  2. I added “Orientation” type to allow specify “Horizontal” or “Vertical” type into your class:

    http://docs.google.com/leaf?id=0B74Xgeyf2m99ODBlMjkwNDktNzAzYi00YWE4LWJkYTctYWNkMDhiNWJiOWYx&hl=en

    Like

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s