Share via

Add Windows.Forms ControlBox to UserControl

raklali 236 Reputation points
Dec 31, 2021, 2:51 PM

VB.Net UserControl does not Provide facility to use ControlBox in the UserControl.

Can we Customise a UserControl to display a ControlBox similar to Windows.Forms.Form?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,781 questions
{count} votes

Accepted answer
  1. Castorix31 86,986 Reputation points
    Dec 31, 2021, 8:34 PM

    From comments, the test I did with a random UserControl
    Not very standard to do that... =>

    Imports System.Runtime.InteropServices
    
    Public Class UserControl1
        Inherits UserControl
        Friend WithEvents Label1 As Label
        Public Sub New()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.Label1.Location = New System.Drawing.Point(2, 2)
            Me.Label1.Name = "Label1"
            Me.Label1.Text = "Label1"
            Me.Label1.Size = New System.Drawing.Size(100, 20)
            Me.Controls.Add(Label1)
            Me.BorderStyle = BorderStyle.FixedSingle
        End Sub
    
        Protected Overrides ReadOnly Property CreateParams As CreateParams
            Get
                Dim cp As CreateParams = MyBase.CreateParams
                cp.Style = cp.Style Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX
                Return cp
            End Get
        End Property
    
        Public Const WS_SYSMENU = &H80000
        Public Const WS_CAPTION = &HC00000
        Public Const WS_THICKFRAME = &H40000
        Public Const WS_MINIMIZEBOX = &H20000
        Public Const WS_MAXIMIZEBOX = &H10000
        Public Const WS_CHILD = &H40000000
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If (m.Msg = WM_SYSCOMMAND) Then
                If (CInt(m.WParam) And &HFFF0) = SC_CLOSE Then
                    Console.Beep(6000, 10)
                    m.Result = CType(0, IntPtr)
                    Me.Dispose()
                    ' To avoid WM_CLOSE to be posted to main Form
                    Return
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    
        Public Const WM_SYSCOMMAND = &H112
    
        Public Const SC_MINIMIZE = &HF020
        Public Const SC_MAXIMIZE = &HF030
        Public Const SC_RESTORE = &HF120
        Public Const SC_CLOSE = &HF060
    End Class
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.