Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Creating controls dynamically on windows form using visual basic.net

Author: Shahzad Latif
Download Source Code : 569_Creating an Enterprise Services Component.zip

Creating controls dynamically or on-the-fly is very easy in Visual Basic.Net. This article shows you how the controls can be added on a windows form and how to read these controls latter.

Create a New Visual Basic.Net Project and select Windows Application from the templates.

Add a text box and two button controls on the windows form. Name the  controls as txtCount, cmdShowControls, and cmdShowValues.

Add following code in the forms load event

Me.Height = 104

Me.Width = 500

Me.cmdShowValues.visible = False

First two lines of code set the initial height and width of the form.

Now, add following code in the Declarations section of the form

Dim xPos As Integer = 72

Dim yPos As Integer = 72

Dim iWidth As Integer = 128

Dim iHeight As Integer = 23

Dim MyHashTable As New Hashtable

Your form, in the start, will show a text box and a button. When you'll click the button after entering a number in the text box, the form will show the text boxes  according to the number entered in the field. So, you need to add some code in the click event of the cmdShowControls.

Oh, before that, remember i have created a HashTable object. It'll keep track of all the controls you will be creating dynamically, so that latter on you would be able to check which control was created by your code on-the-fly.

Dim iCounter As Integer

Dim MyText As TextBox

If IsNumeric(Me.txtCount.Text) Then

For iCounter = 0 To CInt(Me.txtCount.Text) - 1

MyText = New TextBox

MyText.Text = "My Text Box Number " & iCounter

MyText.Name = "MyText" & iCounter

MyText.Location = New Point(xPos, yPos)

MyText.Size = New Size(iWidth, iHeight)

Me.Controls.Add(MyText)

Me.MyHashTable.Add("MyText" & iCounter, "MyText" & iCounter)

yPos += 30

Next

End If

If IsNumeric(Me.txtCount.Text) Then

Me.Height += CInt(Me.txtCount.Text)  *  30 +  50

Me.txtCount.Text = ""

End If

Me.cmdShowValues.Location = New Point(xPos, yPos)

Me.cmdShowValues.Visible = True

After you have created some controls on your form, you would obviously like to get there values. For that purpose you need to add following code snippet in the click event of the cmdShowValues.

Dim MyControl As Control

Dim MyTextBox As TextBox

For Each MyControl In Me.Controls

If TypeOf (MyControl) Is TextBox Then

MyTextBox = MyControl

If Me.MyHashTable.Contains(MyTextBox.Name) Then

MessageBox.Show(MyTextBox.Text)

End If

End If

Next

Well, this is a simple article about adding controls on a windows form; Nevertheless, you can do much more good work with the help of this article.

Article Comments
Ok the code helps me a lot! thanks..

Posted on 10/10/2006 12:25:15 AM by Israel B. Ocbina

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   


Disclaimer - Privacy
© 2002-2014 DevASP.net