2013-11-15 78 views
1
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我做的幾乎只能用代碼表示歉意,但我迷茫......有人請向我解釋爲什麼我的三角計算器沒有按」工作。該程序運行時沒有任何錯誤,如果在輸入任何值之前單擊其中一個按鈕,它將起作用。再次,我真的很抱歉...並感謝您提供的任何幫助。三角計算器vb.net,不明錯誤

+0

當它不工作,會發生什麼? – xpda

回答

1

而不是使用標籤()屬性和一個字符串,你可以從字面上檢查,如果發件人是這樣一個特殊按鈕:

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 
+0

你感到非常驚訝SOOOOOO!你剛剛保存了我的成績! :D,你讓我的一天感謝萬億@idle_Mind – Subwayeatfresh

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並且上面的違規行不執行。

+0

所以我做了我切出Dim「btnButtonClicked As Button = sender」,並用「Dim btnButtonClicked As Button = DirectCast(sender,Button)」替換它。標籤屬性有什麼問題,並感謝一百萬順便幫助這個noob out。 – Subwayeatfresh

+0

@AidanMcGinn - 現在有一個'Tag'屬性的問題,或者是一個問題,如果使用'Tag'有問題? –

+0

哦對不起,你說「那麼你可以檢查標籤屬性並繼續。」那是什麼意思? – Subwayeatfresh