Adding a control to a Form programmatically

Introduction:

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()
Main(System.Environment.GetCommandLineArgs())
End Sub
Public
Overloads Shared Sub Main(ByVal args() As String)
Dim f As New MyForm()
Application.Run(f)
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()
InitializeComponent()
End Sub 'New
Private Sub
InitializeComponent()
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)
Me.Controls.Add(btncont)
Me.Controls.Add(TBox)
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()
Main(System.Environment.GetCommandLineArgs())
End Sub
Public Overloads Shared Sub Main(ByVal args() As String)
Application.Run(
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.

 


Similar Articles