我是vb.net的新手。想要了解如何将项目添加到列表中。目前,它只添加一个项目。我需要它来保存许多项目,并且必须能够显示另一个文本框中的所有项目。请帮忙!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim patients As List(Of String) = New List(Of String)
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
End Class
答案 0 :(得分:2)
每次单击该按钮时,都会创建一个列表变量的新副本,当然,它最初是空的。你添加一个项目,但那是游戏的结束 如果要保留列表的内容,则需要在全局类范围中移动List变量
Public Class Form1
Dim patients As List(Of String) = New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
.....
End Class
答案 1 :(得分:1)
您需要在Button Click:
之外声明并实例化您的列表Public Class Form1
Dim patients As New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
End Class