Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

SUMMARY 183

number in front of the list of arguments, as shown in Figure 4.5. This number is the order of the overloaded form of the function, and it’s followed by the arguments of the specific form of the function. The figure shows all the forms of the CountFiles() function.

Figure 4.5

The overloaded forms of the CountFiles() function

You will have to overload many of the functions you’ll be writing once you start developing real applications, because you’ll want your functions to work on a variety of data types. This is not the only reason to overload functions. You may also need to write functions that behave differently based on the number and types of their arguments.

Note Notice that you can’t overload a function by changing its return type. That’s why the Min() function returns a double value, which is the most accurate value. If you don’t need more than a couple of decimal digits (or no fractional part at all), you can round the return value in your code accordingly. However, you can’t have two Min() functions that accept the exact same arguments and return different data types. Overloaded forms of a function are differentiated by the number and/or the type of their arguments, but not by the return value.

Summary

This chapter concludes the presentation of the core of the language. In the last two chapters, you’ve learned how to declare and use variables, and how to break your applications into smaller, manageable units of code. These units of code are the subroutines and functions. Subroutines perform actions and don’t return any values. Functions, on the other hand, perform calculations and return values. Most of the language’s built-in functionality is in the form of functions. The methods of the various controls look and feel like functions, because they’re implemented as functions. Functions are indeed a major aspect of the language.

Subroutines aren’t as common. Many programmers actually prefer to write only functions and use the return value to indicate the success or failure of the procedure, even if the procedure need not return any value. Event handlers are implemented as subroutines, because they don’t return any values. Event handlers aren’t called from within your code; they are simply activated by the Common Language Runtime.

Subroutines and functions communicate with the rest of the application through arguments. There are many ways to pass arguments to a procedure, and you’ve seen them all. You have also seen how to write overloaded functions, which are new to VB.NET; and as you will see in the rest of this book, they’re quite common.

In the following chapters, we’ll explore the Windows controls in depth, and you will write your first “real” Windows applications.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 5

Working with Forms

In Visual Basic, the form is the container for all the controls that make up the user interface. When a Visual Basic application is executing, each window it displays on the desktop is a form. In previous chapters, we concentrated on placing the elements of the user interface on forms, setting their properties, and adding code behind selected events. Now, we’ll look at forms themselves and at a few related topics, such as menus (forms are the only objects that can have menus attached), how to design forms that can be automatically resized, and how to access the controls of one form from within another form’s code. The form is the top-level object in a Visual Basic application, and every application starts with the form.

Note The terms form and window describe the same entity. A window is what the user sees on the desktop when the application is running. A form is the same entity at design time. The proper term is a Windows form, as opposed to a Web form, but I will refer to them as forms.

Forms have built-in functionality that is always available without any programming effort on your part. You can move a form around, resize it, and even cover it with other forms. You do so with the mouse, or with the keyboard through the Control menu. As you will see, forms are not passive containers; they’re “intelligent” objects that are aware of the controls placed on them and can actually manipulate the controls at runtime. For example, you can instruct the form to resize certain controls when the form itself is resized. Forms have many trivial properties that won’t be discussed here. Instead, let’s jump directly to the properties that are unique to forms and then look at how to manipulate forms from within an application’s code.

The forms that constitute the visible interface of your application are called Windows forms; this term includes both the regular forms and dialog boxes, which are simple forms you use for very specific actions, such as to prompt the user for a specific piece of data or to display critical information. A dialog box is a form with a small number of controls, no menus, and usually an OK and a Cancel button to close it. For more information on dialog boxes, see the section “Forms vs. Dialog Boxes” later in this chapter. Everything you’ll read about forms in the following sections applies to dialog boxes as well, even if some form features (such as menus) are never used with dialog boxes.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com