我有一个表单,将商店选中的商品从checkedlistbox转移到另一个表单上的列表框中。
'Form2
Public Sub New(ByVal Citems As List(Of String), ByVal Citems2 As List(Of String))
InitializeComponent()
ListBox1.Items.AddRange(Citems.ToArray)
ListBox2.Items.AddRange(Citems2.ToArray)
Private Sub Cancel_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Cancel the form and returns to Form 1
ListBox1.Items.Clear()
ListBox1.Refresh()
ListBox2.Items.Clear()
ListBox2.Refresh()
Me.Close()
End Sub
'Form 1
Dim Citems As New List(Of String)
Dim Citems2 As New List(Of String)
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each I As String In CheckedListBox1.CheckedItems
Citems.Add(I)
Next
For Each I As String In CheckedListBox2.CheckedItems
Citems2.Add(I)
Next
Dim SecondForm As New Form2(Citems, Citems2)
SecondForm.Show()
这是我想要清除列表框并关闭表单的代码。问题是当它再次打开时,先前选择的项目仍在列表框中。如何在不完全关闭应用程序的情况下清除数据?
答案 0 :(得分:0)
这有效:
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Citems.Clear()
For Each I As String In CheckedListBox1.CheckedItems
Citems.Add(I)
Next
Citems2.Clear()
For Each I As String In CheckedListBox2.CheckedItems
Citems2.Add(I)
Next
在添加之前清除列表。谢谢你的帮助。