我正在使用该模块在所有形式的程序中声明公共类患者。为了从三个文本框中将数据输入到数组中,我该怎么做。一个用于名称,高度和重量。谢谢
Public Module Module1
Public PatientCount As Integer = 0
Public Class Patient
Public Property Name As String = String.Empty
Public Property Height As Decimal = 0
Public Property Weight As Decimal = 0
End Class
End Module
Dim patients As List(Of Patient) = New List(Of Patient)
用于在列表框中显示患者
For Each p As Module1.Patient In Patients
lstPatients.Items.Add(p.Name)
Next
当前代码: MODULE
Public Module Module1
Public PatientCount As Integer = 0
Public Patients As List(Of Patient) = New List(Of Patient)
Public Class Patient
Public Property Name As String
Public Property Height As Decimal
Public Property Weight As Decimal
Public Sub New(ByVal name As String, ByVal height As Decimal, ByVal weight As Decimal)
name = _Name
weight = _Weight
height = _Height
End Sub
End Class
End Module
表格2(输入数据)
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click
patients.Add(New Patient(txtName.Text, CDec(txtHeight.Text), CDec(txtWeight.Text)))
PatientCount = PatientCount + 1
Label1.Text = PatientCount
End Sub
End Class
表格3(数据清单) 公共类Form3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each p As Patient In Patients
lstPatients.Items.Add("Name: " & p.Name)
lstPatients.Items.Add("Weight: " & p.Weight)
lstPatients.Items.Add("Height: " & p.Height)
lstPatients.Items.Add("___________")
Next
End Sub
End Class
答案 0 :(得分:1)
一个选项是使用一个New构造函数来获取属性的3个值。
Public Module Module1
Public PatientCount As Integer = 0
Public Class Patient
Public Property Name As String = String.Empty
Public Property Height As Decimal = 0
Public Property Weight As Decimal = 0
Public Sub New(_name As String, _height As Decimal, _weight As Decimal)
Name = _name
Height = _height
Weight = _weight
End Sub
End Class
End Module
Dim patients As List(Of Patient) = New List(Of Patient)
假设您已经验证了文本框中的输入并将它们转换为变量(NewName,NewHeight,NewWeight),您可以添加这样的新患者:
patients.Add(New Patient(NewName,NewHeight,NewWeight))
答案 1 :(得分:1)
要存储x个患者的数据,您可以将此代码放在button-clicked
事件处理程序中。
Private Sub ButtonOK_Click()Handles BtnOk.Click()
patients.Add(New Module1.Patient(txtName.Text, CDec(txtHeight.Text), CDec(txtWeight.text))
End Sub
然后将其添加到您的Patient
课程中:
Public Module Module1
Public Class Patient
Public Property Name As String = String.Empty
Public Property Height As Decimal = 0
Public Property Weight As Decimal = 0
Public Sub New(_name as String, _height as decimal, _weight as decimal)
Name = _name
Weight = _weight
Height = _height
End Sub
End Class
End Module
Dim patients As List(Of Patient) = New List(Of Patient)
有了这个,您将在每次按下btnOk时创建患者列表,并且您可以轻松访问任何患者及其数据。
将数据放入列表框中:
Private Sub BTNLIST_Click()Handles BTNLIST.Click
For Each p As Patient in patients
lstbox.Items.Add("Name: " & p.Name)
lstbox.Items.Add("Weight: " & p.Weight)
lstbox.Items.Add("Height: " & p.Height)
lstbox.Items.Add("___________")
Next
End Sub
<强> OPTIONAL 强>
您甚至可以创建一个PatientList
类来帮助您进一步(但删除患者列表)。您可以使用PatientList
类添加有用的功能,例如按姓名或身高查找患者。
Public Class PatientList
Public List(Of Patient) Patients = New List(Of Patient)
Public ReadOnly Property PatientCount() As Integer
Get
Return Patients.Count
End Get
End Property
Public Sub AddPatientToList(name as String, height as decimal, weight as decimal)
Patients.Add(New Patient(name, height, weight)
End Sub
End Class
Dim patients as PatientList
然后在button_click处理程序中添加它:
Private Sub ButtonOK_Click()Handles BtnOk.Click()
patients.AddPatientToList(New Module1.Patient(txtName.Text, CDec(txtHeight.Text), CDec(txtWeight.text))
End Sub
HTH
答案 2 :(得分:0)
如果您想添加患者,则需要创建一个实例,并使用您使用的任何形式的文本框设置其属性。然后将新患者添加到患者列表中,这样就可以了。
Dim patient as new Patient()
patient.Name = txtName.Text()
patient.Height = txtHeight.Text()
patient.Weight = txtWeight.Text()
patients.Add(patient)