Com web hosting - Chapter 6 Methods 185 (e.g., when the user

Chapter 6 Methods 185 (e.g., when the user enters name and address information) or to display many pieces of data at once (such as the values of the first ten squares in this example). To introduce more elaborate user interfaces, the program in Fig. 6.3 illustrates two graphical user interface concepts attaching multiple GUI components to an application and event handling. To build this application, we use the Windows Form Designer to add a Button GUI component object to the Form. This example also uses a label to display the results of calculating the first ten squares. The program invokes a special method, known as an event handler, when the user clicks the Calculate Squares button. An event handler is a method that performs some action in response to an event. Events occur when certain actions take place in a graphical user interface, such as when the user clicks a button. Using GUI component objects and events together allows programmers to create applications that interact with users in more sophisticated ways than we have seen previously. In Visual Studio .NET s Windows Form Designer, double clicking on a GUI component object causes Visual Studio .NET to generate an empty event handler method. The event handler method s name defaults to the GUI component s name, followed by an underscore and the name of the event. The programmer then can fill in the event handler method with code that performs a particular task. In Fig. 6.3, method calculateButton_Click (lines 30 46) is an event handler method for calculateButton s Click event (i.e., the event that occurs when the user clicks the button). When the user clicks the button, line 33 assigns the empty string (”") to outputLabel s Text property to ensure that the output does not scroll off the form if the user presses the Calculate Squares button more than once. Lines 36 44 repeatedly invoke method Square to calculate the squares of the integers from 1 to 10. Line 39 invokes the Square method and passes the variable counter as an argument. As in some of our previous Windows applications, we create a label called output- Label to display the program s output. Every label contains a string property called Text, which can be accessed using the dot operator (.). We append the results of the square calculations to this label s Text property. Line 39 declares int variable result to store the result of each square calculation. Lines 36 44 contain a for repetition structure in which each iteration of the loop calculates the Square of the current value of control variable counter and stores the value in result. Lines 42 43 concatenate each result to the Text property of outputLabel. At the end of the loop, the Label contains the results of squaring the values from 1 to 10. The program invokes method Square on line 39. The parentheses, (), after Square represent the method-call operator, which has high precedence. At this point, the program makes a copy of the value of counter (the argument to the method call), and program control transfers to method Square (defined at lines 24 28). Method Square receives the copy of the value of counter in the parameter y. Then, Square calculates y*y (line 26). Method Square uses a return statement to return (i.e., give back) the result of the calculation to the statement that invoked Square (located in line 39). Line 39 then assigns the returned value to variable result. Lines 42 43 concatenate “Thesquare of”, the value of counter, “is”, the value of resultand a newline character to the end of outputLabel s Text property. The for repetition structure repeats this process 10 times. The definition of method Square (line 24) shows (inside the parentheses) that Square expects an integer parameter y. Parameter y is the name that holds the value passed to Square as an argument. The parameter name provides access to the argument
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Leave a Reply