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



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)
  • Microsoft .NET Framework Class Libraries 1.0

This article was previously published under Q318821

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

  1. 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
    					
  2. 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
    					
  3. 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)
    					
  4. 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("value", 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

  1. 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.
  2. In Solution Explorer, right-click the Windows Application project, and then click Add Reference.
  3. On the Projects tab, click BorderedUserControlSample, click Select, and then click OK.
  4. Paste the following code sample at the beginning in Form1.vb:
    Imports BorderedUserControlSample
    					
  5. Paste the following member declaration in the Form1 class after the INHERITS statement:
    Private myUserControl As BorderedUserControlSample.UserControl1
    					
  6. 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)
    					
  7. In Solution Explorer, right-click the Windows Application that you created, and then click Set as StartUp Project.
  8. Run the Windows Application Project, and then note that two text boxes appear inside a Fixed3D border.
back to the top

REFERENCES

For additional information in a Microsoft Visual C# .NET version of this article, click the article number below to view the article in the Microsoft Knowledge Base:

316574 HOW TO: Set a BorderStyle Property for UserControls in Visual C# .NET


back to the top

Modification Type:MajorLast Reviewed:2/10/2003
Keywords:kbHOWTOmaster KB318821 kbAudDeveloper