I have a form with a listbox, and I want to be able to populate it with all open forms of the same application. However, I want to be able to select an Item from the listbox, and be able to close the form associated with that item in the list box. Is this possible to do?
I found the answer to the issue. The following code works:
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
Where the code under ListBox1_SelectedIndexChanged
can very easily be placed in a button.
My.Application.OpenForms
is a collection of the open forms in your project. So something like:
For Each f As Form In My.Application.OpenForms
Me.SomeListBox.Items.Add(f)
Next
Then to close the selected item, it's
DirectCast(Me.SomeListBox.SelectedItem, Form).Close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.