As you should know from your learning C#, in order to use a variable in your application, you must declare a variable for it and, when declaring a variable, you must give it a name. This rule also applies to Windows controls you will use in your application. Based on this, and as seen in the previous lesson, you can programmatically create a control by declaring a variable for and give that variable a name.
To create a control, the primary piece of information you must provide is its name. You do this when declaring a variable for the control. Here is an example: public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
this.Controls.Add(btnSubmit);
}
}
The Win32 library uses a concept called handle to represent each object of an application. A handle in Win32 is simply an unsigned integer used in place of a control. You have a choice of using it or not but it exists. The handle is created by the operating system when the application comes up. This means that you don't need to create it but you can access it to retrieve its value. To access the handle to a control, you can call its Handle property. In the .NET Framework, the Handle property is defined as an IntPtr value. To retrieve the handle of a control, you can access its Handle property.
Some controls are text-based, meaning they are meant to display or sometimes request text from the user. If a control displays text, then it has a property called Text. Here is an example: public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
this.Controls.Add(btnSubmit);
}
}
Many controls (not all), such as the Label, are configured to resize themselves as you provide a string to their Text property. If you type a long string, the control would be made large enough to show the whole text. Some other controls, such as the Button, will not resize themselves if their text is too long. The ability of a control to enlarge itself to accommodate its string is controlled by a Boolean property named AutoSize. For some controls, the default value of this property is False. For some other controls, this property is defaulted to True. Therefore, use this property to control whether a control on the form can be resized or not. To specify the auto-sizing option of a control, access its AutoSize property and assign it the desired Boolean value. Here is an example: public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.AutoSize = true;
Controls.Add(btnSubmit);
}
}
After adding a control to a window, the control is positioned in the body of the parent using a Cartesian coordinate system whose origin is located on the top-left corner of the parent window. The position of a control on a form (or a container) is referred to as its location. The location is defined by two values:
The location of a Windows control can be illustrated as follows:
The location of a control is specified using a structure named Point. To identify the concept of a point, the System.Drawing namespace provides the Point structure. The Point structure is defined in the System.Drawing.dll. One of the properties of the Point structure is X, which represents the horizontal distance of the point from the top-left corner of the object that owns the point. Another property, Y, represents the vertical measurement of the point with regards to the top-left corner of the object that owns the point. A Point object can be represented on the Windows coordinate system as follows: ![]()
You can specify the location of a control. To support this, each control has a property named Location. To assist you with specifying the location of a control, the Point class provides a constructor as follows: public Point(int x, int y) This constructor takes a left and a top arguments. Here is an example of setting the location of a control: using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Location = new Point(100, 40);
this.Controls.Add(btnSubmit);
}
public Exercise()
{
InitializeComponent();
}
}
public class Program
{
static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
You can also use an existing Point object and assign it to the Location property of the control. Here is an example: public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
Point pt = new Point();
pt.X = 100;
pt.Y = 40;
btnSubmit.Location = pt;
this.Controls.Add(btnSubmit);
}
}
You can also retrieve the location of a control and store it in a Point object. To do this, you can simply assign the Location property of a control to a Point object. public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Location = new Point(100, 40);
Point pt = btnSubmit.Location;
Controls.Add(btnSubmit);
}
}
When studying control alignment, we saw that you could use the location of one control as a reference to position another control. You can also do this programmatically by retrieving the location of one control and changing either its X or its Y values to position the new control. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
private Button btnSubmit;
private TextBox txtEmployeeName;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Location = new Point(100, 40);
txtEmployeeName = new TextBox();
Point pt = btnSubmit.Location;
txtEmployeeName.Location = new Point(pt.X, pt.Y + 32);
Controls.Add(btnSubmit);
Controls.Add(txtEmployeeName);
}
public Exercise()
{
InitializeComponent();
}
}
public class Program
{
static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
The Point structure provides other constructors you can use for the location of a control.
The distance from the left border to the right border of a control is referred to as its width property. This can be illustrated as follows: ![]() The combination of the width and the height of a control is referred to as its size. If you add a control to a form, it assumes a default size.
To support the size of an object, the System.Drawing namespace defines the Size structure that represents a location and dimensions. A Size value must have a starting point (X, Y) just as the Point object. The width is the distance from the left to the right borders of a Size object. The height represents the distance from the top to the bottom borders of a Size value: ![]() To assist you with sizes, the Size structure provides the following constructor: public Size(int width, int height); Using this constructor, to specify the size of a control, assign a Size value to its Size property. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Location = new Point(100, 40);
btnSubmit.Size = new Size(80, 32);
Controls.Add(btnSubmit);
}
public Exercise()
{
InitializeComponent();
}
}
public class Program
{
static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
You can also define a Size object using a Point value. To support this, the Size structure is equipped with the following constructor: public Size(Point pt); After declaring a variable with this constructor, you can access its Width and Height properties to complete the definition of the Size object. If you already have the size of an object, you may only want to specify the dimensions of the variable. Besides the Size, the System.Drawing namespace also provides the SizeF structure. It uses the same properties as Size except that its members float types. To retrieve the dimensions of a control, you can get its Size property and assign it to a Size object.
The combination of the location and size of an object is represented as a rectangle: a geometric figure with four sides. To support this figure, the System.Drawing namespace provides the Rectangle and the RectangleF structures. A rectangle can be represented as follows: ![]() To completely represent it, a rectangle is defined by its location and its dimensions. The location is defined by a point on the top-left corner of the rectangle. The distance from the left border of the object that owns the rectangle to the left border of the rectangle is represented by a property called Left. The distance from the top border of the object that owns the rectangle to the top border of the rectangle is represented by a property called Top. The distance from the left to the right borders of the rectangle is represented by a property called Width. The distance from the left to the right borders of the rectangle is represented by a property called Height. The distance from the left border of the object that owns the rectangle to the right border of the rectangle is represented by a property called Right. The distance from the top border of the object that owns the rectangle to the bottom border of the rectangle is represented by a property called Bottom. Based on this, a rectangle can be illustrated as follows: ![]()
To create a rectangle, you can use the following constructor to declare a Rectangle variable: public Rectangle(Point location, Size size); This constructor requires that you define a Point and a Size in order to use it. If instead you know the integer values of the location and dimensions, you can use the following constructor to declare a Rectangle object: public Rectangle(int x, int y, int width, int height); Besides the Rectangle structure, the System.Drawing namespace provides the RectangleF structure that uses the same definition as Rectangle, except that it is defined with float values instead of integers. The be able to recognize the location and the size of a control, the Control class is equipped with a property named Bounds. This property is of type Rectangle represented by the property. Therefore, at any time, to get the location and the size of a control, you can call its Bounds property, which produces a Rectangle value.
When a control is meant to display text, it has a property named TextAlign. The TextAlign property is of type ContentAlignment, which is an enumeration. The members and result of this enumeration are: TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter, MiddleRight, BottomLeft, BottomCenter, and BottomRight. To programmatically specify the text alignment of a control, access its TextAlign property and assign it the desired member of the ContentAlignment enumeration. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Location = new Point(20, 20);
btnSubmit.Size = new Size(100, 60);
btnSubmit.TextAlign = ContentAlignment.BottomRight;
Controls.Add(btnSubmit);
}
public Exercise()
{
InitializeComponent();
}
}
public class Program
{
static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
The ability to manage a control or a group of controls' location and size when the user resizes it is done using the Anchor property. The Anchor property is created from the AnchorStyles enumeration. By default, when you add a control to a form, its position is relative to the top left corner of its container. The Anchor property can be used to "glue" one border of a control to its parent using one of the following values:
In the same way, you can combine AnchorStyles values to "glue" one or more corners of a control to its parent when the parent is resized. To programmatically combine two or more AnchorStyles values, you use the OR bit operator: "|"
When a control is added to a host, in some cases, you will want the control to be attached to a border or to a corner of its parent. This can be done using the Dock property. This property is managed through the DockStyle enumeration. To use this property, click the control and, in the Properties window, click the arrow of the Dock field. You can then select one of the following values:
To specify the docking option of a control, access its Dock property and assign the desired member of the DockStyle enumeration. Here is an example: public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.Dock = DockStyle.Right;
Controls.Add(btnSubmit);
}
}
Controls used on Microsoft Windows are painted using a color. The names of colors are defined in an enumeration called Color. To specify the background color of a control, access its BackColor property and assign it the desired member of the Color enumeration. Here is an example: public class Exercise : Form
{
private Button btnSubmit;
private void InitializeComponent()
{
btnSubmit = new Button();
btnSubmit.Text = "Submit";
btnSubmit.BackColor = Color.Aquamarine;
Controls.Add(btnSubmit);
}
}
Instead of a color, you may want to fill the control with a picture. To do this, you can access the control's BackgroundImage property and assign an Image object to it. Some controls display a border when they are drawn and some others don't. This characteristic is controlled by the BorderStyle property, which is based on BorderStyle enumerator. Its members are: Fixed3D, FixedSingle, and None. To specify the border style of a control, access its BorderStyle property and assign it the desired BorderStyle member. Here is an example: using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
private Panel pnlViewer;
private void InitializeComponent()
{
pnlViewer = new Panel();
pnlViewer.BorderStyle = BorderStyle.Fixed3D;
pnlViewer.BackColor = Color.LightBlue;
Controls.Add(pnlViewer);
}
public Exercise()
{
InitializeComponent();
}
}
public class Program
{
static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
A user can navigate through controls using the Tab key. When that key has been pressed, the focus moves from one control to the next. By their designs, not all controls can receive focus and not all controls can participate in tab navigation. Even controls that can receive focus must be primarily included in the tab sequence.
A control is referred to as visible if it can be visually located on the screen. A user can use a control only if he or she can see it. The visibility of an object is controlled by a property named Visible, which is Boolean. If you create a control, by default, it is made visible once you add it to its parent's Controls property. To check whether a control is visible at one time, apply a conditional statement (if or while) to its Visible property and check whether its value is true or false.
The availability of an object is controlled by a Boolean property named Enabled. To find out whether a control is enabled or not, check its Enabled property. Drag n' drop is the ability to drag an object or text from one location or object to another. This is a significant operation in computer use. Although Microsoft Windows fully supports drag n' drop operations, because the operating system cannot predict how the operations should be carried, you must write code. Various controls support drag n' drop operations. While most controls support them, it is not always realistic to implement the operations on every control. This means that you will have to decide when, how, and what controls of your application will need to allow the user do drag what and drop it where. A control that allows drag n' drop operations is equipped with a Boolean property named AllowDrop. When you set this property to true, it means the user can either drag something from it or drop something on it. After setting this property, you must write code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||