Quick Windows Forms Control… a Label with a 3D Line…

I was mocking up a UI yesterday, and I wanted to produce an interface that looked like Front Page’s “New from Template” dialog…

But I couldn’t easily replicate those little dividers (for Options, Description, Preview), so I created a “DividerLabel” with about 10 minutes of VB.NET code (2003)…

And, if you are so inclined, you can set the text to nothing and you have a line!

The Code

 Imports System.Drawing Imports System.Windows.Forms Public Class DividerLabel Inherits System.Windows.Forms.Label Dim m_spacing As Integer Dim m_borderStyle As Border3DStyle = Border3DStyle.Etched <System.ComponentModel.Category("Appearance")> _ Public Property LineStyle() As Border3DStyle Get Return m_borderStyle End Get Set(ByVal Value As Border3DStyle) If Value <> m_borderStyle Then m_borderStyle = Value Me.Invalidate() End If End Set End Property <System.ComponentModel.Category("Appearance")> _ Public Property Spacing() As Integer Get Return m_spacing End Get Set(ByVal Value As Integer) If Value <> m_spacing Then m_spacing = Value Me.Invalidate() End If End Set End Property Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics Dim f As Font = Me.Font Dim b As Brush = New SolidBrush(Me.ForeColor) Dim sf As StringFormat = StringFormat.GenericTypographic Dim labelBounds As New RectangleF(0, 0, Me.Width, Me.Height) Dim textSize As SizeF = g.MeasureString(Me.Text, f, Me.Width) g.DrawString(Me.Text, f, b, 0, 0, sf) If textSize.Width + Spacing < Me.Width Then Dim startingPoint As Point startingPoint.X = textSize.Width + Spacing startingPoint.Y = textSize.Height \ 2 ControlPaint.DrawBorder3D(g, startingPoint.X, _ startingPoint.Y, _ Me.Width - startingPoint.X, _ 5, m_borderStyle, Border3DSide.Top) End If End Sub Public Sub New() Me.SetStyle(ControlStyles.DoubleBuffer, True) Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True) Me.SetStyle(ControlStyles.ResizeRedraw, True) End Sub End Class 

Quick Windows Forms Control… a Label with a 3D Line…

10 Responses

  1. I’ve had similar experiences. It’s funny how such a small detail can drive a person nuts!

    That’s a cool little control though, and it demonstrates just how much you can do without too much code.

    josh April 14, 2005 at 4:41 pm #
  2. fsq… you could do that, but then you’d have to write code to handle repositioning/resizing when your text changed, the font changed, or the user’s resolution changed… might not apply in your apps though, but the resolution change (dpi and/or Large Fonts) is an issue that I see quite often.

    Also… it doesn’t look quite the same, but it is pretty close… all depends how picky you are šŸ™‚

    Duncan Mackenzie April 14, 2005 at 6:05 pm #
  3. Quick, run and put a software patent on it!

    Brian April 14, 2005 at 6:17 pm #
  4. Is it me or is the variable in this declaration unused?
    “Dim labelBounds As New RectangleF(0, 0, Me.Width, Me.Height)”

    I was interested in how the code deals with word-wrapping. I’m still not sure so I’ll have to try it out.

    Scott April 14, 2005 at 6:23 pm #
  5. Scott, you caught me… That variable was something I was adding to handle wrapping…. then I went to lunch šŸ™‚

    These lines can be swapped in for what is in the original post to provide word wrap….

    Dim labelBounds As New RectangleF(0, 0, Me.Width, Me.Height)
    Dim textSize As SizeF = g.MeasureString(Me.Text, f, Me.Width)

    g.DrawString(Me.Text, f, b, labelBounds, sf)

    Note that the line is always drawn in the middle (vertical) of the full text block, so in this case it will be drawn in the middle of the two lines (or middle of the 2nd line if there are 3, etc…)

    Duncan Mackenzie April 14, 2005 at 8:46 pm #
  6. couldn’t you just make a 1px-height group box with no caption and stick a label out to the left of it?

    Thats what I always do.

    Of course, I’m a bad programmer.

    fsg April 14, 2005 at 5:58 pm #
  7. Okay, I just wanted to make sure I’m not a complete idiot. That IS how alot of people do it in .Net, isn’t it?

    Btw, good article. I’ll probably steal your code and throw it in my controls lib.

    fsg April 14, 2005 at 6:19 pm #
  8. Maybe I’m just stupid, but I can’t get this too work. It doesn’t display a line for me.

    jf April 15, 2005 at 12:37 pm #
  9. made this type of control year ago in vb 6 and vb.not,

    abhishek April 19, 2005 at 9:01 pm #
  10. made this type of control year ago in vb 6 and vb.not,

    ab April 19, 2005 at 9:02 pm #

Leave a Reply