vb.net的三角计算器,未知错误

时间:2013-11-15 18:43:54

标签: vb.net geometry

Public Class Triangle
    Inherits Form1
    Private SideA As Single
    Property a() As Single
        Get
            Return SideA
        End Get
        Set(value As Single)
            SideA = value
        End Set
    End Property
    Private SideB As Single
    Property b() As Single
        Get
            Return SideA
        End Get
        Set(value As Single)
            SideB = value
        End Set
    End Property
    Private SideC As Single
    Property C() As Single
        Get
            Return SideA
        End Get
        Set(value As Single)
            SideC = value
        End Set
    End Property

    Private Semiperimeter As Single
    ReadOnly Property s() As Single
        Get
            Return Semiperimeter
        End Get
    End Property
    'consrtuctor 
    Sub New(ByVal sngSideA As Single, ByVal sngSideB As Single, ByVal sngSideC As Single)

        Me.SideA = sngSideA
        Me.SideB = sngSideB
        Me.SideC = sngSideC
        Me.Semiperimeter = 0.5 * (SideA + SideB + SideC)

    End Sub

    'Public methods 

    '-----------------------------------------------------------------------
    'Returns true if sides are a,b,c are valid for a triangle 
    'post:true returned if te sum of sides a and b are greaerthan side c. False returned otherwise 
    '-----------------------------------------------------------------------

    Public Function ValidTriangle() As Boolean

        If SideA + SideB > SideC Then 'valid triangle
            Return True
        Else
            Return False
        End If
    End Function

    '--------------------------------------------------------------------
    'Pre: sides a,b, and c correspond to a valid triangle 
    'post: Area of a triangle returned 
    '---------------------------------------------------------------------

    Public Function Area() As Single

        Return Math.Sqrt(Semiperimeter * (Semiperimeter - SideA) * (Semiperimeter - SideB) * (Semiperimeter - SideC))

    End Function

    '--------------------------------------------------------------------
    'Returns the type (right, acute, obtuse) of an triangle 
    'Pre: Sides a,b, and c correpond to a valid riangle 
    'Post: type of a triangle returned
    '-------------------------------------------------------------------
    Public Function Type() As String

        Select Case SideC ^ 2
            Case SideA ^ 2 + SideB ^ 2
                Return "Right Triangle."
            Case Is < SideA ^ 2 + SideB ^ 2
                Return "Acute Triangle."
            Case Is > SideA ^ 2 + SideB ^ 2
                Return "Obtuse Triangle."
        End Select
    End Function
End Class
-------------------------------------------------------------------------------------------
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private Sub Calculate(ByVal sender As Object, ByVal e As EventArgs) Handles btnArea.Click, btnPerimeter.Click, btnType.Click

        Dim sngSideA As Single = Val(Me.txtSideA.Text)
        Dim sngSideB As Single = Val(Me.txtSideB.Text)
        Dim sngSideC As Single = Val(Me.txtSideC.Text)
        Dim UserTriangle As New Triangle(sngSideA, sngSideB, sngSideC)

        If UserTriangle.ValidTriangle Then
            Dim btnButtonClicked As Button = sender
            Select Case btnButtonClicked.Tag
                Case "Area"
                    Me.lblArea.Text = UserTriangle.Area
                Case "Perimeter"
                    Me.lblPerimeter.Text = UserTriangle.s * 2
                Case "Type"
                    Me.lblType.Text = UserTriangle.Type
            End Select
        Else
            MessageBox.Show("Measurements are wrong")
            Me.txtSideA.Text = Nothing
            Me.txtSideB.Text = Nothing
            Me.txtSideC.Text = Nothing
        End If
    End Sub

    Private Sub SideChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSideA.TextChanged, txtSideB.TextChanged, txtSideC.TextChanged
        Me.lblArea.Text = Nothing
        Me.lblPerimeter.Text = Nothing
        Me.lblType.Text = Nothing
    End Sub
End Class

好吧soooo我为很多只使用代码而道歉,但我很失落......有人可以向我解释为什么我的三角计算器不起作用。该程序运行没有错误,如果我在输入任何值之前单击其中一个按钮,它将起作用。再一次,我真的很抱歉......感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:2)

这是一个问题:

Dim btnButtonClicked As Button = sender

您需要将sender投射到Button,如下所示:

Dim btnButtonClicked As Button = DirectCast(sender, Button)

现在,您可以像在此代码中一样检查按钮的Tag属性:

Select Case btnButtonClicked.Tag
    Case "Area"
        Me.lblArea.Text = UserTriangle.Area
    Case "Perimeter"
        Me.lblPerimeter.Text = UserTriangle.s * 2
    Case "Type"
        Me.lblType.Text = UserTriangle.Type
    End Select

顺便说一句,当您点击按钮而不输入任何值时,它会起作用,因为UserTriangle.ValidTriangle会返回False并且上面的违规行不会执行。

答案 1 :(得分:1)

您可以逐字检查发件人是否是特定的按钮,而不是使用Tag()属性和字符串:

    If UserTriangle.ValidTriangle Then
        If sender Is btnArea Then
            Me.lblArea.Text = UserTriangle.Area
        ElseIf sender Is btnPerimeter Then
            Me.lblPerimeter.Text = UserTriangle.s * 2
        Else
            Me.lblType.Text = UserTriangle.Type
        End If
    Else
        MessageBox.Show("Measurements are wrong")
        Me.txtSideA.Text = Nothing
        Me.txtSideB.Text = Nothing
        Me.txtSideC.Text = Nothing
    End If