Page 194 - Open Soource Technologies 304.indd
P. 194

Event Driven Programming



                          Private Sub btnCalculate_Click(...) Handles btnCalculate.Click
                            ‘Calculate the length of the hypotenuse of a right triangle
                            Dim a, b As Double
                            a = CDbl(txtSideOne.Text)
                            b = CDbl(txtSideTwo.Text)
                            txtHyp.Text = CStr(Hypotenuse(a, b))
                          End Sub
                          function Hypotenuse(ByVal a As Double, ByVal b As Double) As Double
                            ‘Calculate the hypotenuse of a right triangle
                            ‘having sides of lengths a and b
                            Return Math.Sqrt(a ^ 2 + b ^ 2)
                          End function
                          [Run, type 3 and 4 into the text boxes, and then click the button.]























                          8.3  User-Defined Function Having No Parameters


                          Function, like sub function, need not have any parameters.

                                        1: The following program uses a parameterless function.


                          Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
                            ‘Request and display a saying
                            txtBox.Text = Saying()
                          End Sub
                          function Saying() As String
                            ‘Retrieve a saying from the user
                            Return InputBox(“What is your favorite saying?”)




                          188                    LOVELY PROFESSIONAL UNIVERSITY
   189   190   191   192   193   194   195   196   197   198   199