Microsoft KB Archive/316574

= How to set a BorderStyle property for UserControls in Visual C# =

Article ID: 316574

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q316574



IN THIS TASK
SUMMARY
 * Create a Windows Forms Application
 * Customize Form and Control Properties
 * Compile the Project
 * Add a Reference to the UserControl

REFERENCES



SUMMARY
This step-by-step article describes how to set a custom border style for a UserControl object. By default, UserControl objects do not have a BorderStyle property set. The BorderStyle property specifies the border style for controls that have a changeable border.

back to the top

Create a Windows Forms Application

 * 1) Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Control Library under Templates. In the Name text box, type BorderedUserControlSample.

Note In Visual Studio 2005, click Visual C# under Project Types.

back to the top

Customize the Form and the Control Properties
  Copy and paste the following code before the UserControl1 class declaration: class NativeMethods { public const int WS_EX_CLIENTEDGE = unchecked((int)0x00000200); public const int WS_BORDER = unchecked((int)0x00800000); }                     Copy and paste the following member declarations in the UserControl1 class: private BorderStyle borderStyle; private System.Windows.Forms.TextBox textName; private System.Windows.Forms.TextBox textAddress;   Copy and paste the following code in the InitializeComponent method: textName = new System.Windows.Forms.TextBox; textAddress = new System.Windows.Forms.TextBox; textName.Location = new System.Drawing.Point(50, 8); textName.Size = new System.Drawing.Size(100, 20); textName.TabIndex = 0; textAddress.Location = new System.Drawing.Point(50, 32); textAddress.Size = new System.Drawing.Size(100, 20); textAddress.TabIndex = 1; Controls.AddRange(new System.Windows.Forms.Control[] {    textName,     textAddress, }); // Size the user control. Size = new System.Drawing.Size(200, 100);   Copy and paste the following code in UserControl1 class after UserControl1_Load method: protected override CreateParams CreateParams {    get { CreateParams cp = base.CreateParams; cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE); cp.Style &= (~NativeMethods.WS_BORDER);

switch (borderStyle) { case BorderStyle.Fixed3D: cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: cp.Style |= NativeMethods.WS_BORDER; break; }             return cp; } }

public BorderStyle BorderStyle { get { return borderStyle; }

set { if (borderStyle != value) { if (!Enum.IsDefined(typeof(BorderStyle), value)) { throw new InvalidEnumArgumentException(&quot;value&quot;, (int)value, typeof(BorderStyle)); }

borderStyle = value; UpdateStyles; }           }        }                    

back to the top

Compile the Project
Compile the project. To do this, on the Build menu, click Build Solution. This generates a dynamic-link library (.dll) file.

back to the top

Add a Reference to the UserControl
To add a reference to the UserControl that you created earlier, follow these steps:  Add a new Visual C# Windows Application to the Solution.

To add a new application, on the File menu, click Add Project, click New Project, and then click Windows Application. Right-click the Windows Application project in the Solution Explorer, and then click Add Reference.</li> Click the Projects tab, and then click UserControl.</li>  Include the following code at the end of all USING statements: using BorderedUserControlSample; </li>  Copy and paste the following member declaration in the Form1 class: private BorderedUserControlSample.UserControl1 myUserControl; </li> Copy and paste the following code in the InitializeComponent method:</li></ol>

myUserControl = new BorderedUserControlSample.UserControl1; myUserControl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.Controls.Add(myUserControl);

When you run the Windows Application Project, two text boxes appear inside a Fixed3D border.

back to the top