Adding controls withevents at runtime in VB .NET

Submitted on: 1/1/2015 2:34:00 AM
By: Sahand (from psc cd)  
Level: Beginner
User Rating: By 13 Users
Compatibility: VB.NET
Views: 11631
     Sometimes it is necessary to add controls at runtime or use a dynamic array of controls and you want to have events too , this tutorial will show you how to do this.

This article has accompanying files

Adding controls at runtime

First of all you need to declare an array of that control but you can't do that by the withevents statement so you first need to create a class that has the events in itself then you delare an array of that class. in the examples below i will make a program with a button that when it is pressed it will add a textbox to the form in a random location. here's the definition of the class:
(in this example only the TextChanged event is rewritten but you can do it with all the events)

Public Class MyTextBox 
Inherits System.Windows.Forms.TextBox 
Private Sub MyTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.TextChanged 
MsgBox(Me.Name & "'s text changed!") 
End Sub
End Class

Then you declare an array of this class in the Form class like this:

Private TextBoxes() as MyTextBox

then assuming that you have a Button with the name of 'Button1' you write the following code in the Clicked event:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static num As Integer
num += 1
ReDim TextBoxes(num)
TextBoxes(num - 1) = New MyTextBox()
With TextBoxes(num - 1)
 .Name = "Textbox" & num
 .Text = "Textbox" & num
 .Location = New System.Drawing.Point(10, num * (.Height + 10))
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {TextBoxes(num - 1)}) End Sub End Class

then TextBox appears on the form , so that's it.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Other 7 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.