Module6Enrichment Activity

advertisement
Module 6 Apply Breakpoint, Watch Window and Try & Catch Programming Enrichment Activity
Create a new project called Perimeter. Add the controls shown. Name appropriately.
1. Using Try...Catch, get input for each side of the rectangle.
2. If all side lengths are positive, calculate and display the perimeter otherwise display the
message "You cannot have a negative length." in the answer label.
3. Your exception MessageBox should display "Incorrect Input. Enter a numeric value." and
clear any previous text from the answer label.
4. Create TextChanged Events to clear the answer if the user clicks in any TextBox.
Answer
Public Class Form1
'This is a sample solution only.
Private Sub btnCalculate_Click_1(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCalculate.Click
Dim dblSide1, dblSide2, dblSide3, dblSide4, dblPerimeter As
Double
Try
dblSide1 = Convert.ToDouble(Me.txts1.Text)
dblSide2 = Convert.ToDouble(Me.txts2.Text)
dblSide3 = Convert.ToDouble(Me.txts3.Text)
dblSide4 = Convert.ToDouble(Me.txts4.Text)
Me.lblDisplayResult.Text = dblPerimeter.ToString()
Catch ex As Exception
MessageBox.Show("Enter numeric values.")
Me.lblDisplayResult.Text = Nothing
End Try
End Sub
Private Sub txts1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txts1.TextChanged
Me.lblDisplayResult.Text = Nothing
End Sub
Private Sub txts2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txts2.TextChanged
Me.lblDisplayResult.Text = Nothing
End Sub
Private Sub txts3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txts3.TextChanged
Me.lblDisplayResult.Text = Nothing
End Sub
Private Sub txts4_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txts4.TextChanged
Me.lblDisplayResult.Text = Nothing
End Sub
End Class
Download