30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

192 Procedures Chapter 6<br />

Class FrmMaximum uses a GUI consisting of three TextBoxes (txtFirst, txt-<br />

Second and txtThird) for user input, a But<strong>to</strong>n (cmdMaximum) <strong>to</strong> invoke the calculation<br />

and four Labels, including lblMaximum, which displays the results. We create<br />

these components visually, using the Toolbox, and change their properties in the Properties<br />

window. Lines 7–21 are declarations indicating the name of each component.<br />

Although these lines of code are actually part of the <strong>Visual</strong> Studio .<strong>NET</strong> generated code,<br />

we display them <strong>to</strong> indicate the objects that are part of the form (as always, the complete<br />

code for this program is on the CD-ROM that accompanies this book and at<br />

www.deitel.com).<br />

Line 5 indicates that class FrmMaximum Inherits from System.Windows.Forms.Form.<br />

Remember that all forms inherit from class System.Windows.Forms.Form.<br />

A class can inherit attributes and behaviors (data and methods)<br />

from another class if that class is specified <strong>to</strong> the right of the Inherits keyword. We discuss<br />

inheritance in detail in Chapter 9, Object-Oriented <strong>Program</strong>ming: Inheritance.<br />

FrmMaximum contains two programmer-defined methods. Method Maximum (lines<br />

39–43) takes three Double parameters and returns the value of the largest parameter. Note<br />

that this method definition looks just like the definition of a Function procedure in a<br />

module. The program also includes method cmdMaximum_Click (lines 26–36). When<br />

the user double-clicks a component, such as a But<strong>to</strong>n, in Design mode, the IDE generates<br />

a method that Handles an event (i.e., an event handler). An event represents a user<br />

action, such as clicking a But<strong>to</strong>n or altering a value. An event handler is a method that is<br />

executed (called) when a certain event is raised (occurs). In this case, method<br />

cmdMaximum_Click handles the event in which But<strong>to</strong>n cmdMaximum is clicked.<br />

<strong>Program</strong>mers write code <strong>to</strong> perform certain tasks when such events occur. By employing<br />

both events and objects, programmers can create applications that enable more sophisticated<br />

user interactions than those we have seen previously. Event-handler names created<br />

by the IDE begin with the object’s name, followed by an underscore and the name of the<br />

event. We explain how <strong>to</strong> create our own event handlers, which can be given any name, in<br />

Chapter 12, Graphical User Interface Concepts: Part 1.<br />

When the user clicks cmdMaximum, procedure cmdMaximum_Click (lines 26–36)<br />

executes. Lines 31–33 retrieve the values in the three TextBoxes, using the Text property.<br />

The values are converted implicitly <strong>to</strong> type Double and s<strong>to</strong>red in variables value1,<br />

value2 and value3.<br />

Line 35 calls method Maximum (lines 39–43) with the arguments value1, value2<br />

and value3. The values of these arguments are then s<strong>to</strong>red in parameters valueOne,<br />

valueTwo and valueThree in method Maximum. Maximum returns the result of the<br />

expression on line 42, which makes two calls <strong>to</strong> method Max of the Math class. Method<br />

Max returns the largest of its two Double arguments, meaning the computation in line 42<br />

first compares valueOne and valueTwo, then compares the value returned by the first<br />

method call <strong>to</strong> valueThree. Calls <strong>to</strong> methods, such as Math.Max, that are defined in a<br />

class in the FCL must include the class name and the dot (.) opera<strong>to</strong>r (also called the<br />

member access opera<strong>to</strong>r). <strong>How</strong>ever, calls <strong>to</strong> methods defined in the class that contains the<br />

method call need only specify the method name.<br />

When control returns <strong>to</strong> method cmdMaximum_Click, line 35 assigns the value<br />

returned by method Maximum <strong>to</strong> lblMaximum’s Text property, causing it <strong>to</strong> be displayed<br />

for the user.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!