FREE BOOK

Chapter 3: How to code and test a Windows Forms application using C# 2008

Posted by Murach Free Book | Windows Forms December 10, 2008
In this chapter, you'll learn how to code and test a Windows Forms application. When you're done, you'll be able to develop simple applications of your own.

Introduction to object-oriented programming

Whether you know it or not, you are using object-oriented programming as you design a Windows form with Visual Studio's Form Designer. That's because each control on a form is an object, and the form itself is an object. These objects are derived from classes that are part of the .NET Class Library.

When you start a new project from the Windows Application template, you are actually creating a new class that inherits the characteristics of the Form class that's part of the .NET Class Library. Later, when you run the form, you are actually creating an instance of your form class, and this instance is known as an object.

Similarly, when you add a control to a form, you are actually adding a control object to the form. Each control is an instance of a specific class. For example, a text box control is an object that is an instance of the TextBox class. Similarly, a label control is an object that is an instance of the Label class. This process of creating an object from a class can be called instantiation.

As you progress through this book, you will learn much more about classes and objects because C# is an object-oriented language. In chapter 12, for example, you'll learn how to use the C# language to create your own classes. At that point, you'll start to understand what's actually happening as you work with classes and objects. For now, though, you just need to get comfortable with the terms and accept the fact that a lot is going on behind the scenes as you design a form and its controls.

Figure 3-1 summarizes what I've just said about classes and objects. It also introduces you to the properties, methods, and events that are defined by classes and used by objects. As you've already seen, the properties of an object define the object's characteristics and data. For instance, the Name property gives a name to a control, and the Text property determines the text that is displayed within the control. In contrast, the methods of an object determine the operations that can be performed by the object.

An object's events are signals sent by the object to your application that something has happened that can be responded to. For example, a Button control object generates an event called Click if the user clicks the button. Then, your application can respond by running a C# method to handle the Click event.

By the way, the properties, methods, and events of an object or class are called the members of the object or class. You'll learn more about properties, methods, and events in the next three figures.

A form object and its ten control objects

 


 Figure 3-1 Introduction to object-oriented programming

Class and object concepts

  • An object is a self-contained unit that combines code and data. Two examples of objects you have already worked with are forms and controls.

  • A class is the code that defines the characteristics of an object. You can think of a class as a template for an object.

  • An object is an instance of a class, and the process of creating an object from a class is called instantiation.

  • More than one object instance can be created from a single class. For example, a form can have several button objects, all instantiated from the same Button class. Each is a separate object, but all share the characteristics of the Button class.

Property, method, and event concepts

  • Properties define the characteristics of an object and the data associated with an object.

  • Methods are the operations that an object can perform.

  • Events are signals sent by an object to the application telling it that something has happened that can be responded to.

  • Properties, methods, and events can be referred to as members of an object.

  • If you instantiate two or more instances of the same class, all of the objects have the same properties, methods, and events. However, the values assigned to the properties  can vary from one instance to another.

Objects and forms

  • When you use the Form Designer, Visual Studio automatically generates C# code that creates a new class based on the Form class. Then, when you run the project, a form object is instantiated from the new class.

  • When you add a control to a form, Visual Studio automatically generates C# code in the class for the form that instantiates a control object from the appropriate class and sets the  control's default properties. When you move and size a control, Visual Studio automatically  sets the properties that specify the location and size of the control.

Total Pages : 10 12345

comments