16,554
社区成员
发帖
与我相关
我的任务
分享
Public Class Form1
Dim htForm As New List(Of Form2)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Static index As Integer = 1
Dim newform2 As New Form2
newform2.Text = "窗口 " & index.ToString
'newform2.Tag 这个属性保存窗口的特征数据比用标题更合适,因为实际的窗口标题可能没有1,2,3这样有规律
index += 1
newform2.Show()
htForm.Add(newform2)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Try
Dim newform2 As Form2 = htForm.First(Function(u) u.Text.Substring(3) = TextBox1.Text)
'或者:Dim newform2 As Form2 = htForm.Find(Function(u) u.Text.Substring(3) = TextBox1.Text)
'或者:如果不支持linq的话,通过循环获取也一样
htForm.Remove(newform2)
newform2.Close()
Catch ex As InvalidOperationException
MessageBox.Show("窗口已关闭")
End Try
End Sub
End Class