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.csusing 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 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.csusing 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.csThe 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.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: