Reader Level:

Adding a control to a Form programmatically

On November 10, 2012 Articles | Visual Basic .NET
In this article I will show you how to add Windows controls to a Form programmatically. This is very useful to programmers who do not use Visual Studio or other IDE.
  • 0
  • 0
  • 5661


In this article I explain how to Add a control to a form programmatically. This is useful for developer who is not using VS.NET IDE. The System.Windows.Forms namespace contains a number of types that represent common GUI widgets. Let us see those things in detail.

Creating a Form:

Now first let us see a construction of simple form.

The example:Simple.vb

Imports System
Imports System.WinForms
Public Class MyForm
Inherits Form
Public Sub New()
Me.Text = "Welcome India!!!"
End Sub 'New
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
End Sub
Overloads Shared Sub Main(ByVal args() As String)
Dim fAs New MyForm()
End Sub 'Main
End Class 'MyForm

The Output:

Adding Controls to Forms:

The Windows Forms framework summarize native Win32 APIs and exposes secure, managed classes for creating Win32 client-side applications. The Windows Forms class library offers a lot of controls such as buttons, check boxes, drop-down lists, combo boxes, data grid, and other client-side functionality.

To add controls in the form the First step is declare the required private member variables. After that configure the look of each control in the Form's constructor or in the IntializeComponent () method using properties, methods and events. Finally add the controls to the Form's controls collection using control property.

Now let us see an example program Formexample:

In this example as I said previously, first I declare the private Button btncont,& TextBox TBox member variable. Then I constitute the Properties of the Button control,TextBox control in the InitializeComponent(). Then I build a delegate for the click event of a button. The argument to the constructor contains a reference to the method that performs the event handling logic.The btncont.Click + = handler registers the delegate with the Click event of the button.The Form class has a Controls property that is a collection of child controls. The Add method adds your control to that collection.

The example: Formexample.VB

Imports System
Imports System.ComponentModel
Imports System.WinForms
Imports System.Drawing
Public Class Formexample
Inherits Form
Private btncont As New Button()
Private TBox As New TextBox()
Sub New()
End Sub 'New
Private Sub
Dim handler As New EventHandler(btncontClick)
btncont.Text = "Click Me!!"
btncont.Anchor = AnchorStyles.TopLeft
btncont.Click += handler
TBox.Location =
New Point(85, 0)
TBox.Size =
New Size(150, 50)
TBox.TabIndex = 1
ClientSize =
New Size(250, 250)
End Sub 'InitializeComponent
Private Sub btncontClick(ByVal sender As Object,ByVal e As EventArgs)Me.BackColor = Color.IndianRed
TBox.Text = "You Click the Button[Arungg]"
TBox.BackColor = Color.Cyan
End Sub 'btncontClick
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
End Sub
Public Overloads Shared Sub Main(ByVal args() As String)
New Formexample)
End Sub 'Main
End Class 'Formexample

VBC /r:System.WinFOrms.dll /r:System.dll /r:Microsoft.Win32.Interop.dll /r:System.Drawing.dll Formexample.VB

The Output:

In another way by using a rapid application development (RAD) environment such as Visual Studio.NET the application development is very much simplified. In the Visual Studio Windows Forms designer, the controls appear in a toolbox and are dropped on a design surface. This is accompanied by automatic code generation.

The forms designer has a property window that displays properties and events. Every Windows Forms application is a class that derives from System.WinForms.Form. invokes the Run method of the System.WinForms.Application class in its Main method, with an instance of the form as the argument. The Application.Run method processes messages from the operating system to the application.


G Gnana Arun Ganesh

G.GNANA ARUN GANESH, an ECE graduate (1997-2001) seeking a profession in either Software or Hardware Company. During Campus interview selected as a Software Engineer at GREEN MICRO SYSTEMS Chennai, a ...

  • 187rank
  • 2M readers

Trending up