VB.Net Tutorial/GUI/Custom Control
Содержание
Create user control
<source lang="vbnet">Imports System.ruponentModel 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.rupilerServices.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.ruponentModel.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.rupilerServices.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.ruponentModel.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.ruponentModel.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.ruponentModel.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</source>
Custom paint Button
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MouseEnterLeave
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private ButtonBackColor As Color = Color.LightGreen Private Sub Button2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.MouseEnter ButtonBackColor = Color.Yellow End Sub Private Sub Button2_MouseLeave(ByVal sender As Object,ByVal e As System.EventArgs) Handles Button2.MouseLeave ButtonBackColor = Color.Red End Sub Private Sub Button2_MouseDown(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown ButtonBackColor = Color.Green End Sub Private Sub Button2_MouseUp(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseUp ButtonBackColor = Color.LightGreen Button2.Refresh() End Sub Private Sub Button2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button2.Paint Dim canvas As Graphics = e.Graphics canvas.Clear(ButtonBackColor) Dim atomPen As Pen = New Pen(Color.Blue, 2) Dim largerFont As Font = New Font(Me.Font.Name, 10) e.Graphics.DrawString("This is a form", largerFont, Brushes.Black,0, 20) End Sub Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click MsgBox("Button2 clicked!", MsgBoxStyle.Exclamation,"Painting on Controls") End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.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.ruponentModel.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.Panel1 = New System.Windows.Forms.Panel Me.Button2 = New System.Windows.Forms.Button Me.SuspendLayout() " "Panel1 " Me.Panel1.Location = New System.Drawing.Point(56, 32) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(368, 40) Me.Panel1.TabIndex = 0 " "Button2 " Me.Button2.Location = New System.Drawing.Point(272, 104) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(152, 152) Me.Button2.TabIndex = 1 Me.Button2.Text = "Button2" Me.Button2.UseVisualStyleBackColor = True " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(479, 284) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Panel1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Text = "Special Effects" Me.ResumeLayout(False) End Sub Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents Button2 As System.Windows.Forms.Button
End Class</source>
Friend Control
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GetDialogResult
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.TextBox1 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(112, 40) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(88, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = "Show Dialog" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(104, 96) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(112, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(304, 181) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frmDialog As New Form2 If frmDialog.ShowDialog() = DialogResult.OK Then TextBox1.Text = "You typed: " & frmDialog.TextBox1.Text End If End Sub
End Class
Public Class Form2
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(16, 16) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(256, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" " "Button1 " Me.Button1.DialogResult = System.Windows.Forms.DialogResult.OK Me.Button1.Location = New System.Drawing.Point(48, 72) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "OK" " "Button2 " Me.Button2.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.Button2.Location = New System.Drawing.Point(168, 72) Me.Button2.Name = "Button2" Me.Button2.TabIndex = 2 Me.Button2.Text = "Cancel" " "Form2 " Me.AcceptButton = Me.Button1 Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.CancelButton = Me.Button2 Me.ClientSize = New System.Drawing.Size(288, 125) Me.ControlBox = False Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog Me.Name = "Form2" Me.Text = "Dialog" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub
End Class</source>
Paint Control
<source lang="vbnet">Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D <Global.Microsoft.VisualBasic.rupilerServices.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.ruponentModel.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.XButton = New System.Windows.Forms.Button Me.ColorList = New System.Windows.Forms.ruboBox Me.SuspendLayout() " "XButton " Me.XButton.Location = New System.Drawing.Point(112, 24) Me.XButton.Name = "XButton" Me.XButton.Size = New System.Drawing.Size(75, 23) Me.XButton.TabIndex = 0 Me.XButton.Text = "Button1" Me.XButton.UseVisualStyleBackColor = True " "ColorList " Me.ColorList.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed Me.ColorList.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList Me.ColorList.FormattingEnabled = True Me.ColorList.Location = New System.Drawing.Point(88, 64) Me.ColorList.Name = "ColorList" Me.ColorList.Size = New System.Drawing.Size(121, 21) Me.ColorList.TabIndex = 1 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(292, 221) Me.Controls.Add(Me.ColorList) Me.Controls.Add(Me.XButton) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.Text = "Control Drawing" Me.ResumeLayout(False) End Sub Friend WithEvents XButton As System.Windows.Forms.Button Friend WithEvents ColorList As System.Windows.Forms.ruboBox
End Class Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ColorList.Items.Add("Red") ColorList.Items.Add("Orange") ColorList.Items.Add("Yellow") ColorList.Items.Add("Green") ColorList.Items.Add("Blue") ColorList.Items.Add("Indigo") ColorList.Items.Add("Violet") End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint e.Graphics.DrawEllipse(Pens.Black, 10, 10, Me.ClientRectangle.Width - 20, _ Me.ClientRectangle.Height - 20) End Sub Private Sub XButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles XButton.Paint Dim usePen As Pen e.Graphics.Clear(SystemColors.Control) usePen = New Pen(SystemColors.ControlText, 3) e.Graphics.DrawRectangle(usePen, XButton.ClientRectangle) usePen.Dispose() End Sub Private Sub ColorList_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ColorList.DrawItem Dim useBrush As Brush If (e.Index = -1) Then Return e.DrawBackground() useBrush = New SolidBrush(Color.FromName(CStr(ColorList.Items(e.Index)))) e.Graphics.FillRectangle(useBrush, _ e.Bounds.Left + 2, e.Bounds.Top + 2, _ e.Bounds.Width - 4, e.Bounds.Height - 4) useBrush.Dispose() e.Graphics.DrawRectangle(Pens.Black, _ e.Bounds.Left + 2, e.Bounds.Top + 2, _ e.Bounds.Width - 4, e.Bounds.Height - 4) e.DrawFocusRectangle() End Sub Private Sub XButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XButton.Click MsgBox("Button clicked.") End Sub
End Class public class ControlPaint
public Shared Sub Main Application.Run(New Form1) End Sub
End class</source>
User-defined control with a timer and a label
<source lang="vbnet">Imports System.Windows.Forms public class UserControlDefinition
public Shared Sub Main Application.Run(New FrmClock) End Sub
End class Public Class FrmClock
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents myClockControl As ClockControl "Required by the Windows Form Designer Private components As System.ruponentModel.Container "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.myClockControl = New ClockControl() Me.SuspendLayout() " "myClockControl " Me.myClockControl.Location = New System.Drawing.Point(24, 24) Me.myClockControl.Name = "myClockControl" Me.myClockControl.Size = New System.Drawing.Size(96, 48) Me.myClockControl.TabIndex = 0 " "FrmClock " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(144, 93) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.myClockControl}) Me.Name = "FrmClock" Me.Text = "Clock" Me.ResumeLayout(False) End Sub
- End Region
End Class
Public Class ClockControl
Inherits UserControl
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "UserControl overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub " displays time Friend WithEvents lblDisplay As Label " non-visible event-triggering timer object Friend WithEvents tmrClock As Timer "Required by the Windows Form Designer Private components As System.ruponentModel.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.ruponents = New System.ruponentModel.Container() Me.tmrClock = New System.Windows.Forms.Timer(Me.ruponents) Me.lblDisplay = New System.Windows.Forms.Label() Me.SuspendLayout() " "tmrClock " Me.tmrClock.Enabled = True " "lblDisplay " Me.lblDisplay.BackColor = System.Drawing.Color.White Me.lblDisplay.Dock = System.Windows.Forms.DockStyle.Fill Me.lblDisplay.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblDisplay.Name = "lblDisplay" Me.lblDisplay.Size = New System.Drawing.Size(150, 72) Me.lblDisplay.TabIndex = 0 Me.lblDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "ClockControl " Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblDisplay}) Me.Name = "ClockControl" Me.Size = New System.Drawing.Size(150, 72) Me.ResumeLayout(False) End Sub
- End Region
Private Sub tmrClock_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles tmrClock.Tick lblDisplay.Text = DateTime.Now.ToLongTimeString End Sub
End Class</source>