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
 

Implement Windows Controls Dynamically using .net 2.0

Author: Zunnair

Instead of using the Windows Form Designer to add controls, developers can also add Controls to forms or other objects dynamically by using code.

Instead of using the Windows Form Designer to add controls, developers can also add Controls to forms or other objects dynamically  by using code. Adding controls in this manner allows controls to be created dynamically during runtime as opposed to During design time.

 

For example the Function Below Create the Button and setits Multiple Properties.

You can set Color, text, BackColor and all Properties which you can set at design time.

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

 

        Dim mybutton As New Button

        mybutton.Location = New Point(10, 90)

        mybutton.BackColor = Color.Black

        mybutton.ForeColor = Color.White

        mybutton.TextAlign = ContentAlignment.MiddleCenter

        mybutton.Text = "My Dynamic Button ."

        Me.Controls.Add(mybutton)

 

End Sub

 

This Function Create the TextBox Dynamically.

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

 

        Dim mytextbox As New TextBox

        mytextbox.Location = New Point(10, 50)

        mytextbox.BackColor = Color.Coral

        mytextbox.ForeColor = Color.Blue

        mytextbox.Text = "My Dynamic TextBox ."

        Me.Controls.Add(mytextbox)

 

End Sub

 

This function Create Label Dynamically.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       

 Dim lblMyLabel As New Label

 lblMyLabel.Location = New Point(10, 10)

 lblMyLabel.BackColor = Color.BurlyWood

 lblMyLabel.ForeColor = Color.Red

 lblMyLabel.TextAlign = ContentAlignment.MiddleCenter

 lblMyLabel.Text = "My Dynamic Label."

 Me.Controls.Add(lblMyLabel)

 

End Sub

Article Comments
hi sir,m intersted in doing a project in develping a media player,as this is m first project ,i lack in guidance so i request you to help m in completion of this project

Posted on 4/7/2010 7:26:50 AM by muttu

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Implementing Application with single GUI in Visual Basic.Net 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net