Home

Introduction to Windows Controls

 

Control Creation

 

Introduction

The objects used in a Windows application are defined in various assemblies. To use one of these controls in your application, you must first know the name of its class. With this information, you can declare a variable of its class. For example, a command button is an object of type Button that is based on the Button class. The Button class is defined in the System.Windows.Forms namespace of the System.Windows.Forms.dll assembly. Based on this, to create a button, you can declare a Button variable as follows:

using System;
using System.Windows.Forms;

class Program : Form
{
	static void Main()
	{
		Button btnSubmit;
	}
}

After, or when, declaring the variable, you can use the new operator to allocate memory for it:

using System;
using System.Windows.Forms;

class Program : Form
{
	static void Main()
	{
		Button btnSubmit;

		btnSubmit = new Button();
	}
}

This is also referred to as dynamically creating a control. After declaring the variable and allocating memory for it, the control is available but doesn't have a host, which makes it invisible. You can then call the Controls property of its parent to add the new control to its collection. The parent would then be responsible for hosting or carrying the new control. Here is an example:

using System;
using System.Windows.Forms;

class Program : Form
{
	static void Main()
	{
		Program frm;
		Button btnSubmit;

		frm= new Exercise();
		btnSubmit = new Button();

		frm.Controls.Add(btnSubmit);
	}
}

If you declare and completely create a control in a particular method, the control would be available only in that method and cannot be accessed outside. To make such a control available to other controls or methods of the same class, you should declare it outside of a method. You can do this by declaring the variable in the form's class. Here is an example:

using System;
using System.Windows.Forms;

class AppMainForm : Form
{
	private Button btnSubmit;

	static void Main()
	{
	}
}

After declaring the control, you can allocate memory for it in the form's constructor or prior to using it. Here is an example:

using System;
using System.Windows.Forms;

class AppMainForm : Form
{
	private Button btnSubmit;
	
	public AppMainForm()
	{
		btnSubmit = new Button();
		this.Controls.Add(btnSubmit);
	}

	static void Main()
	{
	}
}

Because there can be many controls used in a program, instead of using the constructor to initialize them, you can create a method called InitializeComponent to initialize the various objects used in your application. Then simply call that method from the constructor of your form.

Practical LearningPractical Learning: Dynamically Creating a Control

  1. Start Notepad
  2. In the empty file, type the following: 
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : System.Windows.Forms.Form
      {
        // Declare a class used to create a control
        private TextBox Editor;
        
        public Exercise()
        {
    	// Initialize the control
    	Editor = new TextBox();
    	// and add it to the collection of controls of the form
    	this.Controls.Add(Editor);
    
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
        }
    
        static int Main()
        {
    	// Instantiate an Exercise object
    	Exercise frmMain = new Exercise();
    	// Call the Run() static method of the Application
    	// and pass it the instance of the class to display
    	Application.Run(frmMain);
    
    	return 0;
        }
      }
    }
  3. To save the application, on the main menu, click File -> Save
  4. Locate the CSharpWindows folder you created in the previous lesson and display it in the Save In combo box
  5. Click the Create New Folder button again
  6. Type Fundamentals2 and press Enter twice to display it in the Save In combo box
  7. Set the File Name to Exercise.cs and change the Files of Types to All Files
  8. Click Save
  9. Open the Command Prompt and switch to the Fundamentals2 folder that contains the above Exercise.cs file
  10. To compile the application, type
     
    csc /r:System.Windows.Forms.dll /target:winexe Exercise.cs
  11. Press Enter
  12. In Windows Explorer or My Computer, locate the folder that contains the current exercise
  13. To execute the application, double-click Exercise
     
    A form with a control
  14. Close it by clicking its system Close button Close

Control Addition by Class Derivation

If you are using a .NET Framework control, you must know the name of the class on which the control is based (and each control is based on a particular class). If you have examined the types of classes available but none of them implements the behavior you need, you can first locate one that is close to the behavior you need, then use it as a base to derive a new class. To derive a class from an existing control, you can use your knowledge of inheritance to derive a new class. Here is an example:

public class Numeric : System.Windows.Forms.TextBox
{
}

If you want to perform some early initialization to customize your new control, you can declare a constructor. Here is an example:

public class Numeric : System.Windows.Forms.TextBox
{
	public Numeric() : base()
	{
	}
}

Besides the constructor, in your class, you can add the properties, methods, and/or events as you see fit. You can also use it to globally set a value for a property of the base class. Once the control is ready, you can dynamically use it like any other control. Here is an example:

Source File: Numeric.cs
public class Numeric : System.Windows.Forms.TextBox
{
	public Numeric() : base()
	{
	}
}
Source File: Form1.cs
using System;
using System.Windows.Forms;

namespace WindowsApplication
{
	public class Form1 : System.Windows.Forms.Form
	{
		private Numeric txtBox;

		public Form1()
		{
			InitializeComponent();

			txtBox = new Numeric();
			this.Controls.Add(txtBox);
		}

		. . . No Change
	}
}

 

 

Previous Copyright © 2004-2012, FunctionX Next