VB.Net Tutorial/GUI/Form
Содержание
Add control to Form dynamically
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AddButtonToForm
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(104, 56) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Click Me" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(96, 168) Me.TextBox1.Name = "TextBox1" Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Dim WithEvents Button2 As Button Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Button2 = New Button Button2.Location = New Point(104, 104) Button2.Size = New Size(75, 23) Button2.Text = "And Me!" AddHandler Button2.Click, AddressOf Button2_Clicked Controls.Add(Button2) End Sub Private Sub Button2_Clicked(ByVal sender As Object, ByVal e As System.EventArgs) TextBox1.Text = "No problem!" End Sub
End Class</source>
Auto Scroll Form
<source lang="vbnet">Imports System.Windows.Forms public class AutoScroll
public Shared Sub Main Application.Run(New AutoScrollExample) End Sub
End class Public Class AutoScrollExample
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 Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents TextBox3 As System.Windows.Forms.TextBox Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents TextBox4 As System.Windows.Forms.TextBox Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents TextBox5 As System.Windows.Forms.TextBox Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents TextBox6 As System.Windows.Forms.TextBox Friend WithEvents Label9 As System.Windows.Forms.Label Friend WithEvents TextBox7 As System.Windows.Forms.TextBox Friend WithEvents Label10 As System.Windows.Forms.Label Friend WithEvents TextBox8 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.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.TextBox2 = New System.Windows.Forms.TextBox Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.RadioButton1 = New System.Windows.Forms.RadioButton Me.RadioButton2 = New System.Windows.Forms.RadioButton Me.RadioButton3 = New System.Windows.Forms.RadioButton Me.Label3 = New System.Windows.Forms.Label Me.TextBox3 = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.TextBox4 = New System.Windows.Forms.TextBox Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker Me.Label5 = New System.Windows.Forms.Label Me.ruboBox1 = New System.Windows.Forms.ruboBox Me.Label6 = New System.Windows.Forms.Label Me.Label7 = New System.Windows.Forms.Label Me.TextBox5 = New System.Windows.Forms.TextBox Me.Label8 = New System.Windows.Forms.Label Me.TextBox6 = New System.Windows.Forms.TextBox Me.Label9 = New System.Windows.Forms.Label Me.TextBox7 = New System.Windows.Forms.TextBox Me.Label10 = New System.Windows.Forms.Label Me.TextBox8 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.GroupBox1.SuspendLayout() Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(16, 32) Me.TextBox1.Name = "TextBox1" Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" " "Label1 " Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(8, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(63, 16) Me.Label1.TabIndex = 1 Me.Label1.Text = "First Name:" " "Label2 " Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(120, 8) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(62, 16) Me.Label2.TabIndex = 3 Me.Label2.Text = "Last Name:" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(128, 32) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(256, 20) Me.TextBox2.TabIndex = 2 Me.TextBox2.Text = "" " "GroupBox1 " Me.GroupBox1.Controls.Add(Me.RadioButton3) Me.GroupBox1.Controls.Add(Me.RadioButton2) Me.GroupBox1.Controls.Add(Me.RadioButton1) Me.GroupBox1.Location = New System.Drawing.Point(240, 64) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(144, 128) Me.GroupBox1.TabIndex = 4 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "Style" " "RadioButton1 " Me.RadioButton1.Location = New System.Drawing.Point(32, 24) Me.RadioButton1.Name = "RadioButton1" Me.RadioButton1.TabIndex = 0 Me.RadioButton1.Text = "S4 -- Blue" " "RadioButton2 " Me.RadioButton2.Location = New System.Drawing.Point(32, 56) Me.RadioButton2.Name = "RadioButton2" Me.RadioButton2.TabIndex = 1 Me.RadioButton2.Text = "S2 -- White" " "RadioButton3 " Me.RadioButton3.Location = New System.Drawing.Point(32, 88) Me.RadioButton3.Name = "RadioButton3" Me.RadioButton3.TabIndex = 2 Me.RadioButton3.Text = "S0 -- Black" " "Label3 " Me.Label3.AutoSize = True Me.Label3.Location = New System.Drawing.Point(8, 58) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(33, 16) Me.Label3.TabIndex = 6 Me.Label3.Text = "Zone:" " "TextBox3 " Me.TextBox3.Location = New System.Drawing.Point(48, 56) Me.TextBox3.Name = "TextBox3" Me.TextBox3.Size = New System.Drawing.Size(184, 20) Me.TextBox3.TabIndex = 5 Me.TextBox3.Text = "" " "Label4 " Me.Label4.AutoSize = True Me.Label4.Location = New System.Drawing.Point(8, 90) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(96, 16) Me.Label4.TabIndex = 8 Me.Label4.Text = "City of Residence:" " "TextBox4 " Me.TextBox4.Location = New System.Drawing.Point(104, 88) Me.TextBox4.Name = "TextBox4" Me.TextBox4.Size = New System.Drawing.Size(128, 20) Me.TextBox4.TabIndex = 7 Me.TextBox4.Text = "" " "DateTimePicker1 " Me.DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Short Me.DateTimePicker1.Location = New System.Drawing.Point(104, 112) Me.DateTimePicker1.Name = "DateTimePicker1" Me.DateTimePicker1.Size = New System.Drawing.Size(128, 20) Me.DateTimePicker1.TabIndex = 9 " "Label5 " Me.Label5.AutoSize = True Me.Label5.Location = New System.Drawing.Point(8, 114) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(90, 16) Me.Label5.TabIndex = 10 Me.Label5.Text = "Residence Entry:" " "ComboBox1 " Me.ruboBox1.Location = New System.Drawing.Point(64, 136) Me.ruboBox1.Name = "ComboBox1" Me.ruboBox1.Size = New System.Drawing.Size(168, 21) Me.ruboBox1.TabIndex = 11 Me.ruboBox1.Text = "State" " "Label6 " Me.Label6.AutoSize = True Me.Label6.Location = New System.Drawing.Point(8, 138) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(45, 16) Me.Label6.TabIndex = 12 Me.Label6.Text = "System:" " "Label7 " Me.Label7.AutoSize = True Me.Label7.Location = New System.Drawing.Point(8, 170) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(62, 16) Me.Label7.TabIndex = 14 Me.Label7.Text = "Field Code:" " "TextBox5 " Me.TextBox5.Location = New System.Drawing.Point(72, 168) Me.TextBox5.Name = "TextBox5" Me.TextBox5.Size = New System.Drawing.Size(160, 20) Me.TextBox5.TabIndex = 13 Me.TextBox5.Text = "" " "Label8 " Me.Label8.AutoSize = True Me.Label8.Location = New System.Drawing.Point(8, 202) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(44, 16) Me.Label8.TabIndex = 16 Me.Label8.Text = "Code 1:" " "TextBox6 " Me.TextBox6.Location = New System.Drawing.Point(56, 200) Me.TextBox6.Name = "TextBox6" Me.TextBox6.Size = New System.Drawing.Size(328, 20) Me.TextBox6.TabIndex = 15 Me.TextBox6.Text = "" " "Label9 " Me.Label9.AutoSize = True Me.Label9.Location = New System.Drawing.Point(8, 226) Me.Label9.Name = "Label9" Me.Label9.Size = New System.Drawing.Size(44, 16) Me.Label9.TabIndex = 18 Me.Label9.Text = "Code 2:" " "TextBox7 " Me.TextBox7.Location = New System.Drawing.Point(56, 224) Me.TextBox7.Name = "TextBox7" Me.TextBox7.Size = New System.Drawing.Size(328, 20) Me.TextBox7.TabIndex = 17 Me.TextBox7.Text = "" " "Label10 " Me.Label10.AutoSize = True Me.Label10.Location = New System.Drawing.Point(8, 250) Me.Label10.Name = "Label10" Me.Label10.Size = New System.Drawing.Size(44, 16) Me.Label10.TabIndex = 20 Me.Label10.Text = "Code 3:" " "TextBox8 " Me.TextBox8.Location = New System.Drawing.Point(56, 248) Me.TextBox8.Name = "TextBox8" Me.TextBox8.Size = New System.Drawing.Size(328, 20) Me.TextBox8.TabIndex = 19 Me.TextBox8.Text = "" " "Button1 " Me.Button1.Location = New System.Drawing.Point(232, 280) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 21 Me.Button1.Text = "OK" " "Button2 " Me.Button2.Location = New System.Drawing.Point(312, 280) Me.Button2.Name = "Button2" Me.Button2.TabIndex = 22 Me.Button2.Text = "Cancel" " "AutoScrollExample " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.AutoScroll = True Me.ClientSize = New System.Drawing.Size(392, 310) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.Label10) Me.Controls.Add(Me.TextBox8) Me.Controls.Add(Me.Label9) Me.Controls.Add(Me.TextBox7) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.TextBox6) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.TextBox5) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.ruboBox1) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.DateTimePicker1) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.TextBox4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.TextBox3) Me.Controls.Add(Me.GroupBox1) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.TextBox1) Me.Name = "AutoScrollExample" Me.Text = "AutoScrollExample" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
End Class</source>
Border Less form
<source lang="vbnet">Imports System.Windows.Forms public class ConvertMouseEventToFormDragEvent
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Const HT_CAPTION As Integer = &H2 Const WM_NCLBUTTONDOWN As Integer = &HA1 Private Sub DragBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DragBar.MouseDown If (e.Button = Windows.Forms.MouseButtons.Left) Then DragBar.Capture = False Me.WndProc(Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _ CType(HT_CAPTION, IntPtr), IntPtr.Zero)) End If End Sub Private Sub ActClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActClose.Click Me.Close() 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.ActClose = New System.Windows.Forms.Button Me.DragBar = New System.Windows.Forms.PictureBox CType(Me.DragBar, System.ruponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() " "ActClose " Me.ActClose.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.ActClose.Location = New System.Drawing.Point(208, 80) Me.ActClose.Name = "ActClose" Me.ActClose.Size = New System.Drawing.Size(75, 23) Me.ActClose.TabIndex = 0 Me.ActClose.Text = "Close" Me.ActClose.UseVisualStyleBackColor = True " "DragBar " Me.DragBar.BackColor = System.Drawing.SystemColors.ActiveCaption Me.DragBar.Location = New System.Drawing.Point(8, 8) Me.DragBar.Name = "DragBar" Me.DragBar.Size = New System.Drawing.Size(280, 24) Me.DragBar.TabIndex = 1 Me.DragBar.TabStop = False " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.CancelButton = Me.ActClose Me.ClientSize = New System.Drawing.Size(292, 114) Me.Controls.Add(Me.DragBar) Me.Controls.Add(Me.ActClose) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None Me.Name = "Form1" Me.Text = "Form1" CType(Me.DragBar, System.ruponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub Friend WithEvents ActClose As System.Windows.Forms.Button Friend WithEvents DragBar As System.Windows.Forms.PictureBox
End Class</source>
Build Form hierarchically
<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms
public class InheritedForm : inherits BaseForm
private WithEvents btn as Button public sub New() Text = "Inherited Form" btn = new Button() btn.Location = new Point(25,150) btn.Size = new Size(125,25) btn.Text = "C&lose on Inherited" Controls.Add(btn) lbl.Text = "Now from InheritedForm" end sub Public Shadows Shared Sub Main() Application.Run(new InheritedForm()) end sub private sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn.Click Application.Exit() end sub protected Overrides Sub SomeMethod() MessageBox.Show("This is the overridden SomeMethod called " + _ "from InheritedForm.") end sub
end class
public class BaseForm : inherits System.Windows.Forms.Form
private WithEvents btnClose as Button private WithEvents btnApp as Button protected lbl as Label public Sub New() btnClose = new Button() btnClose.Location = new Point(25,100) btnClose.Size = new Size(100,25) btnClose.Text = "&Close" btnApp = new Button() btnApp.Location = new Point(200,100) btnApp.Size = new Size(150,25) btnApp.Text = "&Base Application" lbl = new Label() lbl.Location = new Point(25,25) lbl.Size = new Size(100,25) lbl.Text = "This label on BaseForm" Controls.AddRange(new Control(){lbl, btnClose, btnApp}) end sub private sub btnClose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClose.Click Application.Exit() end sub private sub btnApp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApp.Click MessageBox.Show("This is the Base application.") SomeMethod() end sub protected Overridable Sub SomeMethod() MessageBox.Show("This is SomeMethod called from BaseForm.") end sub
end class</source>
Create Form window by hand
<source lang="vbnet">Option Strict On Imports System.Drawing Imports System.Windows.Forms Public Class Form1 : Inherits Form
Private WithEvents cmdClickCtr As Button = New Button() Public Sub New() Me.cmdClickCtr.Location = New Point(100, 75) Me.cmdClickCtr.Size = New Size(100, 50) Me.cmdClickCtr.Text = "Click Counter" Me.Controls.Add(cmdClickCtr) Me.Text = "Click Counter" End Sub Private Sub cmdClickCtr_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdClickCtr.Click Static ctr As Integer = 1 MsgBox("Button was clicked " & ctr & " times.") ctr += 1 End Sub Public Shared Sub Main() Dim frm As New Form1() frm.ShowDialog() End Sub
End Class</source>
Form Fade out
<source lang="vbnet">Imports System.Windows.Forms public class FormFadeOut
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub ActClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ActClose.Click Dim counter As Integer For counter = 90 To 10 Step -5 Me.Opacity = counter / 100 Me.Refresh() Threading.Thread.Sleep(100) Next counter Me.Close() 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.ActClose = New System.Windows.Forms.Button Me.SuspendLayout() " "ActClose " Me.ActClose.Location = New System.Drawing.Point(104, 112) Me.ActClose.Name = "ActClose" Me.ActClose.Size = New System.Drawing.Size(75, 23) Me.ActClose.TabIndex = 0 Me.ActClose.Text = "Close" Me.ActClose.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(292, 266) Me.Controls.Add(Me.ActClose) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.Text = "Fading Form" Me.ResumeLayout(False) End Sub Friend WithEvents ActClose As System.Windows.Forms.Button
End Class</source>
Form Inheritance
<source lang="vbnet">Imports System.Windows.Forms public class FormInheritance
public Shared Sub Main Application.Run(New Startup) End Sub
End class
Public Class Startup
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 Button2 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button() Me.Button2 = New System.Windows.Forms.Button() Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(48, 48) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "BaseForm" " "Button2 " Me.Button2.Location = New System.Drawing.Point(152, 48) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(88, 23) Me.Button2.TabIndex = 1 Me.Button2.Text = "InheritedForm" " "Startup " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1}) Me.Name = "Startup" Me.Text = "Startup" 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 frm1 as new BaseForm() frm1.Show() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click dim frm2 as new InheritedForm() frm2.Show() End Sub
End Class
Public Class BaseForm
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. Protected WithEvents lblHeading As System.Windows.Forms.Label Protected WithEvents btn As System.Windows.Forms.Button Protected WithEvents lblOutput As System.Windows.Forms.Label Private WithEvents Label3 As System.Windows.Forms.Label Private WithEvents Label2 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblHeading = New System.Windows.Forms.Label() Me.btn = New System.Windows.Forms.Button() Me.lblOutput = New System.Windows.Forms.Label() Me.Label3 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.SuspendLayout() " "lblHeading " Me.lblHeading.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblHeading.Location = New System.Drawing.Point(24, 16) Me.lblHeading.Name = "lblHeading" Me.lblHeading.Size = New System.Drawing.Size(248, 23) Me.lblHeading.TabIndex = 0 Me.lblHeading.Text = "Base Form" Me.lblHeading.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "btn " Me.btn.Location = New System.Drawing.Point(96, 72) Me.btn.Name = "btn" Me.btn.TabIndex = 1 Me.btn.Text = "Time" " "lblOutput " Me.lblOutput.Location = New System.Drawing.Point(64, 128) Me.lblOutput.Name = "lblOutput" Me.lblOutput.Size = New System.Drawing.Size(136, 23) Me.lblOutput.TabIndex = 2 Me.lblOutput.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label3 " Me.Label3.Location = New System.Drawing.Point(80, 200) Me.Label3.Name = "Label3" Me.Label3.TabIndex = 3 Me.Label3.Text = "Base Form" Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label2 " Me.Label2.Location = New System.Drawing.Point(40, 232) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(216, 23) Me.Label2.TabIndex = 4 Me.Label2.Text = "Created in Visual Studio .NET" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "BaseForm " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label3, Me.lblOutput, Me.btn, Me.lblHeading}) Me.Name = "BaseForm" Me.Text = "Visual Form Inheritance" Me.ResumeLayout(False)
End Sub
- End Region
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click lblOutput.Text = "The time is: " + DateTime.Now.ToString("T") End Sub
End Class
Public Class InheritedForm
Inherits BaseForm
- 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 btn.Text = "Date" lblHeading.Text = "Inherited Form" 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. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SuspendLayout() " "lblHeading " Me.lblHeading.BackColor = System.Drawing.Color.Yellow Me.lblHeading.Visible = True " "btn " Me.btn.BackColor = System.Drawing.Color.Yellow Me.btn.Visible = True " "lblOutput " Me.lblOutput.BackColor = System.Drawing.Color.Yellow Me.lblOutput.Visible = True " "InheritedForm " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.BackColor = System.Drawing.Color.Yellow Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblOutput, Me.btn, Me.lblHeading}) Me.Name = "InheritedForm" Me.ResumeLayout(False)
End Sub
- End Region
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click lblOutput.Text = "Today is: " + DateTime.Now.ToString("D") End Sub
End Class</source>
Form: show, hide and get data from
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class FormShowHide
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 Button2 As System.Windows.Forms.Button Friend WithEvents Button3 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.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.TextBox1 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(80, 40) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(120, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = "Show" " "Button2 " Me.Button2.Location = New System.Drawing.Point(80, 96) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(120, 23) Me.Button2.TabIndex = 1 Me.Button2.Text = "Hide" " "Button3 " Me.Button3.Location = New System.Drawing.Point(80, 152) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(120, 23) Me.Button3.TabIndex = 2 Me.Button3.Text = "Get text" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(80, 208) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(120, 20) Me.TextBox1.TabIndex = 3 Me.TextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Dim frmTwo As New Form2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frmTwo.Show() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click frmTwo.Hide() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click TextBox1.Text = frmTwo.TextBox1.Text 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 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(96, 64) Me.TextBox1.Name = "TextBox1" Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "Welcome to Form2" " "Form2 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.TextBox1) Me.Name = "Form2" Me.Text = "Form2" Me.ResumeLayout(False) End Sub
- End Region
End Class</source>
Owner draw form
<source lang="vbnet">Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D
public class PartialVisible
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click Me.Close() End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint e.Graphics.Clear(Color.Fuchsia) e.Graphics.FillRectangle(Brushes.Gold, 0.0F, _ Me.ClientRectangle.Height / 3.0F, _ CSng(Me.ClientRectangle.Width), _ Me.ClientRectangle.Height / 3.0F) Dim largerFont = New Font(Me.Font.Name, 20) e.Graphics.DrawString("This is a form", largerFont, Brushes.Black, _ 20, (Me.ClientRectangle.Height / 2) - (largerFont.Height / 2)) 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.SuspendLayout() " "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, 266) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Form1" Me.TransparencyKey = System.Drawing.Color.Fuchsia Me.ResumeLayout(False) End Sub
End Class</source>
SendKeys
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AddOwnedForm
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 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(104, 64) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Send Keys" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) 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 AppActivate("Document - WordPad") System.Windows.Forms.SendKeys.Send("Visual Basic says Hello!") End Sub
End Class</source>
Set Form Icon
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MyResourcesMainFormIcon
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Icon = New Icon("1.ico") End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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.SuspendLayout() " "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, 273) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
End Class</source>