AdorableVB
AdorableVB

Reputation: 1393

delete a form instance from a list(of form)

Dim fList As New List(Of Form)

every time I hit a marker..

Dim f As New Form2
Me.AddOwnedForm(f)
fList.Add(f)
f.Show()

I create an instance and add it to the fList..

however, if I close that particular instance.. it stays to the fList and whenever I hit a certain event that calls out all from the list, the supposedly closed form appears..

here is the flow of my program :

Click marker --> show form, add to list
Drag map --> hides forms
mouseLeave --> shows hidden forms
but when I manually close the form, when I Drag and MouseLeave it appears again.

Question is : How can I delete the added instance to the fList if I close it?

It should only appear if it was opened again through clicking the marker. hope you get what I mean. Thanks!

Upvotes: 0

Views: 370

Answers (2)

Matt Wilko
Matt Wilko

Reputation: 27342

If you still have a reference to the form object:

fList.Remove(f) 'f is a reference to a form object

Documentation: http://msdn.microsoft.com/en-us/library/cd666k3e(v=vs.110).aspx

If you know the index of the form in the List:

fList.RemoveAt(index) 'removes the item at position of index

Documentation: http://msdn.microsoft.com/en-us/library/5cw9x18z(v=vs.110).aspx

If you want to remove by Text you can use the first method to do something like this:

For Each f as Form in fList
    If f.Text = "foo" Then fList.Remove(f)
    Exit For
Next

Or using Linq:

fList.RemoveAll(Function(x) x.Text = "foo")

Upvotes: 2

AdorableVB
AdorableVB

Reputation: 1393

Matt Miko's answer should be the best suited, but I cannot apply it to how I created my list. Anyway, I solved the problem.

Since I need to delete the form element in fList which was the one that was closed..

Public fList As New List(Of Form)

then added this code in Form2:

Form1.fList.Remove(Me)

didn't know this was possible, but it worked for me.

if you can see it logically, if I hit X on the form, it will remove that form from the list. so that my program will only show the chosen forms. :)

Upvotes: 0

Related Questions