How to set a BorderStyle property for UserControls in Visual C# (316574)
The information in this article applies to:
- Microsoft .NET Framework SDK 1.0
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# 2005, Express Edition
This article was previously published under Q316574 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- Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, point to New, and then click Project.
- 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("value", (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.
- Click the Projects tab, and then click UserControl.
- Include the following code at the end of all USING statements:
using BorderedUserControlSample;
- Copy and paste the following member declaration in the Form1 class:
private BorderedUserControlSample.UserControl1 myUserControl;
- 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.
back to the top
REFERENCES For more information, see the following MSDN Web sites:
back to the top
Modification Type: | Major | Last Reviewed: | 1/9/2006 |
---|
Keywords: | kbCtrl kbHOWTOmaster KB316574 kbAudDeveloper |
---|
|