|
Introduction
While designing a windows form in Visual Studio .NET I always find the standard
Label control difficult to manage and creating ultra high fine layout/UI. The Label
control does not show the border in Visual Studio designer. It?s difficult to guess
the size and placement of all labels in a single look.
Take case of the below UI
I really can?t judge how the labels are placed here. It may be a mess like this
one
This could be overcome by designing simple.. really simple Label by placing just
4 more lines of code to Standard Label. I like the cleanness in below UI. This one
designed using the Label produced after just 4 lines of code.
Source Code
To do this simply draw a rectangle along the sides of Label while it is in design mode. Here is the source code for this Label.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Owf
{
public class MyLabel : Label
{
public MyLabel()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyLabel
//
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyLabel_Paint);
this.ResumeLayout(false);
}
private void MyLabel_Paint(object sender, PaintEventArgs e)
{
if (this.DesignMode)
{
using (Pen pen = new Pen(Color.Gray))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
e.Graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
}
}
}
}
}
|