Click here to Skip to main content
13,044,705 members (62,893 online)
Click here to Skip to main content
Add your own
alternative version

Stats

96.9K views
54 bookmarked
Posted 17 Aug 2003

Office 2003 Line Control

, 21 Aug 2003
Rate this:
Please Sign up or sign in to vote.
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:

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.

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:

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)

Share

About the Author

Rob Tomson
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionLicense Pin
Member 896106528-May-12 23:18
memberMember 896106528-May-12 23:18 
AnswerRe: License Pin
Rob Tomson29-May-12 10:52
memberRob Tomson29-May-12 10:52 
GeneralWell Done Pin
ME-Mike Elliott27-Jan-06 22:07
memberME-Mike Elliott27-Jan-06 22:07 
GeneralIntegration Pin
M Y David16-Dec-04 2:55
sussM Y David16-Dec-04 2:55 
GeneralComplex Line Choice Pin
PoRniK [yoda]3-Sep-03 10:00
sussPoRniK [yoda]3-Sep-03 10:00 
GeneralRe: Complex Line Choice Pin
""3-Sep-03 14:32
member""3-Sep-03 14:32 
GeneralRe: Complex Line Choice Pin
PoRniK4-Sep-03 10:47
memberPoRniK4-Sep-03 10:47 
GeneralVisual Styles Pin
Heath Stewart18-Aug-03 4:52
editorHeath Stewart18-Aug-03 4:52 
GeneralRe: Visual Styles Pin
""19-Aug-03 6:32
member""19-Aug-03 6:32 
GeneralRe: Visual Styles Pin
Heath Stewart19-Aug-03 8:04
editorHeath Stewart19-Aug-03 8:04 
GeneralRe: Visual Styles Pin
""19-Aug-03 17:17
member""19-Aug-03 17:17 
GeneralRe: Visual Styles Pin
Heath Stewart19-Aug-03 18:32
editorHeath Stewart19-Aug-03 18:32 
GeneralRe: Visual Styles Pin
""19-Aug-03 20:24
member""19-Aug-03 20:24 
GeneralRe: Visual Styles Pin
""19-Aug-03 20:37
member""19-Aug-03 20:37 
GeneralRe: Visual Styles Pin
Heath Stewart20-Aug-03 2:16
editorHeath 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Aug 2003
Article Copyright 2003 by Rob Tomson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid