3

我有一个带有列表框的表单,我希望能够使用同一应用程序的所有打开表单来填充它。但是,我希望能够从列表框中选择一个项目,并能够在列表框中关闭与该项目关联的表单。这可能吗?

4

2 回答 2

4

我找到了问题的答案。以下代码有效:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myForms As FormCollection = Application.OpenForms

  For Each frmName As Form In myForms
    ListBox1.Items.Add(frmName.Name.ToString)
  Next
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  If Not ListBox1.SelectedIndex = -1 Then
    Dim myForm As Form = Application.OpenForms(ListBox1.Text)
    myForm.Close()
  End If
End Sub

下面的代码ListBox1_SelectedIndexChanged可以很容易地放在一个按钮中。

于 2013-06-30T05:18:24.750 回答
1

My.Application.OpenForms是您项目中打开的表单的集合。所以像:

For Each f As Form In My.Application.OpenForms
    Me.SomeListBox.Items.Add(f)
Next

然后关闭所选项目,它是

DirectCast(Me.SomeListBox.SelectedItem, Form).Close()
于 2013-06-30T04:17:13.387 回答