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.
Terms of Agreement:
By using this article, you agree to the following terms...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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
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:
Re-scan downloaded files using your personal virus checker before using it.
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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
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.)
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.)
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.)