Add Label to a form
Imports System.Windows.Forms
public class AddNewLabelToForm
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
Dim lbl As New Label
lbl.SetBounds(10, 50, 100, 25)
lbl.Text = "Hello World!"
Me.Controls.Add(lbl)
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
Add TextBox to Form in code
Imports System.Windows.Forms
public class AddTextBoxForm
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dynamicText As TextBox = Nothing
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "TimeTextBox"
dynamicText.Location = New System.Drawing.Point(8, 8)
dynamicText.Size = New System.Drawing.Size(232, 20)
dynamicText.TabIndex = 0
Me.Controls.Add(dynamicText)
Dim theTextBox As TextBox
theTextBox = Me.Controls("TimeTextBox")
If (theTextBox IsNot Nothing) Then
theTextBox.Text = Now.ToLongTimeString()
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.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(260, 76)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "Dynamic Controls"
Me.ResumeLayout(False)
End Sub
End Class
Add the Button control to the Form Controls collection
imports System
imports System.Drawing
imports System.Windows.Forms
public class ControlParent : inherits Form
Private WithEvents btn as Button
public sub New()
btn = new Button()
btn.Location = new Point(50,50)
btn.Size = new Size(100,23)
btn.Text = "Relationships"
Controls.Add(btn)
btn.Parent = me
end sub
public shared sub Main()
Application.Run(new ControlParent())
end sub
private sub btn_Click(ByVal sender as object,ByVal e as EventArgs) Handles btn.Click
Console.WriteLine("Button Parent: " + btn.Parent.ToString())
Console.WriteLine("Button HasChildren: " + btn.HasChildren.ToString())
Console.WriteLine("TopLevelControl: " + btn.TopLevelControl.ToString())
Console.WriteLine("Form HasChildren: " + me.HasChildren.ToString())
Console.WriteLine("Form Controls Count: " + me.Controls.Count.ToString())
end sub
end class
Cast event sender to control
Option Strict On
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1 : Inherits Form
" Instantiate buttons
Public WithEvents btnOK As New Button()
Public WithEvents btnCancel As New Button()
Public WithEvents btnQuit As New Button()
" Application entry point
Public Shared Sub Main()
Dim frm As New Form1()
frm.ShowDialog()
End Sub
" Class constructor
Public Sub New()
MyBase.New()
" Define button sizes and locations
Me.btnOK.Location = New Point(100, 50)
Me.btnOK.Size = New Size(100, 50)
Me.btnOK.Text = "OK"
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnCancel.Location = New Point(100, 125)
Me.btnCancel.Size = New Size(100, 50)
Me.btnCancel.Text = "Cancel"
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnQuit.Location = New Point(100, 200)
Me.btnQuit.Size = New Size(100, 50)
Me.btnQuit.Text = "Exit"
Me.btnQuit.DialogResult = System.Windows.Forms.DialogResult.Abort
" Define form controls and caption
Me.Controls.Add(btnOK)
Me.Controls.Add(btnCancel)
Me.Controls.Add(btnQuit)
Me.Text = "Button Click Events"
End Sub
" Event handler for all three buttons
Private Sub ButtonClicked(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnOK.Click, btnCancel.Click, btnQuit.Click
If TypeOf sender Is Button Then
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "btnOK" Then
Console.WriteLine("btnOK")
ElseIf btn.Name = "btnCancel" Then
Console.WriteLine("Cancel")
Exit Sub
Else
Me.Close()
End If
Else
Throw New ArgumentException( _
"The event was raised by an invalid object.")
End If
End Sub
End Class
Check Control type
Option Strict On
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1 : Inherits Form
" Instantiate buttons
Public WithEvents btnOK As New Button()
Public WithEvents btnCancel As New Button()
Public WithEvents btnQuit As New Button()
" Application entry point
Public Shared Sub Main()
Dim frm As New Form1()
frm.ShowDialog()
End Sub
" Class constructor
Public Sub New()
MyBase.New()
" Define button sizes and locations
Me.btnOK.Location = New Point(100, 50)
Me.btnOK.Size = New Size(100, 50)
Me.btnOK.Text = "OK"
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnCancel.Location = New Point(100, 125)
Me.btnCancel.Size = New Size(100, 50)
Me.btnCancel.Text = "Cancel"
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnQuit.Location = New Point(100, 200)
Me.btnQuit.Size = New Size(100, 50)
Me.btnQuit.Text = "Exit"
Me.btnQuit.DialogResult = System.Windows.Forms.DialogResult.Abort
" Define form controls and caption
Me.Controls.Add(btnOK)
Me.Controls.Add(btnCancel)
Me.Controls.Add(btnQuit)
Me.Text = "Button Click Events"
End Sub
" Event handler for all three buttons
Private Sub ButtonClicked(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnOK.Click, btnCancel.Click, btnQuit.Click
If TypeOf sender Is Button Then
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "btnOK" Then
Console.WriteLine("btnOK")
ElseIf btn.Name = "btnCancel" Then
Console.WriteLine("Cancel")
Exit Sub
Else
Me.Close()
End If
Else
Throw New ArgumentException( _
"The event was raised by an invalid object.")
End If
End Sub
End Class
Hide a control
Imports System.Windows.Forms
public class HideControl
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Public val1 As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If val1 = 1 Then
Button1.Visible = False
val1 = 0
Button2.Text = "Button1 Hidden"
Else
Button1.Visible = True
val1 = 1
Button2.Text = "Click Button2 to hide button1"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(105, 65)
Me.Button1.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(253, 40)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
Me.Button1.Visible = False
"
"Button2
"
Me.Button2.Location = New System.Drawing.Point(105, 162)
Me.Button2.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(253, 41)
Me.Button2.TabIndex = 1
Me.Button2.Text = "click Button2 to hide button1"
Me.Button2.UseVisualStyleBackColor = True
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(473, 300)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
End Class
Read and Save controls on a form to a file
Option Strict On
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class SaveReadControlsFile
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
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents btnChangeColor As System.Windows.Forms.Button
Friend WithEvents btnChangeFont As System.Windows.Forms.Button
Friend WithEvents btnSaveRTF As System.Windows.Forms.Button
Friend WithEvents btnOpenRTF As System.Windows.Forms.Button
Friend WithEvents btnWriteControls As System.Windows.Forms.Button
Friend WithEvents btnReadControls As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
"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.RichTextBox1 = New System.Windows.Forms.RichTextBox()
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog()
Me.FontDialog1 = New System.Windows.Forms.FontDialog()
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.btnChangeColor = New System.Windows.Forms.Button()
Me.btnChangeFont = New System.Windows.Forms.Button()
Me.btnSaveRTF = New System.Windows.Forms.Button()
Me.btnOpenRTF = New System.Windows.Forms.Button()
Me.btnWriteControls = New System.Windows.Forms.Button()
Me.btnReadControls = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.TextBox3 = New System.Windows.Forms.TextBox()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
"
"Panel1
"
Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnReadControls, Me.btnWriteControls, Me.btnOpenRTF, Me.btnSaveRTF, Me.btnChangeFont, Me.btnChangeColor})
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Panel1.Location = New System.Drawing.Point(0, 222)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(656, 64)
Me.Panel1.TabIndex = 0
"
"RichTextBox1
"
Me.RichTextBox1.Location = New System.Drawing.Point(16, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(336, 176)
Me.RichTextBox1.TabIndex = 1
Me.RichTextBox1.Text = "Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they w" & _
"ere perfectly normal, thank you very much."
"
"btnChangeColor
"
Me.btnChangeColor.Location = New System.Drawing.Point(24, 24)
Me.btnChangeColor.Name = "btnChangeColor"
Me.btnChangeColor.Size = New System.Drawing.Size(88, 32)
Me.btnChangeColor.TabIndex = 0
Me.btnChangeColor.Text = "Change Color"
"
"btnChangeFont
"
Me.btnChangeFont.Location = New System.Drawing.Point(128, 24)
Me.btnChangeFont.Name = "btnChangeFont"
Me.btnChangeFont.Size = New System.Drawing.Size(88, 32)
Me.btnChangeFont.TabIndex = 1
Me.btnChangeFont.Text = "Change Font"
"
"btnSaveRTF
"
Me.btnSaveRTF.Location = New System.Drawing.Point(232, 24)
Me.btnSaveRTF.Name = "btnSaveRTF"
Me.btnSaveRTF.Size = New System.Drawing.Size(88, 32)
Me.btnSaveRTF.TabIndex = 2
Me.btnSaveRTF.Text = "Save RTF"
"
"btnOpenRTF
"
Me.btnOpenRTF.Location = New System.Drawing.Point(336, 24)
Me.btnOpenRTF.Name = "btnOpenRTF"
Me.btnOpenRTF.Size = New System.Drawing.Size(88, 32)
Me.btnOpenRTF.TabIndex = 3
Me.btnOpenRTF.Text = "Open RTF"
"
"btnWriteControls
"
Me.btnWriteControls.Location = New System.Drawing.Point(440, 24)
Me.btnWriteControls.Name = "btnWriteControls"
Me.btnWriteControls.Size = New System.Drawing.Size(88, 32)
Me.btnWriteControls.TabIndex = 4
Me.btnWriteControls.Text = "Write Controls"
"
"btnReadControls
"
Me.btnReadControls.Location = New System.Drawing.Point(544, 24)
Me.btnReadControls.Name = "btnReadControls"
Me.btnReadControls.Size = New System.Drawing.Size(88, 32)
Me.btnReadControls.TabIndex = 5
Me.btnReadControls.Text = "Read Controls"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(400, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(232, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
"
"TextBox2
"
Me.TextBox2.Location = New System.Drawing.Point(400, 68)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(232, 20)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
"
"TextBox3
"
Me.TextBox3.Location = New System.Drawing.Point(400, 112)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(232, 20)
Me.TextBox3.TabIndex = 4
Me.TextBox3.Text = "TextBox3"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(656, 286)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox3, Me.TextBox2, Me.TextBox1, Me.RichTextBox1, Me.Panel1})
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnWriteControls_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnWriteControls.Click
"Get a file
Dim theFile As String
SaveFileDialog1.InitialDirectory = Application.ExecutablePath
SaveFileDialog1.DefaultExt = "hld" "custom format
SaveFileDialog1.FileName = "myFile"
SaveFileDialog1.Filter = "HLD Files (*.hld)|*.hld|All Files (*.*) | *.*"
SaveFileDialog1.OverwritePrompt = True
SaveFileDialog1.ShowDialog()
theFile = SaveFileDialog1.FileName
Dim fs As FileStream = New FileStream(theFile, FileMode.OpenOrCreate)
Dim w As StreamWriter = New StreamWriter(fs)
Dim c As Object
For Each c In Me.Controls
If TypeOf (c) Is TextBox Then
Dim tbox As TextBox = CType(c, TextBox)
w.WriteLine(tbox.Name & "=" & tbox.Text)
End If
Next
w.Close()
fs.Close()
End Sub
Private Sub btnReadControls_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnReadControls.Click
Dim theFile As String
OpenFileDialog1.InitialDirectory = Application.ExecutablePath
OpenFileDialog1.DefaultExt = "hld" "custom format
OpenFileDialog1.FileName = "myFile"
OpenFileDialog1.Filter = "HLD Files (*.hld)|*.hld|All Files (*.*) | *.*"
OpenFileDialog1.ShowDialog()
theFile = OpenFileDialog1.FileName
Dim fs As FileStream = New FileStream(theFile, FileMode.Open)
Dim r As StreamReader = New StreamReader(fs)
Dim c As Object
Dim tmpArray() As String
For Each c In Me.Controls
If TypeOf (c) Is TextBox Then
Dim tbox As TextBox = CType(c, TextBox)
tmpArray = Split(r.ReadLine(), "=")
If tbox.Name = tmpArray(0) Then
tbox.Text = tmpArray(1)
End If
End If
Next
r.Close()
End Sub
End Class
Set Control Parent
imports System
imports System.Drawing
imports System.Windows.Forms
public class ControlParent : inherits Form
Private WithEvents btn as Button
public sub New()
btn = new Button()
btn.Location = new Point(50,50)
btn.Size = new Size(100,23)
btn.Text = "Relationships"
Controls.Add(btn)
btn.Parent = me
end sub
public shared sub Main()
Application.Run(new ControlParent())
end sub
private sub btn_Click(ByVal sender as object,ByVal e as EventArgs) Handles btn.Click
Console.WriteLine("Button Parent: " + btn.Parent.ToString())
Console.WriteLine("Button HasChildren: " + btn.HasChildren.ToString())
Console.WriteLine("TopLevelControl: " + btn.TopLevelControl.ToString())
Console.WriteLine("Form HasChildren: " + me.HasChildren.ToString())
Console.WriteLine("Form Controls Count: " + me.Controls.Count.ToString())
end sub
end class
Use Constrol"s Tag
imports System
imports System.Drawing
imports System.Windows.Forms
public class Tags : inherits Form
private lbl as Label
public sub New()
Size = new Size(300,200)
lbl = new Label()
lbl.Text = "Label..."
lbl.AutoSize = true
lbl.Parent = me
lbl.Location = new Point(10,10)
dim theEnum as new FontStyle()
dim theStyles as FontStyle() = CType([Enum].GetValues(theEnum.GetType()), FontStyle())
dim i as integer = 1
dim style as FontStyle
for each style in theStyles
dim btn as new Button()
btn.Parent = me
btn.Location = new Point(25,25 * i)
btn.Size = new Size(75,20)
btn.Text = style.ToString()
btn.Tag = style
AddHandler btn.Click, AddressOf btn_Click
i += 1
next
end sub
public shared sub Main()
Application.Run(new Tags())
end sub
private sub btn_Click(ByVal sender as object,ByVal e as EventArgs)
dim btn as Button = CType(sender, Button)
dim fs as FontStyle = CType(btn.Tag, FontStyle)
lbl.Font = new Font(lbl.Font, fs)
end sub
end class