Windows Form Control – Part1 – Adding a control to a Form


Introduction:

In this article I explained how to Add acontrol to a form. The System.Windows.Forms namespace contains a numberof types that represent common GUI widgets. Let us see those things indetail.

Creating a Form:

Now first let us see a construction of simple form.

The example:Simple.cs

using System;
using System.WinForms;
public class MyForm : Form {
public MyForm() {
this.Text = "Welcome India!!!";
}
public static void Main(string[] args) {
MyForm f = new MyForm();
Application.Run(f);
}
}

The Output:

Adding Controls to Forms:

The Windows Forms framework summarize nativeWin32 APIs and exposes secure, managed classes for creating Win32client-side applications. The Windows Forms class library offers a lotof 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 isdeclare the required private member variables. After that configure thelook of each control in the Form's constructor or in theIntializeComponent () method using properties, methods and events.Finally add the controls to the Form's controls collection usingcontrol property.

Now let us see an example program Formexample:

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

The example: Formexample.cs

using System;
using System.ComponentModel;
using System.WinForms;
using System.Drawing;

public class Formexample :Form
{
private Button btncont = new Button();
private TextBox TBox = new TextBox();

Formexample ()
{
InitializeComponent();
}
private void InitializeComponent() {
EventHandler handler = 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);

this.Controls.Add(btncont);
this.Controls.Add(TBox);
}

private void btncontClick (object sender, EventArgs e) {
this.BackColor = Color.IndianRed;
TBox.Text = "You Click the Button[Arungg]";
TBox.BackColor=Color.Cyan;

}

public static void Main(string[] args) {
Application.Run(new Formexample());
}
}

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 applicationdevelopment (RAD) environment such as Visual Studio.NET the applicationdevelopment is very much simplified. In the Visual Studio Windows Formsdesigner, the controls appear in a toolbox and are dropped on a designsurface. This is accompanied by automatic code generation.

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

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="468" height="60"><param name="movie" value="/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" /><param name="quality" value="high" /> <embed src="http://www.csharphelp.com/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="468" height="60"></embed> </object>

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!