Important alert: (current site time 7/15/2013 8:18:44 AM EDT)
 

article

Adding controls withevents at runtime in VB .NET

Email
Submitted on: 3/20/2002 10:35:38 AM
By: Sahand  
Level: Beginner
User Rating: By 13 Users
Compatibility: VB.NET
Views: 56957
(About the author)
 
     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
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

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 including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 9 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

5/22/2002 5:09:32 AMJoe Bourne

Cool code - wish MS would bring back control arrays like we had in the good old days of VB6!!
(If this comment was disrespectful, please report it.)

 
7/4/2002 11:59:39 PMraxix

I tried to do the samethng in asp.net but cannot..do u have any idea?
(If this comment was disrespectful, please report it.)

 
12/15/2003 3:01:40 PMreturnvoid

To raxix:
The code shared by Sahand will work under ASP.NET, however with some minor modifications.
Note that the textbox webcontrol for ASP.NET is NOT the same with the textbox control for winforms. This applies to all webcontrols. So some textbox properties used in this article are not available to ASP.NET (such as for example .Name and .Location).
Also for ASP.NET we need to set an ID for each control that we programmatically create during runtime.

eg: Textboxes(num).ID = "txtBox" + trim(str(num))

Also we need to set the control to visible so that it will appear on the Page.

eg: Textboxes(num).Visible = TRUE

And finally but most importantly we must add the control the the page, webform, panel, placeholder.

eg: Panel1.controls.Add(Textboxes(num))

Note: I beleive the following statement
Me.Controls.AddRange(New System.Windows.Forms.Control() {TextBoxes(num - 1)})
will not be accepted in ASP.NET and it is not needed.
(If this comment was disrespectful, please report it.)

 
5/13/2004 6:25:21 AM

I think this code is very simple and easy to use. Thanks a lot. Searched the help files but couldn't find anything like this. Thx, Gd blss!
(If this comment was disrespectful, please report it.)

 
8/23/2004 6:48:07 AM

This is really good code. I was searching for this for quit a while
(If this comment was disrespectful, please report it.)

 
10/18/2004 6:39:38 AMRobert Håkansson

Great code! Thank you!
(If this comment was disrespectful, please report it.)

 
2/5/2005 2:57:55 AM

it is realy good
(If this comment was disrespectful, please report it.)

 
2/25/2005 5:46:16 PMSahand

***Important Note From The Writer*****

Please see the other submision that I have with a similar name before you use this code because in most cases, the way suggested in the other submission is better. Thanks.
(If this comment was disrespectful, please report it.)

 
2/28/2007 3:28:03 AMsudha

very nice
(If this comment was disrespectful, please report it.)

 

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.