Microsoft KB Archive/318821

= HOW TO: Set the BorderStyle Property for User Controls By Using Visual Basic .NET =

Article ID: 318821

Article Last Modified on 2/10/2003

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework Class Libraries 1.0

-



This article was previously published under Q318821



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

REFERENCES



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

back to the top

Create a Windows Forms Application

 * 1) Start Microsoft Visual Studio .NET.
 * 2) On the File menu, click New, and then click Project.
 * 3) In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Control Library under Templates.
 * 4) In the Name text box, type BorderedUserControlSample, and then click OK.

back to the top

Customize Form and Control Properties
  Paste the following code before the PUBLIC CLASS USERCONTROL1 statement: Imports System.ComponentModel

Class win32 Public Const WS_BORDER = &H800000 Public Const WS_EX_CLIENTEDGE = &H200 End Class   Paste the following member declarations in the UserControl1 class after the INHERITS statement: Private borderStyleValue as System.Windows.Forms.BorderStyle Private textName As System.Windows.Forms.TextBox Private textAddress As System.Windows.Forms.TextBox Private e As System.Enum   Expand the Windows Form Designer generated code block, and then 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

' Add the controls to the user control. Controls.AddRange(New System.Windows.Forms.Control {textName, textAddress})

' Size the user control. Size = New System.Drawing.Size(200, 100)   Paste the following code in UserControl1 class after the UserControl1_Load method: Protected Overrides ReadOnly Property CreateParams _ As System.Windows.Forms.CreateParams Get Dim cp As System.Windows.Forms.CreateParams = MyBase.CreateParams

cp.ExStyle = cp.ExStyle And (Not Win32.WS_EX_CLIENTEDGE) cp.Style = cp.Style And (Not Win32.WS_BORDER)

Select Case BorderStyle Case BorderStyle.Fixed3D cp.ExStyle = cp.ExStyle Or Win32.WS_EX_CLIENTEDGE Case BorderStyle.FixedSingle cp.Style = cp.Style Or Win32.WS_BORDER End Select

Return cp       End Get End Property

Public Property BorderStyle As System.Windows.Forms.BorderStyle Get Return borderStyleValue End Get

Set(ByVal Value As System.Windows.Forms.BorderStyle) If (Not (borderStyleValue = value)) Then If (Not (e.IsDefined(GetType(BorderStyle), Value))) Then Throw New InvalidEnumArgumentException(&quot;value&quot;, CType(value, Integer), GetType(BorderStyle)) End If               borderStyleValue = value UpdateStyles End If       End Set End Property 

back to the top

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

back to the top

Add a Reference to the User Control
 Add a new Visual Basic .NET Windows Application to the solution: On the File menu, click Add Project, click New Project, click Windows Application, and then click OK. In Solution Explorer, right-click the Windows Application project, and then click Add Reference.</li> On the Projects tab, click BorderedUserControlSample, click Select, and then click OK.</li>  Paste the following code sample at the beginning in Form1.vb: Imports BorderedUserControlSample </li>  Paste the following member declaration in the Form1 class after the INHERITS statement: Private myUserControl As BorderedUserControlSample.UserControl1 </li>  Expand the Windows Form Designer generated code, and then paste the following code in the InitializeComponent method: myUserControl = New BorderedUserControlSample.UserControl1 myUserControl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Controls.Add(myUserControl) </li> In Solution Explorer, right-click the Windows Application that you created, and then click Set as StartUp Project.</li> Run the Windows Application Project, and then note that two text boxes appear inside a Fixed3D border.</li></ol>

back to the top

<div class="references_section">