Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Article

Office 2003 Line Control

Rate me:
Please Sign up or sign in to vote.
2.90/5 (10 votes)
21 Aug 2003CPOL1 min read 104K   2.5K   54   17
A GroupBox-like single line similar to that found in Office 2003.

Offce 2003 Line Control

Introduction

These controls mimic that of Office 2003 where they have a Label or GroupBox with a line next to it This is my first control so please comment and give suggestions on how to do things better, etc.

Using the Code

Basically what I did was override the OnPaint events to get the desired look like this:

C#
protected override voi OnPaint(System.Windows.Forms.PaintEventArgs e)
{ 
    Graphics g = e.Graphics; SolidBrush brush =
        new SolidBrush(this.textColor);
    Pen pen =new Pen(this.lineColor);
    SizeF sizef = g.MeasureString(this.Text, this.Font);
    PointF pointf = new PointF(0, 0);
  
    g.DrawString(this.Text, this.Font, brush, pointf);
    g.DrawLine(pen, sizef.Width + 8, sizef.Height / 2, this.Width - 8,
        sizef.Height / 2);
}

After that I created a custom designer so I can control what it looks like in design mode.

C#
public class GroupBoxLineDesigner : ParentControlDesigner
{
  protected override void PostFilterProperties(IDictionary id)
  {
    id.Remove("FlatStyle");
    base.PostFilterProperties(id);
  }

  protected override void OnPaintAdornments(PaintEventArgs pe)
  {
    this.DrawBorder(pe.Graphics);
    base.OnPaintAdornments(pe);
  }

  private void DrawBorder(Graphics graphics)
  {
    Control control1 = base.Control;
    Rectangle rectangle1 = control1.ClientRectangle;
    Color borderColor;
    Color backColor = control1.BackColor;
    if (((double) backColor.GetBrightness()) < 0.5)
    {
      borderColor = ControlPaint.Light(control1.BackColor);
    }
    else
    {
      borderColor = ControlPaint.Dark(control1.BackColor);
    }
    Pen pen = new Pen(borderColor);
    pen.DashPattern = new float[] {3, 1};
    rectangle1.Width--;
    rectangle1.Height--;
    graphics.DrawRectangle(pen, rectangle1);
    pen.Dispose();
  } 
}

[Designer(typeof(GroupBoxLineDesigner))]
...

And here's all code put together:

C#
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Newco.Controls
{
  /// <SUMMARY>
  /// Summary description for GroupBoxLine.
  /// </SUMMARY>
  
  public class GroupBoxLineDesigner : ParentControlDesigner
  {
    protected override void PostFilterProperties(IDictionary id)
    {
      id.Remove("FlatStyle");
      base.PostFilterProperties(id);
    }
    protected override void OnPaintAdornments(PaintEventArgs pe)
    {
      this.DrawBorder(pe.Graphics);
      base.OnPaintAdornments(pe);
    }

    private void DrawBorder(Graphics graphics)
    {
      Control control1 = base.Control;
      Rectangle rectangle1 = control1.ClientRectangle;
      Color borderColor;
      Color backColor = control1.BackColor;

      if (((double) backColor.GetBrightness()) < 0.5)
      {
        borderColor = ControlPaint.Light(control1.BackColor);
      }
      else
      {
        borderColor = ControlPaint.Dark(control1.BackColor);
      }
      Pen pen = new Pen(borderColor);
      pen.DashPattern = new float[] {3, 1};
      rectangle1.Width--;
      rectangle1.Height--;
      graphics.DrawRectangle(pen, rectangle1);
      pen.Dispose();
    } 
  }

  [Designer(typeof(GroupBoxLineDesigner))]
  [ToolboxBitmap(typeof(GroupBoxLine), @"Toolbox_GroupBoxLine.bmp")]
  public class GroupBoxLine : System.Windows.Forms.GroupBox
  {
    protected Color lineColor;
    protected Color textColor;
    private System.ComponentModel.Container components = null;

    // Add the 'LineColor' property
    [Description("The line color."),
    Category("Appearance")]
    public Color LineColor
    {
      get
      {
        return this.lineColor;
      }
      set
      {
        this.lineColor = value;
        Invalidate();
      }
    }

    // Add the 'TextColor' property
    [Description("The text color."),
    Category("Appearance")]
    public Color TextColor
    {
      get
      {
        return this.textColor;
      }
      set
      {
        this.textColor = value;
        Invalidate();
      }
    }

    public GroupBoxLine()
    {
      this.textColor = Color.FromArgb(0, 70, 213);
      this.lineColor = Color.FromArgb(208, 208, 191);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      SolidBrush brush = new SolidBrush(this.textColor);
      Pen pen = new Pen(this.lineColor);
      SizeF sizef = g.MeasureString(this.Text, this.Font);
      PointF pointf = new PointF(0, 0);

      g.DrawString(this.Text, this.Font, brush, pointf);
      g.DrawLine(pen, sizef.Width, sizef.Height / 2, this.Width,
        sizef.Height / 2);

      brush.Dispose();
      pen.Dispose();
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if( components != null )
          components.Dispose();
      }
      base.Dispose( disposing );
    }
  }
}

Points of Interest

Like I said before - this is my first control and I think it turned out rather well, but I know there's better ways to do this I've also noticed that if you change the FlatStyle property then the control goes back to the original form. What can I do to fix this? And is the GroupBox is hard to see in design mode so is there a way to draw a rectangle in design mode only? Thanks for helping me out everyone and I hope I learn something so I can post more articles to codeproject. Thanks, Rob Tomson

Updates

  • 08/18//2003: Initial Release
  • 08/20/2003: Added a 'design time' interface.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicense Pin
Member 896106528-May-12 23:18
Member 896106528-May-12 23:18 
The article does not include any specified license, so, can you tell what license should apply?
Is it The Code Project Open License (CPOL)?

Thanks
Cornelia
AnswerRe: License Pin
Rob Tomson29-May-12 10:52
Rob Tomson29-May-12 10:52 
GeneralWell Done Pin
ME-Mike Elliott27-Jan-06 22:07
ME-Mike Elliott27-Jan-06 22:07 
GeneralIntegration Pin
Yossef16-Dec-04 2:55
Yossef16-Dec-04 2:55 
GeneralComplex Line Choice Pin
PoRniK3-Sep-03 10:00
PoRniK3-Sep-03 10:00 
GeneralRe: Complex Line Choice Pin
Member 5092623-Sep-03 14:32
Member 5092623-Sep-03 14:32 
GeneralRe: Complex Line Choice Pin
PoRniK4-Sep-03 10:47
PoRniK4-Sep-03 10:47 
GeneralVisual Styles Pin
Heath Stewart18-Aug-03 4:52
protectorHeath Stewart18-Aug-03 4:52 
GeneralRe: Visual Styles Pin
Member 50926219-Aug-03 6:32
Member 50926219-Aug-03 6:32 
GeneralRe: Visual Styles Pin
Heath Stewart19-Aug-03 8:04
protectorHeath Stewart19-Aug-03 8:04 
GeneralRe: Visual Styles Pin
Member 50926219-Aug-03 17:17
Member 50926219-Aug-03 17:17 
GeneralRe: Visual Styles Pin
Heath Stewart19-Aug-03 18:32
protectorHeath Stewart19-Aug-03 18:32 
GeneralRe: Visual Styles Pin
Member 50926219-Aug-03 20:24
Member 50926219-Aug-03 20:24 
GeneralRe: Visual Styles Pin
Member 50926219-Aug-03 20:37
Member 50926219-Aug-03 20:37 
GeneralRe: Visual Styles Pin
Heath Stewart20-Aug-03 2:16
protectorHeath Stewart20-Aug-03 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.