Rob,
When you write Controls.Remove(Me), you are trying to remove the form
instance (more than likely) form the controls collection. "Me" isn't the
object that is the subject of the handler (the button), it is the object
that is handling the event (the form).
Presumably if you are creating the controls manually, you are assigning
handlers manually as well. Here is an example in full. Create a Windows
Forms project and add a button to it at the top called "Button1" (name it
"Add"). With the code below, whenever you click "Add", a new button will be
created and docked at the bottom of the form. A handler will also be added
so that when the new button is clicked, it removes itself from the form
controls collection.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim newButton As New Button
newButton.Dock = DockStyle.Bottom
newButton.Text = Controls.Count.ToString
Controls.Add(newButton)
AddHandler newButton.Click, AddressOf AnyButton_Click
End Sub
Private Sub AnyButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim theButton As Button = CType(sender, Button)
RemoveHandler theButton.Click, AddressOf AnyButton_Click
Controls.Remove(theButton)
End Sub
End Class
Robin
"Rob" <ro***@yahoo.comwrote in message
news:bd******************************@comcast.com. ..
Also,
I would think this should work, but does not....
Controls.Remove(Me)