Methods of Managing Controls

 

Control Creation 

In the previous lesson, we saw that the easiest way to create a control is by selecting it from the Toolbox and adding it to the form. If for any reason you cannot visually add a control, you can programmatically create it.

The classes used to manage controls of the .Net ensemble are created in the System namespace. Inside of the System namespace, there is a Windows namespace. Inside of the Windows namespace, there is the Forms namespace. All Windows control available in the .Net programming studio are created in the System.Windows.Forms namespace.

Every .Net control is based on a class. Every one of these control classes has at least a default constructor. You can use this constructor to dynamically create a control. The general syntax used is:

ClassName VariableName;

Here is an example:

System.Windows.Forms.Button btnSubmit;

You must use the new operator and call its default constructor to initialize the control. After calling its constructor, you can initialize any of its properties as necessary. Here is an example:

public class Form1 : System.Windows.Forms.Form
	{
		System.Windows.Forms.Button btnSubmit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			btnSubmit = new System.Windows.Forms.Button();
			btnSubmit.Location = new System.Drawing.Point(88, 32);
			btnSubmit.TabIndex = 0;
			btnSubmit.Text = "Submit";
			Controls.Add(btnSubmit);
		
 ...

}

 

 

Focus

The focus is a visual aspect that indicates that a control is ready to receive input from the user. Various controls have different ways of expressing that they have received focus.

Buttons controls indicate that they have focus by drawing a dotted rectangle around their caption. A text-based control indicates that it has focus by displaying a blinking cursor. A list-based control indicates that it has focus when one of its items has a surrounding dotted rectangle.

To give focus to a control, the user can click it or press a key. To programmatically give focus to a control, call the Focus() method. Its syntax is:

bool Focus();


 


Previous Copyright 2003 FunctionX, Inc. Next