1.การคืนค่าพร้อมกัน 2 ค่าจาก Function ไปยัง Form ทำยังไงครับ
2.หากคืนค่า 2 ค่าได้ แล้วตอนรับค่ามาแสดงผลทำยังไง แบบไหนได้บ้างครับ
ไม่ทราบว่าทำได้ไหม แนะนำทีครับ
อีกอย่าง ถ้าใครเห็นว่าโค้ดผมมันวุ่นวายสุนัขไม่รับประทาน ก็แนะนำวิธีเขียนโค้ดง่ายๆได้นะครับ
ส่วนของฟอร์ม Code (VB.NET)
Public Class Form_homeloop
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
TextBox1.MaxLength = 5
TextBox2.MaxLength = 5
ComboBox1.Items.Add(" ")
ComboBox1.Items.Add("บวก")
ComboBox1.Items.Add("ลบ")
ComboBox1.Items.Add("คูณ")
ComboBox1.Items.Add("หาร")
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox2.Items.Add(" มากกว่า ")
ComboBox2.Items.Add(" น้อยกว่า ")
ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ComboBox1.Enabled = False
TextBox2.Enabled = False
Label3.Enabled = False
TextBox3.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
TextBox1.Focus()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r, n, m As Integer
If TextBox1.Text = "" Then
MsgBox("กรุณาป้อนค่าที่หนึ่ง")
TextBox1.Focus()
Exit Sub
ElseIf TextBox2.Text = "" Then
MsgBox("กรุณาป้อนค่าที่สอง")
TextBox2.Focus()
Exit Sub
ElseIf TextBox3.Text = "" Then
MsgBox("กรุณาป้อนค่าที่สาม")
TextBox2.Focus()
Exit Sub
End If
If ComboBox1.SelectedIndex = 0 Then
MsgBox("กรุณาเลือกประเภทการคำนวณ")
Else
Dim num3 As Integer = CInt(TextBox3.Text)
Dim lo As Integer = 1
Dim ol As Integer = 0
n = CInt(TextBox1.Text)
m = CInt(TextBox2.Text)
Dim o As Integer = CInt(ComboBox1.SelectedIndex)
Dim p As Integer = CInt(ComboBox2.SelectedIndex)
'homeloop(TextBox1.Text, TextBox2.Text, TextBox3.Text, o, p)
Try
If lo = 1 Then
TextBox4.Text = "กำหนด " & TextBox1.Text & " " & ComboBox1.Text & " " & TextBox2.Text & vbCrLf &
"โดยให้" & ComboBox1.Text & " " & TextBox2.Text & " เรื่อยๆ" & vbCrLf &
"จนผลลัพธ์" & ComboBox2.Text & " " & num3 & vbCrLf &
"โดยทำการวนทั้งหมด " & homeloop(TextBox1.Text, TextBox2.Text, TextBox3.Text, o, p) & " รอบ" & vbCrLf &
"และค่าสุดท้ายคือ " & r
Else
TextBox4.Text = "กำหนด " & TextBox1.Text & " " & ComboBox1.Text & " " & TextBox2.Text & vbCrLf &
"โดยให้" & ComboBox1.Text & " " & TextBox2.Text & " เรื่อยๆ" & vbCrLf &
"จนผลลัพธ์" & ComboBox2.Text & " " & num3 & vbCrLf &
"โดยทำการวนทั้งหมด " & homeloop(TextBox1.Text, TextBox2.Text, TextBox3.Text, o, p) & " รอบ" & vbCrLf &
"และค่าสุดท้ายคือ " & r
End If
Catch
MsgBox("ผิดพลาด ลองใหม่")
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ComboBox1.Enabled = False
TextBox2.Enabled = False
Label3.Enabled = False
TextBox3.Enabled = False
Button1.Enabled = False
TextBox1.Focus()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox2.Enabled = True
Label3.Enabled = True
'------------------------------
If ComboBox1.SelectedIndex = 0 Then
Label3.Text = " "
TextBox2.Enabled = False
Label3.Enabled = False
ElseIf ComboBox1.SelectedIndex = 1 Then
Label3.Text = "มากกว่า"
ComboBox2.SelectedIndex = 0
ElseIf ComboBox1.SelectedIndex = 2 Then
Label3.Text = "น้อยกว่า"
ComboBox2.SelectedIndex = 1
ElseIf ComboBox1.SelectedIndex = 3 Then
Label3.Text = "มากกว่า"
ComboBox2.SelectedIndex = 0
ElseIf ComboBox1.SelectedIndex = 4 Then
Label3.Text = "น้อยกว่า"
ComboBox2.SelectedIndex = 1
End If
'------------------------------
End Sub
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
ComboBox1.Enabled = True
ComboBox1.SelectedIndex = 0
Button2.Enabled = True
End Sub
Private Sub TextBox2_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
TextBox3.Enabled = True
End Sub
Private Sub TextBox3_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
Button1.Enabled = True
End Sub
End Class
ส่วนของฟังก์ชั่นในโมดูล Code (VB.NET)
Module Module1
Public Function homeloop(ByVal num1 As String, num2 As String, nam3 As String, q As String, w As String) As Long
Dim num3 As Integer = CInt(nam3)
Dim m As Integer = CInt(num2)
Dim n As Integer = CInt(num1)
Dim a As Integer = q
Dim s As Integer = w
Dim r As Integer
Dim lo As Integer = 1
Dim ol As Integer = 0
If s = 0 Then 'มากกว่า
If a = 1 Then
r = n + m
If r < num3 + 1 Then
While r < num3 + 1
r = m + r
lo = lo + 1
End While
End If
ElseIf a = 2 Then
r = n - m
If r < num3 + 1 Then
While r < num3 + 1
r = r - m
lo = lo + 1
End While
End If
ElseIf a = 3 Then
r = n * m
If r < num3 + 1 Then
While r < num3 + 1
r = m * r
lo = lo + 1
End While
End If
ElseIf a = 4 Then
r = n / m
If r < num3 + 1 Then
While r < num3 + 1
r = r / m
lo = lo + 1
End While
End If
End If
ElseIf s = 1 Then
If a = 1 Then
r = n + m
If r > num3 + 1 Then
While r > num3 + 1
r = m + r
lo = lo + 1
End While
End If
ElseIf a = 2 Then
r = n - m
If r > num3 + 1 Then
While r > num3 + 1
r = r - m
lo = lo + 1
End While
End If
ElseIf a = 3 Then
r = n * m
If r > num3 + 1 Then
While r > num3 + 1
r = m * r
lo = lo + 1
End While
End If
ElseIf a = 4 Then
r = n / m
If r > num3 + 1 Then
While r > num3 + 1
r = r / m
lo = lo + 1
End While
End If
End If
End If
If lo = 1 Then
lo = 0
Return r & lo 'ส่งค่าทั้ง 2 ตัวนี้ครับ
Else
Return r & lo 'ส่งค่าทั้ง 2 ตัวนี้ครับ
End If
End Function
End Module
Private Sub getResult(ByVal val1 As Integer, ByVal val2 As Integer, ByRef result1 As Integer, ByRef result2 As Integer)
result1 = val1 - val2
result2 = val1 + val2
End Sub
Private Delegate Function DelegateFormula(ByVal m As Double, ByVal n As Double) As Double
Private Formula As New Dictionary(Of String, DelegateFormula)() From {{"+", Function(m, n) m + n},
{"-", Function(m, n) m - n},
{"*", Function(m, n) m * n},
{"/", Function(m, n) m / n}
}