2013-06-30 23 views

回答

3

我找到了答案的问题。下面的代码工作:

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下的代码可以很容易地放置在一个按钮。

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() 
+1

只是一个建议@minitech;你不能添加一个表单到一个列表框:)你可能想要将它改为:Me.SomeListBox.Items.Add(f.name) – Codexer

+1

@MrCoDeXeR:然后关闭的东西会打破!我不知道表单的字符串表达式在ListBox中是什么,但它会以某种方式显示出来。 :)'Add'需要一个对象。 (虽然我目前没有Visual Studio,但是...) – Ryan

+0

我测试了这个方法,并且列表框显示空白项目,但项目在那里,如果关闭项目,整个程序关闭。 – TheRyan722