Public Class Form1
Dim i, l, txtnum, po, p3, pri As Integer
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtdate.Text = Date.Today
Button2.Enabled = False
txtmean.Enabled = False
txtsd.Enabled = False
txt3sd.Enabled = False
txtmp3sd.Enabled = False
txtm3sd.Enabled = False
txtmin.Enabled = False
txtmax.Enabled = False
txtrang.Enabled = False
End Sub
Public Sub Maketxtbox()
txtnum = Convert.ToInt32(txttest.Text)
l = 30
For Me.i = 1 To txtnum
'Dim ponum(i) As String
po = 100 + (i * l)
Dim txtpo As New TextBox
txtpo.Location = New Point(150, po)
txtpo.Name = "txtpo" + i.ToString
txtpo.Text = txtpo.Name
'ponum(i) = txtpo.Text
Me.Controls.Add(txtpo)
p3 = 100 + (i * l)
Dim txtp3 As New TextBox()
txtp3.Location = New Point(350, p3)
txtp3.Name = "txtp3" + i.ToString
txtp3.Text = txtp3.Name
Me.Controls.Add(txtp3)
pri = 100 + (i * l)
Dim txtpri As New TextBox()
txtpri.Location = New Point(550, pri)
txtpri.Name = "txtpri" + i.ToString
txtpri.Text = txtpri.Name
Me.Controls.Add(txtpri)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If txttest.Text = "" Then
MessageBox.Show("กรุณากรอกจำนวนตัวอยากในการทดลอง")
txttest.Focus()
Else
Maketxtbox()
'Dim btnsum As New Button
'btnsum.Location = New Point(812, 110)
'btnsum.Text = "SUM"
'btnsum.Name = "btnsum"
'Me.Controls.Add(btnsum)
txttest.Enabled = False
lotno.Enabled = False
Button1.Enabled = False
txtdate.Enabled = False
cus.Enabled = False
Button2.Enabled = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
txtmean.Enabled = True
txtsd.Enabled = True
txt3sd.Enabled = True
txtmp3sd.Enabled = True
txtm3sd.Enabled = True
txtmin.Enabled = True
txtmax.Enabled = True
txtrang.Enabled = True
End Sub
End Class
ช่วยทำ Function Mean ให้ดูเป็นตัวอย่างครับ สามารถเอา ไปประยุกต์ใช้ได้ทุก function ที่เหลือครับ
อันนี้มัน จะนำค่าของ textbox ที่ชื่อ txtpo มาบวก กัน แล้วหาร ด้วยจำนวนของ textbox ทั้งหมด ครับ
Code (VB.NET)
Private Sub Mean()
If txttest.Text.Trim = "" Then Exit Sub
Dim dMean As Double = 0
For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox Then
Dim T1 As TextBox = ctl
If InStr(T1.Name, "txtpo") > 0 Then
dMean += Val(T1.Text)
End If
End If
Next
MessageBox.Show((dMean / txtnum).ToString("n2"))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
txtmean.Enabled = True
txtsd.Enabled = True
txt3sd.Enabled = True
txtmp3sd.Enabled = True
txtm3sd.Enabled = True
txtmin.Enabled = True
txtmax.Enabled = True
txtrang.Enabled = True
Me.Mean()
End Sub