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

Adding designable borders to user controls

Rate me:
Please Sign up or sign in to vote.
4.62/5 (22 votes)
8 Dec 20038 min read 145.5K   2K   45  
A tutorial on adding design-time enabled borders to user controls. Covers topics including atributes, interop, and custom overrides
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace BorderControls
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	public class SimpleControl : System.Windows.Forms.UserControl
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SimpleControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label
			// 
			this.label.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.label.Location = new System.Drawing.Point(8, 16);
			this.label.Name = "label";
			this.label.Size = new System.Drawing.Size(48, 23);
			this.label.TabIndex = 0;
			this.label.Text = "Simple";
			this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// SimpleControl
			// 
			this.Controls.Add(this.label);
			this.Name = "SimpleControl";
			this.Size = new System.Drawing.Size(64, 56);
			this.ResumeLayout(false);

		}
		#endregion

		private System.Windows.Forms.Label label;

		// Constant taken from WinUser.h
		private const int WS_EX_CLIENTEDGE = 0x00000200;

		// Provide window style constants to enable control border
		protected override CreateParams CreateParams
		{
			get
			{
				CreateParams p = base.CreateParams;			// Take the default params from the base class
				p.ExStyle = p.ExStyle | WS_EX_CLIENTEDGE;	// Add the extended "3d sunken border" style
				return p;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Jason Dorie is currently employed with Electronic Arts BlackBox in Vancouver, B.C. He does a variety of programming tasks including writing tools for creating games, 3D rendering, data and video compression, and data processing pipelines. He can often be found lurking on XBox live under the pseudonym "djMidnight".

Comments and Discussions