Introduction to Controls Containers |
|
Hosting a Windows Control
Introduction to Controls
A Windows control, or an application's control, or simply a control, is an object in a computer control. That object allows the user of an application to interact with the computer, to tell the computer what the user wants, or what the user wishes to see happening. Computers, or operating systems, support various types of controls, and the controls perform various roles. This means that there are various types of controls with different goals. We have already seen that every control has a class that holds the characteristics (properties), the actions that the control can perform (its methods), and the occurrences (events) related to it.
To make a control available and useable to your application, you can select that control from the Toolbox and add it to your project. As an alternative, you can create a control programmatically. Almost all the controls you will need are created in the System.Windows.Forms namespace that belongs to the System.Windows.Forms.dll assembly. Therefore, you must add a reference to that assembly in your project and you can/should include that namespace to your code. If you create a Windows Forms App (.NET Framework) or a Windows Forms App (.NET Core) project, the reference is automatically created and the namespace appropriately added. After performing any of those actions, you can declare a variable using the class of the control. Here are two examples:
using System;
using System.Windows.Forms;
namespace ControlsFundamentals
{
public partial class Form1 : Form
{
public Form1() => InitializeComponent();
private void btnControls_Click(object sender, EventArgs e)
{
RadioButton rdoMedium = new RadioButton();
GroupBox gbxPizzaSize = new GroupBox();
}
}
}
Of course, you can programmatically access other properties of a control. Here are examples:
using System; using System.Drawing; using System.Windows.Forms; namespace ControlsFundamentals { public partial class Form1 : Form { public Form1() => InitializeComponent(); private void btnControls_Click(object sender, EventArgs e) { RadioButton rdoMedium = new RadioButton(); rdoMedium.Location = new Point(24, 58); rdoMedium.Width = 61; rdoMedium.Text = "Medium"; GroupBox gbxPizzaSize = new GroupBox(); gbxPizzaSize.Location = new Point(12, 12); gbxPizzaSize.Size = new Size(109, 126); gbxPizzaSize.Text = "Pizza Size"; } } }
When it comes to adding objects to your application, there are two broad categories of controls.
A Control's Container
When you pick up a control from the Toolbox, you must drop that control somewhere so your application can use it. A control container is a special control that can receive or host other controls. Microsoft Windows (and other operating systems) support various types of controls containers.
A Control's Container is a Collection
To make it possible for an object to host other objects, the class of every container is equipped with a read-only property named Controls. This property is based on a class named ControlCollection which, as its name implies, is a collection class:
[System.ComponentModel.Browsable(false)] public System.Windows.Forms.Control.ControlCollection Controls { get; }
The ControlCollection class is equipped with all the basic operations of colleection class, including the ability to add, delete, or count the items in the list.
To support the ability to add a control to a container, the ControlCollection class is equipped with a method named Add. Its syntax is:
public virtual void Add (System.Windows.Forms.Control value);
This method takes as argument an object that derives from the Control class, which is the case for all Windows controls. Therefore, to programmatically add a control to a container, access the Control property of the container and call the Add() method on it. Pass the variable name of the control you want. Here is an example:
using System; using System.Drawing; using System.Windows.Forms; namespace ControlsFundamentals { public partial class Form1 : Form { public Form1() => InitializeComponent(); private void btnControls_Click(object sender, EventArgs e) { RadioButton rdoMedium = new RadioButton(); rdoMedium.Location = new Point(24, 58); rdoMedium.Width = 61; rdoMedium.Text = "Medium"; GroupBox gbxPizzaSize = new GroupBox(); gbxPizzaSize.Location = new Point(12, 12); gbxPizzaSize.Size = new Size(109, 126); gbxPizzaSize.Text = "Pizza Size"; gbxPizzaSize.Controls.Add(rdoMedium); } } }
A Form or Dialog Box as a Container
As seen so far, a form is the most fundamental container of an application. All the controls of an application are placed directly or indirectly on a form. Therefore, to programmatically position a control on a form, access the Controls property of the form, call the Add() method on that property, and pass the variable of the control to it. Here is an example:
using System.Drawing;
using System.Windows.Forms;
public class Exercise
{
public static void Main()
{
Form frmExercise = new Form();
RadioButton rdoMedium = new RadioButton();
rdoMedium.Location = new Point(24, 58);
rdoMedium.AutoSize = true;
rdoMedium.Text = "Medium";
GroupBox gbxPizzaSize = new GroupBox();
gbxPizzaSize.Location = new Point(12, 12);
gbxPizzaSize.Size = new Size(109, 126);
gbxPizzaSize.Text = "Pizza Size";
gbxPizzaSize.Controls.Add(rdoMedium);
frmExercise.Controls.Add(gbxPizzaSize);
frmExercise.ShowDialog();
}
}
|
||
Previous | Copyright © 2008-2021, FunctionX | Next |
|