วันพฤหัสบดีที่ 28 กุมภาพันธ์ พ.ศ. 2556

ขั้นตอนการทำโปรแกรมคำนวณพื้นที่รูปสามเหลี่ยม


ขั้นตอนการทำโปรแกรมคำนวณพื้นที่รูปสามเหลี่ยม

1.เรียกใช้งาน Visual Basic
2.จากไดอะล็อก,New Projact
3.ออกแบบหน้าตาแอพพลิเคชันดังนี้




4.กำหนดพร็อพเพอร์ตี้ให้กับคอนโทรลต่างๆ ดังนี้
      ออบเจ็กต์
      พร็อพเพอร์ตี้
            ค่าที่กำหมด
ฟอร์ม
Name
frmTriArea
Caption
ตัวอย่างการใช้งาน Function/คำนวณพื้นที่สามเหลี่ยม
Label
Name
Label1
Caption
ความยาวฐาน
Label
Name
Label2
Caption
ความสูง
TextBox
Name
txtBase
TextBox
Name
txtHeight
TextBox
Name
txtResult
Button
Name
cmdCalc
Caption
คำนวณพื้นที่สามเหลี่ยม
Button
Name
cmdclear
Caption
เคลียร์

5. ในการทำงานนั้นผู้ใช้จะกรอกความยาวฐานและความสูง แล้วคลิกปุ่ม  คำนวณพื้นที่สามเหลี่ยม  ซึ่งจะคำนวณพื้นที่สามเหลี่ยมแล้วแสดงผลลัพธ์ใน TextBox

Private Sub cmdCalc_Click()
    Dim resp As Boolean, base As Double, height As Double
    resp = CheckParameter(txtBase.Text, txtHeight.Text)
    If resp = False Then
        MsgBox "You must determine the length of the base. And the high number of claims", vbOKOnly + vbExclamation, "Mistake"
        Call ClearAll
        Exit Sub
        End If
        ' Calculate the area of a triangle formula 1/2 x base x height.
        txtResult = "Area =" & CStr(0.5 * Val(txtBase.Text) * Val(txtHeight.Text))
End Sub

6. แต่ก่อนที่จะคำนวณค่า เราจะตรวจสอบก่อนว่าผู้ช้งานกรอกความยาวฐาน และความสูงเหมาะสมหรือไม่ นั่นคือต้องกรอกเป็นตัวเลขเท่านั้น และตัวเลขนั้นต้องมากกว่า 0 ด้วย ซึ่งเราเขียนฟังก์ชัน CheckParameter ขึ้นมาเพื่อตรวจสอบ




Function CheckParameter(base As String, height As String) As Boolean
    If IsNumeric(base) And IsNumeric(height) Then   'Users enter a number or not.
        If base > 0 And height > 0 Then             'Users enter 0 or a negative number or not.
            CheckParameter = True
            Exit Function
        End If
    End If
    CheckParameter = False
End Function

7. ส่วนโค้ดในส่วนอื่นๆ นั้นมีรายละเอียดดังนี้
               

Private Sub cmdClear_Click()
    Call ClearAll
End Sub

Sub ClearAll()
    txtBase.Text = ""
    txtHeight.Text = ""
    txtResult.Text = ""
    MsgBox "Fill out the length of the base and height to calculate the area of a triangle.", vbOKOnly + vbInformation
End Sub






8.ทดสอบการใช้งานของแอพพลิเคชันที่สร้างขึ้น โดยกด <F5> จะได้ผลการทำงานดังนี้
123.jpg







                                                                                                                              
156.jpg








2.jpg






5.jpg                                                                     







 


6.jpg

ไม่มีความคิดเห็น:

แสดงความคิดเห็น