Microsoft KB Archive/319266

= How to programmatically add controls to Windows forms at run time by using Visual C# =

Article ID: 319266

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q319266



For a Microsoft Visual Basic .NET version of this article, see 308433.

IN THIS TASK
SUMMARY
 * Requirements
 * Create a Windows Forms Application
 * Customize Form and Control Properties
 * Add Controls to the Form
 * Verify that it Works
 * Notes

REFERENCES



SUMMARY
This step-by-step article shows you how to programmatically add and configure a few commonly used controls on a Windows form. Event handling has been omitted from the sample code.

The Microsoft .NET Framework Software Development Kit (SDK) provides many visual controls that you can use to build a Windows Forms application. You can add and configure controls at design time in Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, or you can add and configure controls programmatically at run time.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Visual C# syntax
 * Visual Studio .NET environment or Visual Studio 2005 environment
 * Purpose of common Visual C# controls

back to the top

Create a Windows Forms Application
 Start Visual Studio .NET or Visual Studio 2005, and create a new Visual C# Windows Application project named WinControls. Form1 is added to the project by default. Double-click Form1 to create and view the Form1_Load event procedure.  Add private instance variables to the Form1 class to work with common Windows controls. The Form1 class starts as follows: public class Form1 : System.Windows.Forms.Form {   //Controls. private TextBox txtBox = new TextBox; private Button btnAdd = new Button; private ListBox lstBox = new ListBox; private CheckBox chkBox = new CheckBox; private Label lblCount = new Label;

//Other code. } Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The Designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms173077.aspx



back to the top

Customize Form and Control Properties
  Locate the Form1_Load event procedure, and add the following code to the procedure to customize the appearance of the Form control: //Set up the form. this.MaximizeBox = false; this.MinimizeBox = false; this.BackColor = Color.White; this.ForeColor = Color.Black; this.Size = new System.Drawing.Size(155, 265); this.Text = &quot;Run-time Controls&quot;; this.FormBorderStyle = FormBorderStyle.FixedDialog; this.StartPosition = FormStartPosition.CenterScreen;   Add the following code to the Form1_Load event procedure to customize the appearance of the Button control: //Format controls. Note: Controls inherit color from parent form. this.btnAdd.BackColor = Color.Gray; this.btnAdd.Text = &quot;Add&quot;; this.btnAdd.Location = new System.Drawing.Point(90, 25); this.btnAdd.Size = new System.Drawing.Size(50, 25);   Add the following code to customize the appearance of the TextBox control in Form1_Load: this.txtBox.Text = &quot;Text&quot;; this.txtBox.Location = new System.Drawing.Point(10, 25); this.txtBox.Size = new System.Drawing.Size(70, 20); </li>  Add the following code to customize the appearance of the ListBox control in Form1_Load: this.lstBox.Items.Add(&quot;One&quot;); this.lstBox.Items.Add(&quot;Two&quot;); this.lstBox.Items.Add(&quot;Three&quot;); this.lstBox.Items.Add(&quot;Four&quot;); this.lstBox.Sorted = true; this.lstBox.Location = new System.Drawing.Point(10, 55); this.lstBox.Size = new System.Drawing.Size(130, 95); </li>  Add the following code to customize the appearance of the CheckBox control in Form1_Load: this.chkBox.Text = &quot;Disable&quot;; this.chkBox.Location = new System.Drawing.Point(15, 190); this.chkBox.Size = new System.Drawing.Size(110, 30); </li>  Add the following code to customize the appearance of the Label control in Form1_Load: this.lblCount.Text = lstBox.Items.Count.ToString + &quot; items&quot;; this.lblCount.Location = new System.Drawing.Point(55, 160); this.lblCount.Size = new System.Drawing.Size(65, 15); </li></ol>

back to the top

Add Controls to the Form
  Add the following code to add each object to the Controls array of the form at the end of Form1_Load: //Add controls to the form. this.Controls.Add(btnAdd); this.Controls.Add(txtBox); this.Controls.Add(lstBox); this.Controls.Add(chkBox); this.Controls.Add(lblCount); </li> Save the project.</li></ol>

back to the top

Verify that it Works
To verify that the sample works, click Start on the Debug menu. Note that although the form and the controls appear, they currently do nothing because you have not written any event handlers.

back to the top