Create user control : Custom Control « GUI « VB.Net Tutorial






Create user control
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms


public class UseCustomControl
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Public ColorChanged As Boolean
    Private origionalcolor As Color
    Enum month
        Frebrary
        January
        March
        April
        May
        June
        July
        Agustor
        September
        October
        November
        December
    End Enum
    Private monthnum As month

    Property mymonth() As month
        Get
            mymonth = monthnum
        End Get
        Set(ByVal value As month)
            monthnum = value
        End Set
    End Property


    Private Sub Control11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Control11.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Visible = False
        colorchanged = Not ColorChanged
        If colorchanged = True Then
            Control11.BackColor = origionalcolor
        Else
            Control11.BackColor = System.Drawing.Color.Red
        End If
        Control11.MyText = "Custom property"
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Label1.Visible = False
        If ColorChanged = True Then
            Control11.BackColor = origionalcolor
            ColorChanged = False
        End If

    End Sub

    Private Sub Control11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Control11.Click
        Label1.Visible = True
    End Sub

    Private Sub Control11_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Control11.MouseMove
        If Not ColorChanged Then
            Control11.BackColor = System.Drawing.Color.Green
            ColorChanged = True
        End If

    End Sub
End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Control11 = New Control1
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 40)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Click Me"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(21, 9)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(41, 12)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Label1"
        '
        'Control11
        '
        Me.Control11.Location = New System.Drawing.Point(36, 82)
        Me.Control11.Name = "Control11"
        Me.Control11.Size = New System.Drawing.Size(398, 166)
        Me.Control11.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(459, 260)
        Me.Controls.Add(Me.Control11)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Control11 As Control1

End Class



Public Class Control1
    Inherits System.Windows.Forms.UserControl
    Private mystring As String
    Private mycolor As System.Drawing.Color
    Private myImage As System.Drawing.Image

    Public Property MyText() As String
        Get
            MyText = mystring
            myname.Text = mystring
        End Get

        Set(ByVal value As String)
            mystring = value
            myname.Text = mystring
        End Set
    End Property
    Public Property MyBackgroundImage() As Image
        Get
            MyBackgroundImage = myImage
            mybackcolor.Image = myImage
        End Get
        Set(ByVal value As Image)
            myImage = value
            mybackcolor.Image = myImage
        End Set
    End Property


    Private Sub Control1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        mybackcolor.Left = Me.Left
        mybackcolor.Top = Me.Top + 10
        mybackcolor.Width = Me.Width
        mybackcolor.Height = Me.Height - 10

    End Sub

End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Control1
    Inherits System.Windows.Forms.UserControl

    'UserControl1 overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.myname = New System.Windows.Forms.Label
        Me.mybackcolor = New System.Windows.Forms.PictureBox
        CType(Me.mybackcolor, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'myname
        '
        Me.myname.AutoSize = True
        Me.myname.Location = New System.Drawing.Point(21, 15)
        Me.myname.Name = "myname"
        Me.myname.Size = New System.Drawing.Size(53, 12)
        Me.myname.TabIndex = 0
        Me.myname.Text = "Control1"
        '
        'mybackcolor
        '
        Me.mybackcolor.BackColor = System.Drawing.SystemColors.ActiveCaption
        Me.mybackcolor.Location = New System.Drawing.Point(18, 42)
        Me.mybackcolor.Name = "mybackcolor"
        Me.mybackcolor.Size = New System.Drawing.Size(317, 141)
        Me.mybackcolor.TabIndex = 1
        Me.mybackcolor.TabStop = False
        '
        'Control1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.mybackcolor)
        Me.Controls.Add(Me.myname)
        Me.Name = "Control1"
        Me.Size = New System.Drawing.Size(349, 200)
        CType(Me.mybackcolor, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Public WithEvents myname As System.Windows.Forms.Label
    Public WithEvents mybackcolor As System.Windows.Forms.PictureBox

End Class








14.78.Custom Control
14.78.1.Create user controlCreate user control
14.78.2.Paint ControlPaint Control
14.78.3.User-defined control with a timer and a labelUser-defined control with a timer and a label
14.78.4.Custom paint ButtonCustom paint Button
14.78.5.Friend ControlFriend Control