Working with Controls using C# in windows programming

In this article I will explain you about Working with Controls using C# in windows programming.

This article has been excerpted from book "The Complete Visual C# Programmer's Guide from the Authors of C# Corner".

Consistency and adaptability are two often-undeclared but implicit software design goals. In an object-oriented world, a frequently employed mechanism to achieve these objectives is inheritance.

In the System.Windows.Forms namespace, all controls derive from the Control class. The raw Control class is not normally instantiated but rather forms the basis for further refining the user interface hierarchy.

The Control class implements the basic functionality of controls and, where appropriate, provides for members to be overridden. This approach promotes not only reusability but also standardization. This can be seen in two Control class properties, Name and Text. The Name property, the equivalent to a control ID in Win32, applies to all controls regardless of type and therefore is not declared "virtual," whereas a Text property implementation differs depending on the type and can be overridden. For example, Form.Text refers to the caption in the title bar, while TextBox.Text returns the user's keyboard input.

In addition to many controls directly deriving from the Control class, a number of classes act as intermediaries to collect controls into what loosely could be termed a behavioral unit. Figure 9.16 shows a number of these intermediate classes.

Fig9.16.gif

Figure 9.16: Intermediate Classes

The ButtonBase class, for instance, is the root of the Button, CheckBox, and RadioButton classes, which exhibit a similar behavior. The ButtonBase class handles common chores such as raising mouse and focus events. Other classes that use this method of control intercession will be pointed out as they arise.

Another gauge of consistency in the Windows.Forms namespace is its treatment of collections. While objects range from the general to the very specific, the means of accessing an object in a collection remains consistent throughout the namespace. Each collection in the namespace has an indexer, as well as identical methods to add and remove objects. The most commonly referenced collection is the nested Control.ControlCollection, accessible through the read-only property called Controls. This is a container for Control objects.

Some of the properties of the Control classes are defined in Table 9.2.

table-9.2.gif

Table 9.2: Control Class Properties

Some Control class methods are itemized in Table 9.3.

table-9.3.gif

Table 9.3: Control Class Methods

The Control class implements basic mouse and keyboard events, some of which are defined in Table 9.4.

table-9.4.gif

Table 9.4: Control Class Events

Besides the methods defined in Table 9.4, there are overridable methods for raising events programmatically, such as OnClick, OnEnter, and OnKeyUp. When overriding any of these eventtriggering methods, the base class's method must be called, so that any registered delegate receives the event.

Control Classes

Table 9.5 lists some of the common control classes.

table-9.5.gif

Table 9.5: Windows Forms Common Control Classes

As has been seen earlier, you can create these controls programmatically as well as from the VS.NET Form Designer. The Form Designer allows you to drag and drop controls from the toolbox onto a form. Here we will show you how to create a control manually and set its properties.

Creating a Control

Use a constructor to create an instance of a control. Most controls are constructed with a default constructor, that is, a constructor with no parameters. Here is an example of creating a Button control:


        Button btn1 = new System.Windows.Forms.Button();


Setting Properties

After creating the object, set the control's properties. The code in Listing 9.5 sets the button properties.

Listing 9.5: Setting the properties of a Button Control


        btn1.ImageAlign = System.Drawing.ContentAlignment.TopLeft;
        btn1.Dock = System.Windows.Forms.DockStyle.Left;
        btn1.ForeColor = System.Drawing.Color.Red;
        btn1.BackColor = System.Drawing.SystemColors.Desktop;
        btn1.DialogResult = System.Windows.Forms.DialogResult.OK;
        btn1.AllowDrop = true;
        btn1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        btn1.Size = new System.Drawing.Size (336, 568);
        btn1.TabIndex = 0;
        btn1.Font = new System.Drawing.Font ("Verdana", 10, System.Drawing.FontStyle.Bold);
        btn1.Text = "Click Me";


Once you have set the properties, you can write an event handler for the control.

Conclusion


Hope this article would have helped you in understanding Working with Controls using C# in windows programming. See other articles on the website on .NET and C#.

visual C-sharp.jpg
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.