------------------------------------------------------------------------------------------------------------------------------

โปรแกรม VB

เรื่อง...การทำโปรแกรมฟังก์ชั่นคำนวณ

------------------------------------------------------------------------------------------------------------------------------

โค้ด Form ฟังก์ชั่นคำนวณ

Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Label1.Text = "ป้อนค่า 1"
Label2.Text = "ป้อนค่า 2"
Label3.Text = ""
TextBox1.Text = 5
TextBox1.Text = 2
Button1.Text = "คำนวณ"
Dim op(4) As String
op(0) = "+"
op(1) = "-"
op(2) = "*"
op(3) = "/"
Dim i As Byte = 0
Do While i < 4
ComboBox1.Items.Add(op(i))
i = i + 1
Loop
ComboBox1.SelectedIndex = 0
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label3.Text = calnum(TextBox1.Text, TextBox2.Text, ComboBox1.Text)
End Sub
End Class

 

โค้ด Module ฟังก์ชั่นคำนวณ

Module Module1
Function calnum(ByVal t1 As String, ByVal t2 As String, ByVal p As String) As Single
Dim r As Single
Dim txt1 As Integer
Dim txt2 As Integer
Dim op As String
txt1 = CInt(t1)
txt2 = CInt(t2)
op = p
Select Case op
Case "+"
r = txt1 + txt2
Case "-"
r = txt1 - txt2
Case "*"
r = txt1 * txt2
Case "/"
r = txt1 / txt2
End Select
Return r
End Function
End Module

ตัวอย่างโปรแกรมฟังก์ชั่นคำนวณ

-----------------------------------------------------------------------------------------------------------------------------

กลับหน้าหลัก

 

Free Web Hosting