How to code and Test a Windows Forms Application

Got this from webpage

How to Code and Test a Windows Forms Application

 
(Page 1 of 4 )

Yesterday we finished an article series that showed you how to design a user interface for a Windows Forms application. Today, we'll kick off a five-part article series that shows you how to bring that application to life. This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456).

In the last chapter, you learned how to design a form for a Windows Forms application. In this chapter, you'll learn how to code and test a Windows Forms application. Here, the emphasis will be on the Visual Studio skills that you need for entering, editing, and testing the Visual Basic code for your applications. You'll learn how to write that code in the rest of this book.

An introduction to coding

Before you learn the mechanics of adding code to a form, it's important to understand some of the concepts behind object-oriented programming.

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 Visual Basic is an object-oriented language. In chapter 11, for example, you'll learn how to use the Visual Basic 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 Visual Basic procedure 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

Class and object concepts

  1. 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.
  2. class is the code that defines the characteristics of an object. You can think of a class as a template for an object. 
     
  3. An object is an instance of a class, and the process of creating an object from a class is called instantiation
     
  4. 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

  1. Properties define the characteristics of an object and the data associated with an object.
  2. Methods are the operations that an object can perform. 
     
  3. Events are signals sent by an object to the application telling it that something has happened that can be responded to. 
     
  4. Properties, methods, and events can be referred to as members of an object. 
     
  5. 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 Visual Basic 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 Visual Basic 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.  

No comments: