VB.Net/GUI/Control
Содержание
- 1 Adding a control to a Form at run time
- 2 Assign the Bitmap to the control"s Image property
- 3 Component Client Rectangle and Display Rectangle
- 4 Control Paint Demo
- 5 Convert object to Control Object
- 6 Display Names for All controls on a Frame
- 7 Get Control Handle and check its value
- 8 Get Labels from all Controls on a Form
Adding a control to a Form at run time
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Printing
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
- 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.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() " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15) Me.ClientSize = New System.Drawing.Size(304, 268) Me.Name = "Form1" Me.Text = "Adding a control at run time!" End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load Dim newButton As New System.Windows.Forms.Button() With newButton .Visible = True .Size = New Size(100, 100) .Text = "I"m a new button" End With Me.Controls.Add(newButton) AddHandler newButton.Click, AddressOf Me.newButton_Click End Sub Public Sub newButton_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) MessageBox.Show("You clicked on my new button") End Sub
End Class
</source>
Assign the Bitmap to the control"s Image property
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Printing Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 Dim bm As New Bitmap( _ picCanvas3.DisplayRectangle.Width, _ picCanvas3.DisplayRectangle.Height) Dim gr As Graphics = Graphics.FromImage(bm) gr.DrawEllipse(Pens.Yellow, _ picCanvas3.DisplayRectangle.X, _ picCanvas3.DisplayRectangle.Y, _ picCanvas3.DisplayRectangle.Width - 1, _ picCanvas3.DisplayRectangle.Height - 1) picCanvas3.Image = bm 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.picCanvas3 = New System.Windows.Forms.PictureBox CType(Me.picCanvas3, System.ruponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() " "picCanvas3 " Me.picCanvas3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.picCanvas3.Location = New System.Drawing.Point(360, 8) Me.picCanvas3.Name = "picCanvas3" Me.picCanvas3.Size = New System.Drawing.Size(168, 168) Me.picCanvas3.TabIndex = 3 Me.picCanvas3.TabStop = False " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(536, 214) Me.Controls.Add(Me.picCanvas3) Me.Name = "Form1" Me.Text = "UsePictureBox" CType(Me.picCanvas3, System.ruponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub Friend WithEvents picCanvas3 As System.Windows.Forms.PictureBox
End Class
</source>
Component Client Rectangle and Display Rectangle
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 Label1.Bounds = GroupBox1.ClientRectangle Label2.Bounds = GroupBox1.DisplayRectangle 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.GroupBox1 = New System.Windows.Forms.GroupBox Me.GroupBox2 = New System.Windows.Forms.GroupBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.GroupBox1.SuspendLayout() Me.GroupBox2.SuspendLayout() Me.SuspendLayout() " "GroupBox1 " Me.GroupBox1.Controls.Add(Me.Label2) Me.GroupBox1.Controls.Add(Me.Label1) Me.GroupBox1.Location = New System.Drawing.Point(8, 16) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(136, 128) Me.GroupBox1.TabIndex = 0 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "GroupBox1" " "GroupBox2 " Me.GroupBox2.Controls.Add(Me.Label3) Me.GroupBox2.Controls.Add(Me.Label4) Me.GroupBox2.Location = New System.Drawing.Point(152, 16) Me.GroupBox2.Name = "GroupBox2" Me.GroupBox2.Size = New System.Drawing.Size(136, 128) Me.GroupBox2.TabIndex = 1 Me.GroupBox2.TabStop = False Me.GroupBox2.Text = "GroupBox2" " "Label1 " Me.Label1.BackColor = System.Drawing.Color.White Me.Label1.Location = New System.Drawing.Point(8, 24) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(104, 16) Me.Label1.TabIndex = 0 Me.Label1.Text = "Label1" " "Label2 " Me.Label2.BackColor = System.Drawing.Color.Gray Me.Label2.Location = New System.Drawing.Point(8, 48) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(104, 16) Me.Label2.TabIndex = 1 Me.Label2.Text = "Label2" " "Label3 " Me.Label3.BackColor = System.Drawing.Color.Gray Me.Label3.Location = New System.Drawing.Point(16, 24) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(104, 16) Me.Label3.TabIndex = 3 Me.Label3.Text = "Label3" " "Label4 " Me.Label4.BackColor = System.Drawing.Color.White Me.Label4.Location = New System.Drawing.Point(16, 48) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(104, 16) Me.Label4.TabIndex = 2 Me.Label4.Text = "Label4" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(297, 153) Me.Controls.Add(Me.GroupBox2) Me.Controls.Add(Me.GroupBox1) Me.Name = "Form1" Me.Text = "DisplayRectangles" Me.GroupBox1.ResumeLayout(False) Me.GroupBox2.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label
End Class
</source>
Control Paint Demo
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO 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
- 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. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() " "ControlPaintExample " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Name = "ControlPaintExample" Me.Text = "ControlPaint Example" End Sub
- End Region
Private Sub ControlPaintExample_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint ControlPaint.DrawCheckBox(e.Graphics, New Rectangle(10, 10, 50, 50), ButtonState.Checked) ControlPaint.DrawCheckBox(e.Graphics, New Rectangle(70, 10, 30, 30), ButtonState.Normal) ControlPaint.DrawCheckBox(e.Graphics, New Rectangle(110, 10, 20, 20), ButtonState.Checked) ControlPaint.DrawButton(e.Graphics, New Rectangle(10, 80, 20, 20), ButtonState.Checked) ControlPaint.DrawButton(e.Graphics, New Rectangle(50, 80, 20, 20), ButtonState.Flat) ControlPaint.DrawButton(e.Graphics, New Rectangle(90, 80, 20, 20), ButtonState.Normal) ControlPaint.DrawFocusRectangle(e.Graphics, New Rectangle(130, 80, 20, 20)) ControlPaint.DrawGrid(e.Graphics, New Rectangle(10, 120, 250, 50), New Size(5, 5), Color.Blue) ControlPaint.DrawScrollButton(e.Graphics, New Rectangle(10, 180, 20, 20), ScrollButton.Left, ButtonState.Normal) ControlPaint.DrawScrollButton(e.Graphics, New Rectangle(50, 180, 20, 20), ScrollButton.Max, ButtonState.Pushed) ControlPaint.DrawScrollButton(e.Graphics, New Rectangle(90, 180, 20, 20), ScrollButton.Up, ButtonState.Normal) ControlPaint.DrawMenuGlyph(e.Graphics, New Rectangle(10, 220, 20, 20), MenuGlyph.Arrow) ControlPaint.DrawMenuGlyph(e.Graphics, New Rectangle(50, 220, 20, 20), MenuGlyph.Checkmark) ControlPaint.DrawMenuGlyph(e.Graphics, New Rectangle(90, 220, 20, 20), MenuGlyph.Max) End Sub
End Class
</source>
Convert object to Control Object
<source lang="vbnet"> Imports System Imports System.Windows.Forms
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
- 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 Button1 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.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() " "TextBox1 " Me.TextBox1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TextBox1.Location = New System.Drawing.Point(16, 60) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(156, 21) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "TextBox1" " "Label1 " Me.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System Me.Label1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(16, 96) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(112, 24) Me.Label1.TabIndex = 2 Me.Label1.Text = "Label1" " "Button1 " Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System Me.Button1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button1.Location = New System.Drawing.Point(16, 20) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 28) Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(280, 186) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.TextBox1, Me.Button1}) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "Form1" Me.Text = "Control Medley" Me.ResumeLayout(False) End Sub
- End Region
Private Sub ctrlClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click, TextBox1.Click, Label1.Click, Button1.Click Dim ctrl As Control = CType(sender, Control) MessageBox.Show("You clicked: " & ctrl.Name) End Sub
End Class
</source>
Display Names for All controls on a Frame
<source lang="vbnet"> Imports System Imports System.IO Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim form1 As Form1 = new Form1() Application.Run(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 For Each ctl As Control In Me.Controls Console.WriteLine(ctl.Name) Next ctl 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.lstDrives = New System.Windows.Forms.ListBox Me.Label1 = New System.Windows.Forms.Label Me.lblAvailableFreeSpace = New System.Windows.Forms.Label Me.lblDriveFormat = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.lblIsReady = New System.Windows.Forms.Label Me.Label6 = New System.Windows.Forms.Label Me.lblDriveType = New System.Windows.Forms.Label Me.Label8 = New System.Windows.Forms.Label Me.lblVolumeLabel = New System.Windows.Forms.Label Me.Label10 = New System.Windows.Forms.Label Me.lblTotalFreeSpace = New System.Windows.Forms.Label Me.Label12 = New System.Windows.Forms.Label Me.lblRootDirectory = New System.Windows.Forms.Label Me.Label14 = New System.Windows.Forms.Label Me.lblName = New System.Windows.Forms.Label Me.Label16 = New System.Windows.Forms.Label Me.SuspendLayout() " "lstDrives " Me.lstDrives.Dock = System.Windows.Forms.DockStyle.Left Me.lstDrives.FormattingEnabled = True Me.lstDrives.Location = New System.Drawing.Point(0, 0) Me.lstDrives.Name = "lstDrives" Me.lstDrives.Size = New System.Drawing.Size(136, 199) Me.lstDrives.TabIndex = 0 " "Label1 " Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(144, 152) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(104, 13) Me.Label1.TabIndex = 1 Me.Label1.Text = "Available Free Space" " "lblAvailableFreeSpace " Me.lblAvailableFreeSpace.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblAvailableFreeSpace.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblAvailableFreeSpace.Location = New System.Drawing.Point(256, 152) Me.lblAvailableFreeSpace.Name = "lblAvailableFreeSpace" Me.lblAvailableFreeSpace.Size = New System.Drawing.Size(152, 16) Me.lblAvailableFreeSpace.TabIndex = 2 " "lblDriveFormat " Me.lblDriveFormat.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblDriveFormat.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDriveFormat.Location = New System.Drawing.Point(256, 128) Me.lblDriveFormat.Name = "lblDriveFormat" Me.lblDriveFormat.Size = New System.Drawing.Size(152, 16) Me.lblDriveFormat.TabIndex = 4 " "Label4 " Me.Label4.AutoSize = True Me.Label4.Location = New System.Drawing.Point(144, 128) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(63, 13) Me.Label4.TabIndex = 3 Me.Label4.Text = "Drive Format" " "lblIsReady " Me.lblIsReady.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblIsReady.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblIsReady.Location = New System.Drawing.Point(256, 32) Me.lblIsReady.Name = "lblIsReady" Me.lblIsReady.Size = New System.Drawing.Size(152, 16) Me.lblIsReady.TabIndex = 8 " "Label6 " Me.Label6.AutoSize = True Me.Label6.Location = New System.Drawing.Point(144, 32) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(45, 13) Me.Label6.TabIndex = 7 Me.Label6.Text = "Is Ready" " "lblDriveType " Me.lblDriveType.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblDriveType.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDriveType.Location = New System.Drawing.Point(256, 56) Me.lblDriveType.Name = "lblDriveType" Me.lblDriveType.Size = New System.Drawing.Size(152, 16) Me.lblDriveType.TabIndex = 6 " "Label8 " Me.Label8.AutoSize = True Me.Label8.Location = New System.Drawing.Point(144, 56) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(55, 13) Me.Label8.TabIndex = 5 Me.Label8.Text = "Drive Type" " "lblVolumeLabel " Me.lblVolumeLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblVolumeLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblVolumeLabel.Location = New System.Drawing.Point(256, 104) Me.lblVolumeLabel.Name = "lblVolumeLabel" Me.lblVolumeLabel.Size = New System.Drawing.Size(152, 16) Me.lblVolumeLabel.TabIndex = 16 " "Label10 " Me.Label10.AutoSize = True Me.Label10.Location = New System.Drawing.Point(144, 104) Me.Label10.Name = "Label10" Me.Label10.Size = New System.Drawing.Size(67, 13) Me.Label10.TabIndex = 15 Me.Label10.Text = "Volume Label" " "lblTotalFreeSpace " Me.lblTotalFreeSpace.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblTotalFreeSpace.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTotalFreeSpace.Location = New System.Drawing.Point(256, 176) Me.lblTotalFreeSpace.Name = "lblTotalFreeSpace" Me.lblTotalFreeSpace.Size = New System.Drawing.Size(152, 16) Me.lblTotalFreeSpace.TabIndex = 14 " "Label12 " Me.Label12.AutoSize = True Me.Label12.Location = New System.Drawing.Point(144, 176) Me.Label12.Name = "Label12" Me.Label12.Size = New System.Drawing.Size(85, 13) Me.Label12.TabIndex = 13 Me.Label12.Text = "Total Free Space" " "lblRootDirectory " Me.lblRootDirectory.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblRootDirectory.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblRootDirectory.Location = New System.Drawing.Point(256, 80) Me.lblRootDirectory.Name = "lblRootDirectory" Me.lblRootDirectory.Size = New System.Drawing.Size(152, 16) Me.lblRootDirectory.TabIndex = 12 " "Label14 " Me.Label14.AutoSize = True Me.Label14.Location = New System.Drawing.Point(144, 80) Me.Label14.Name = "Label14" Me.Label14.Size = New System.Drawing.Size(71, 13) Me.Label14.TabIndex = 11 Me.Label14.Text = "Root Directory" " "lblName " Me.lblName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblName.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblName.Location = New System.Drawing.Point(256, 8) Me.lblName.Name = "lblName" Me.lblName.Size = New System.Drawing.Size(152, 16) Me.lblName.TabIndex = 10 " "Label16 " Me.Label16.AutoSize = True Me.Label16.Location = New System.Drawing.Point(144, 8) Me.Label16.Name = "Label16" Me.Label16.Size = New System.Drawing.Size(31, 13) Me.Label16.TabIndex = 9 Me.Label16.Text = "Name" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(411, 200) Me.Controls.Add(Me.lblVolumeLabel) Me.Controls.Add(Me.Label10) Me.Controls.Add(Me.lblTotalFreeSpace) Me.Controls.Add(Me.Label12) Me.Controls.Add(Me.lblRootDirectory) Me.Controls.Add(Me.Label14) Me.Controls.Add(Me.lblName) Me.Controls.Add(Me.Label16) Me.Controls.Add(Me.lblIsReady) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.lblDriveType) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.lblDriveFormat) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.lblAvailableFreeSpace) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstDrives) Me.Name = "Form1" Me.Text = "GetDriveInfo" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents lstDrives As System.Windows.Forms.ListBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lblAvailableFreeSpace As System.Windows.Forms.Label Friend WithEvents lblDriveFormat As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents lblIsReady As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents lblDriveType As System.Windows.Forms.Label Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents lblVolumeLabel As System.Windows.Forms.Label Friend WithEvents Label10 As System.Windows.Forms.Label Friend WithEvents lblTotalFreeSpace As System.Windows.Forms.Label Friend WithEvents Label12 As System.Windows.Forms.Label Friend WithEvents lblRootDirectory As System.Windows.Forms.Label Friend WithEvents Label14 As System.Windows.Forms.Label Friend WithEvents lblName As System.Windows.Forms.Label Friend WithEvents Label16 As System.Windows.Forms.Label
End Class
</source>
Get Control Handle and check its value
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms 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
- 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 listBox1 As System.Windows.Forms.ListBox Friend WithEvents chkMulti As System.Windows.Forms.CheckBox Friend WithEvents lblWindow As System.Windows.Forms.Label "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.chkMulti = New System.Windows.Forms.CheckBox() Me.lblWindow = New System.Windows.Forms.Label() Me.listBox1 = New System.Windows.Forms.ListBox() Me.SuspendLayout() " "chkMulti " Me.chkMulti.Location = New System.Drawing.Point(192, 64) Me.chkMulti.Name = "chkMulti" Me.chkMulti.Size = New System.Drawing.Size(80, 16) Me.chkMulti.TabIndex = 1 Me.chkMulti.Text = "MultiSelect" " "lblWindow " Me.lblWindow.Location = New System.Drawing.Point(32, 192) Me.lblWindow.Name = "lblWindow" Me.lblWindow.Size = New System.Drawing.Size(232, 16) Me.lblWindow.TabIndex = 2 " "listBox1 " Me.listBox1.Location = New System.Drawing.Point(24, 24) Me.listBox1.Name = "listBox1" Me.listBox1.Size = New System.Drawing.Size(144, 160) Me.listBox1.TabIndex = 0 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 216) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblWindow, Me.chkMulti, Me.listBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer For x = 1 To 20 listBox1().Items.Add("Entry # " & CStr(x)) Next lblWindow().Text = "hWnd = " & listBox1().Handle.ToString End Sub Private Sub chkMulti_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMulti.CheckedChanged If chkMulti().CheckState = CheckState.Checked Then listBox1().SelectionMode = SelectionMode.MultiExtended Else listBox1().SelectionMode = SelectionMode.One End If lblWindow().Text = "hWnd = " & listBox1().Handle.ToString End Sub
End Class
</source>
Get Labels from all Controls on a Form
<source lang="vbnet"> Imports System Imports System.IO Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim form1 As Form1 = new Form1() Application.Run(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 For Each ctl As Control In Me.Controls If (ctl.Name.StartsWith("lbl")) AndAlso (TypeOf ctl Is Label) Then Dim lbl As Label = DirectCast(ctl, Label) lbl.Text = "" End If Next ctl 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.lstDrives = New System.Windows.Forms.ListBox Me.Label1 = New System.Windows.Forms.Label Me.lblAvailableFreeSpace = New System.Windows.Forms.Label Me.lblDriveFormat = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.lblIsReady = New System.Windows.Forms.Label Me.Label6 = New System.Windows.Forms.Label Me.lblDriveType = New System.Windows.Forms.Label Me.Label8 = New System.Windows.Forms.Label Me.lblVolumeLabel = New System.Windows.Forms.Label Me.Label10 = New System.Windows.Forms.Label Me.lblTotalFreeSpace = New System.Windows.Forms.Label Me.Label12 = New System.Windows.Forms.Label Me.lblRootDirectory = New System.Windows.Forms.Label Me.Label14 = New System.Windows.Forms.Label Me.lblName = New System.Windows.Forms.Label Me.Label16 = New System.Windows.Forms.Label Me.SuspendLayout() " "lstDrives " Me.lstDrives.Dock = System.Windows.Forms.DockStyle.Left Me.lstDrives.FormattingEnabled = True Me.lstDrives.Location = New System.Drawing.Point(0, 0) Me.lstDrives.Name = "lstDrives" Me.lstDrives.Size = New System.Drawing.Size(136, 199) Me.lstDrives.TabIndex = 0 " "Label1 " Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(144, 152) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(104, 13) Me.Label1.TabIndex = 1 Me.Label1.Text = "Available Free Space" " "lblAvailableFreeSpace " Me.lblAvailableFreeSpace.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblAvailableFreeSpace.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblAvailableFreeSpace.Location = New System.Drawing.Point(256, 152) Me.lblAvailableFreeSpace.Name = "lblAvailableFreeSpace" Me.lblAvailableFreeSpace.Size = New System.Drawing.Size(152, 16) Me.lblAvailableFreeSpace.TabIndex = 2 " "lblDriveFormat " Me.lblDriveFormat.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblDriveFormat.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDriveFormat.Location = New System.Drawing.Point(256, 128) Me.lblDriveFormat.Name = "lblDriveFormat" Me.lblDriveFormat.Size = New System.Drawing.Size(152, 16) Me.lblDriveFormat.TabIndex = 4 " "Label4 " Me.Label4.AutoSize = True Me.Label4.Location = New System.Drawing.Point(144, 128) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(63, 13) Me.Label4.TabIndex = 3 Me.Label4.Text = "Drive Format" " "lblIsReady " Me.lblIsReady.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblIsReady.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblIsReady.Location = New System.Drawing.Point(256, 32) Me.lblIsReady.Name = "lblIsReady" Me.lblIsReady.Size = New System.Drawing.Size(152, 16) Me.lblIsReady.TabIndex = 8 " "Label6 " Me.Label6.AutoSize = True Me.Label6.Location = New System.Drawing.Point(144, 32) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(45, 13) Me.Label6.TabIndex = 7 Me.Label6.Text = "Is Ready" " "lblDriveType " Me.lblDriveType.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblDriveType.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDriveType.Location = New System.Drawing.Point(256, 56) Me.lblDriveType.Name = "lblDriveType" Me.lblDriveType.Size = New System.Drawing.Size(152, 16) Me.lblDriveType.TabIndex = 6 " "Label8 " Me.Label8.AutoSize = True Me.Label8.Location = New System.Drawing.Point(144, 56) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(55, 13) Me.Label8.TabIndex = 5 Me.Label8.Text = "Drive Type" " "lblVolumeLabel " Me.lblVolumeLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblVolumeLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblVolumeLabel.Location = New System.Drawing.Point(256, 104) Me.lblVolumeLabel.Name = "lblVolumeLabel" Me.lblVolumeLabel.Size = New System.Drawing.Size(152, 16) Me.lblVolumeLabel.TabIndex = 16 " "Label10 " Me.Label10.AutoSize = True Me.Label10.Location = New System.Drawing.Point(144, 104) Me.Label10.Name = "Label10" Me.Label10.Size = New System.Drawing.Size(67, 13) Me.Label10.TabIndex = 15 Me.Label10.Text = "Volume Label" " "lblTotalFreeSpace " Me.lblTotalFreeSpace.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblTotalFreeSpace.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTotalFreeSpace.Location = New System.Drawing.Point(256, 176) Me.lblTotalFreeSpace.Name = "lblTotalFreeSpace" Me.lblTotalFreeSpace.Size = New System.Drawing.Size(152, 16) Me.lblTotalFreeSpace.TabIndex = 14 " "Label12 " Me.Label12.AutoSize = True Me.Label12.Location = New System.Drawing.Point(144, 176) Me.Label12.Name = "Label12" Me.Label12.Size = New System.Drawing.Size(85, 13) Me.Label12.TabIndex = 13 Me.Label12.Text = "Total Free Space" " "lblRootDirectory " Me.lblRootDirectory.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblRootDirectory.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblRootDirectory.Location = New System.Drawing.Point(256, 80) Me.lblRootDirectory.Name = "lblRootDirectory" Me.lblRootDirectory.Size = New System.Drawing.Size(152, 16) Me.lblRootDirectory.TabIndex = 12 " "Label14 " Me.Label14.AutoSize = True Me.Label14.Location = New System.Drawing.Point(144, 80) Me.Label14.Name = "Label14" Me.Label14.Size = New System.Drawing.Size(71, 13) Me.Label14.TabIndex = 11 Me.Label14.Text = "Root Directory" " "lblName " Me.lblName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblName.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblName.Location = New System.Drawing.Point(256, 8) Me.lblName.Name = "lblName" Me.lblName.Size = New System.Drawing.Size(152, 16) Me.lblName.TabIndex = 10 " "Label16 " Me.Label16.AutoSize = True Me.Label16.Location = New System.Drawing.Point(144, 8) Me.Label16.Name = "Label16" Me.Label16.Size = New System.Drawing.Size(31, 13) Me.Label16.TabIndex = 9 Me.Label16.Text = "Name" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(411, 200) Me.Controls.Add(Me.lblVolumeLabel) Me.Controls.Add(Me.Label10) Me.Controls.Add(Me.lblTotalFreeSpace) Me.Controls.Add(Me.Label12) Me.Controls.Add(Me.lblRootDirectory) Me.Controls.Add(Me.Label14) Me.Controls.Add(Me.lblName) Me.Controls.Add(Me.Label16) Me.Controls.Add(Me.lblIsReady) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.lblDriveType) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.lblDriveFormat) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.lblAvailableFreeSpace) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lstDrives) Me.Name = "Form1" Me.Text = "GetDriveInfo" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents lstDrives As System.Windows.Forms.ListBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lblAvailableFreeSpace As System.Windows.Forms.Label Friend WithEvents lblDriveFormat As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents lblIsReady As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents lblDriveType As System.Windows.Forms.Label Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents lblVolumeLabel As System.Windows.Forms.Label Friend WithEvents Label10 As System.Windows.Forms.Label Friend WithEvents lblTotalFreeSpace As System.Windows.Forms.Label Friend WithEvents Label12 As System.Windows.Forms.Label Friend WithEvents lblRootDirectory As System.Windows.Forms.Label Friend WithEvents Label14 As System.Windows.Forms.Label Friend WithEvents lblName As System.Windows.Forms.Label Friend WithEvents Label16 As System.Windows.Forms.Label
End Class
</source>