VB.Net/GUI/User Control

Материал из VB Эксперт
Перейти к: навигация, поиск

Sub class UserControl to create your own control

<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Xml Imports System.Windows.Forms Imports System.Data.SqlClient Imports System.Data.OleDb Public Class MainClass

   Shared Sub Main()
       Dim form1 As Form = New Form1
       Application.Run(form1)
   End Sub

End Class


Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 MyControl1 As MyControl
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.MyControl1 = New MyControl()
       Me.SuspendLayout()
       "
       "MyControl1
       "
       Me.MyControl1.Location = New System.Drawing.Point(8, 8)
       Me.MyControl1.MessageText = "Hello, world!"
       Me.MyControl1.Name = "MyControl1"
       Me.MyControl1.Size = New System.Drawing.Size(152, 104)
       Me.MyControl1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(240, 189)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.MyControl1})
       Me.Name = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region

End Class


Public Class MyControl

   Inherits System.Windows.Forms.UserControl
   Private _messageText As String
   Event HelloWorld(ByVal sender As Object, ByVal e As System.EventArgs)
  1. 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
       MessageText = "Hello, world!"
   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
   "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 btnSayHello As System.Windows.Forms.Button
   Friend WithEvents btnSaySomething As System.Windows.Forms.Button
   Friend WithEvents btnSaySomethingElse As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.btnSayHello = New System.Windows.Forms.Button()
       Me.btnSaySomething = New System.Windows.Forms.Button()
       Me.btnSaySomethingElse = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "btnSayHello
       "
       Me.btnSayHello.Location = New System.Drawing.Point(8, 8)
       Me.btnSayHello.Name = "btnSayHello"
       Me.btnSayHello.Size = New System.Drawing.Size(136, 23)
       Me.btnSayHello.TabIndex = 0
       Me.btnSayHello.Text = "Say, ""Hello, world!"""
       "
       "btnSaySomething
       "
       Me.btnSaySomething.Location = New System.Drawing.Point(8, 40)
       Me.btnSaySomething.Name = "btnSaySomething"
       Me.btnSaySomething.Size = New System.Drawing.Size(136, 23)
       Me.btnSaySomething.TabIndex = 1
       Me.btnSaySomething.Text = "Say Something"
       "
       "btnSaySomethingElse
       "
       Me.btnSaySomethingElse.Location = New System.Drawing.Point(8, 72)
       Me.btnSaySomethingElse.Name = "btnSaySomethingElse"
       Me.btnSaySomethingElse.Size = New System.Drawing.Size(136, 23)
       Me.btnSaySomethingElse.TabIndex = 2
       Me.btnSaySomethingElse.Text = "Say Something Else"
       "
       "MyControl
       "
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSaySomethingElse, Me.btnSaySomething, Me.btnSayHello})
       Me.Name = "MyControl"
       Me.Size = New System.Drawing.Size(152, 104)
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub btnSayHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSayHello.Click
       " raise the event...
       RaiseEvent HelloWorld(Me, New System.EventArgs())
       MsgBox(_messageText)
   End Sub
   Private Sub btnSaySomething_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaySomething.Click
       MsgBox("Something!")
   End Sub
   Private Sub btnSaySomethingElse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaySomethingElse.Click
       MsgBox("Something else!")
   End Sub
   Public Property MessageText() As String
       Get
           Return _messageText
       End Get
       Set(ByVal Value As String)
           " set the text...
           _messageText = Value
           " update the button...
           btnSayHello.Text = "Say """ & _messageText & """"
       End Set
   End Property
   Public Sub ResetMessageText()
       MessageText = "Hello, world!"
   End Sub
   Private Sub MyControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   End Sub

End Class

      </source>


User-defined control with a timer and a label

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms

Public Class MainClass

  Shared Sub Main()
       Dim myform As Form = New FrmClock()
       Application.Run(myform)
  End Sub " Main

End Class


Public Class FrmClock

  Inherits System.Windows.Forms.Form
  1. 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 myCClockUserControl As CClockUserControl
  
  "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.myCClockUserControl = New CClockUserControl()
     Me.SuspendLayout()
     "
     "myCClockUserControl
     "
     Me.myCClockUserControl.Location = New System.Drawing.Point(24, 24)
     Me.myCClockUserControl.Name = "myCClockUserControl"
     Me.myCClockUserControl.Size = New System.Drawing.Size(96, 48)
     Me.myCClockUserControl.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.myCClockUserControl})
     Me.Name = "FrmClock"
     Me.Text = "Clock"
     Me.ResumeLayout(False)
  End Sub
  1. End Region

End Class " create a clock control that inherits from UserControl Public Class CClockUserControl

  Inherits UserControl
  1. 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.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
     "
     "CClockUserControl
     "
     Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblDisplay})
     Me.Name = "CClockUserControl"
     Me.Size = New System.Drawing.Size(150, 72)
     Me.ResumeLayout(False)
  End Sub
  1. End Region
  " update label at every tick
  Private Sub tmrClock_Tick(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles tmrClock.Tick
     " get current time (Now), converto to string
     lblDisplay.Text = DateTime.Now.ToLongTimeString
  End Sub

End Class

      </source>