VB 2010 Create Controls at Run-Time with events

How to Create Controls at Run-Time in 

VB 2010

VB 2010 Create controls at run-time
Visual Basic Online Course

Logic

The example will show you how to create a control (TextBox) at the Application Run-Time. This TextBox will allow only Numbers. We will create a new TextBox control with event  Text Changed when the form is clicked

Project Design

Create new VB 2010 Windows Application Project (MyPro), don't forget to always Save your project

How does it work

You RUN your project and then click on the Form (Form1) and then you will notice that a TextBox control was created with the same properties that we provided in our example in the code below.

TextBox Properties

Location : The cursor location when click on the Form1
Size : 120,300
Parent : Form1 (Me)
TextAlign : HorizontalAlignment.Left
ForeColor : Color.White
Name : MyNewText
Font : Times New Roman, 10, Regular, Point

Code
'Visual Basic Online Course
'2014
'VB 2010 Create TextBox Control at Run-Time with events
Public Class Form1
Public MyNewTXT As TextBox = New TextBox
Const FHEIGHT As Integer = 120
Const FWIDTH As Integer = 300
Dim PointX, PointY As Integer
Private Sub Form1_Click(sender As Object, _
e As System.EventArgs) _
Handles Me.Click
Me.AutoScroll = True
MyNewTXT.Name = "MyNewTXT"
MyNewTXT.Parent = Me
MyNewTXT.Size = New Size(FWIDTH, FHEIGHT)
MyNewTXT.Location = New Point(PointX, PointY)
MyNewTXT.TextAlign = HorizontalAlignment.Left
MyNewTXT.ForeColor = Color.White
MyNewTXT.ReadOnly = False
MyNewTXT.Multiline = False
MyNewTXT.Font = New System.Drawing.Font("times new roman", _
10, _
FontStyle.Regular, _
GraphicsUnit.Point)
MyNewTXT.BackColor = Color.Blue
MyNewTXT.Visible = True
'Event Txt_Changed : Triggers on our new TextBox Changed
AddHandler MyNewTXT.TextChanged, AddressOf Txt_Changed
End Sub
Private Sub Txt_Changed(Thissender As Object, _
e As System.EventArgs)
Dim ThisToChange As String = Thissender.text
'Allow only Numbers
If Not IsNumeric(ThisToChange) Then
SendKeys.Send("{BackSpace}")
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, _
e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseMove
PointX = e.Location.X
PointY = e.Location.Y
End Sub
End Class
view raw gistfile1.vb hosted with ❤ by GitHub

Thank you for reading ....

Here are some online Visual Basic lessons and courses :

Popular posts from this blog

VB .NET DropBox Api Source Code Example

VB .NET Google Drive Api Source Code Example

VB.NET Access 2007 Hierarchical TreeView

VB.NET How to properly close a windows application Form

DAO in VB .NET MS ACCESS Database

Solution - There is already an open DataReader associated with this Command which must be closed first.