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

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.

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.

Customize the Form and the Control Properties

  1. 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);
    }
  2. 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;
  3. 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);
  4. 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("value", (int)value, typeof(BorderStyle));
    }

    borderStyle = value;
    UpdateStyles();
    }
    }
    }

Compile the Project

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

Add a Reference to the UserControl

To add a reference to the UserControl that you created earlier, follow these steps:
  1. 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.
  2. Right-click the Windows Application project in the Solution Explorer, and then click Add Reference.
  3. Click the Projects tab, and then click UserControl.
  4. Include the following code at the end of all USING statements:
    using BorderedUserControlSample;
  5. Copy and paste the following member declaration in the Form1 class:
    private BorderedUserControlSample.UserControl1 myUserControl;
  6. Copy and paste the following code in the InitializeComponent method:
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.

REFERENCES

For more information, see the following MSDN Web sites:
Eigenschaften

Artikelnummer: 316574 – Letzte Überarbeitung: 11.07.2008 – Revision: 1

Feedback