VB 2010 Create Controls at Run-Time with events
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
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
Thank you for reading ....
♥ Here are some online Visual Basic lessons and courses :
- Visual Basic .Net - How to check for the internet connection
- Visual Basic .Net - POP3 and Receiving E-mails
- Visual Basic .Net - Generate Random Combinations
- Visual Basic .Net - Play sounds on Button Click or Mouse Hover
- Visual Basic .Net - Progressbar control
- Visual Basic .Net - The application failed to initialize
- Visual Basic .Net - Working with Database full example
- Visual Basic .Net - There is already an open DataReader associated
- Visual Basic .Net - SyBase Advantage Database [Add, Edit, Search, Delete and DataGridView]
- Visual Basic .Net - Math Functions
- Visual Basic .Net - Communicating with clients [Requirements Docs, Use-Case]
- Visual Basic .Net - Receive Emails using POP3 mail Server