Adding a Control to a form Programmatically

In this article I explain how to Add 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.cs

  1. using System;  
  2. using System.WinForms;  
  3. public class MyForm : Form  
  4. {  
  5.     public MyForm()  
  6.     {  
  7.         this.Text = "Welcome India!!!";  
  8.     }  
  9.     public static void Main(string[] args)  
  10.     {  
  11.         MyForm f = new MyForm();  
  12.         Application.Run(f);  
  13.     }  
  14. }
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.

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.cs
  1. using System;  
  2. using System.ComponentModel;  
  3. using System.WinForms;  
  4. using System.Drawing;  
  5. public class Formexample : Form  
  6. {  
  7.     private Button btncont = new Button();  
  8.     private TextBox TBox = new TextBox();  
  9.     Formexample()  
  10.     {  
  11.         InitializeComponent();  
  12.     }  
  13.     private void InitializeComponent()  
  14.     {  
  15.         EventHandler handler = new EventHandler(btncontClick);  
  16.         btncont.Text = "Click Me!!";  
  17.         btncont.Anchor = AnchorStyles.TopLeft;  
  18.         btncont.Click += handler;  
  19.         TBox.Location = new Point(85, 0);  
  20.         TBox.Size = new Size(150, 50);  
  21.         TBox.TabIndex = 1;  
  22.         ClientSize = new Size(250, 250);  
  23.         this.Controls.Add(btncont);  
  24.         this.Controls.Add(TBox);  
  25.     }  
  26.     private void btncontClick(object sender, EventArgs e)  
  27.     {  
  28.         this.BackColor = Color.IndianRed;  
  29.         TBox.Text = "You Click the Button[Arungg]";  
  30.         TBox.BackColor = Color.Cyan;  
  31.     }  
  32.     public static void Main(string[] args)  
  33.     {  
  34.         Application.Run(new Formexample());  
  35.     }  
  36. }
csc /r:System.WinFOrms.dll /r:System.dll /r:Microsoft.Win32.Interop.dll /r:System.Drawing.dll Formexample.cs

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.