Hi Cor
I have modified the code to illustrate the problem. You can likewise paste
it into a forms app and run it. Mine happens to be called
WindowsApplicat ion7.
<code>
Option Explicit On
Option Strict On
Public Class Form1
Inherits System.Windows. Forms.Form
Private components As System.Componen tModel.IContain er
Friend WithEvents Button1 As System.Windows. Forms.Button
Friend WithEvents Button2 As System.Windows. Forms.Button
Friend UserControlA As WindowsApplicat ion7.UserContro l1
Public Sub New()
MyBase.New()
InitializeCompo nent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private Sub InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.Button2 = New System.Windows. Forms.Button
Me.UserControlA = New WindowsApplicat ion7.UserContro l1
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(240, 16)
Me.Button1.Name = "Button1"
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Remove"
'
'Button2
'
Me.Button2.Loca tion = New System.Drawing. Point(240, 56)
Me.Button2.Name = "Button2"
Me.Button2.TabI ndex = 1
Me.Button2.Text = "Raise Event"
'
' UserControlA
Me.UserControlA .Location = New Point(1, 1)
Me.UserControlA .Size = New System.Drawing. Size(250, 250)
Me.Name = "UserContro lA"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(320, 237)
Me.Controls.Add (Me.Button2)
Me.Controls.Add (Me.Button1)
Me.Controls.Add (Me.UserControl A)
Me.Name = "Form1"
Me.ResumeLayout (False)
End Sub
Public Event MyEvent(ByVal sender As Object, ByVal e As EventArgs)
Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Me.ClientSize = New System.Drawing. Size(500, 500)
AddHandler MyEvent, AddressOf UserControlA.Us erControl_Event Handler
End Sub
Private Sub OnEvent()
RaiseEvent MyEvent(Me, New EventArgs)
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Controls.Remove (UserControlA)
UserControlA.Di spose()
End Sub
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
OnEvent()
End Sub
End Class
Public Class UserControl1
Inherits System.Windows. Forms.UserContr ol
Public Sub New()
MyBase.New()
InitializeCompo nent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private components As System.Componen tModel.IContain er
Friend WithEvents TextBox1 As System.Windows. Forms.TextBox
Private Sub InitializeCompo nent()
Me.TextBox1 = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
Me.TextBox1.Loc ation = New System.Drawing. Point(0, 0)
Me.TextBox1.Mul tiline = True
Me.TextBox1.Siz e = New System.Drawing. Size(100, 100)
Me.TextBox1.Tab Index = 0
Me.TextBox1.Tex t = ""
Me.Controls.Add (Me.TextBox1)
Me.Name = "UserContro l1"
Me.Size = New System.Drawing. Size(101, 101)
Me.ResumeLayout (False)
End Sub
Public Sub UserControl_Eve ntHandler(ByVal sender As Object, ByVal e As
EventArgs)
MessageBox.Show ("I am here")
End Sub
End Class
</code>
When the form is created two buttons and a user control are created. When
the form loads an even handler is added for the event MyEvent in Form1. The
handler is in the user control.
Click the button "Raise Event" and you will see a message box. Each time it
is clicked that is what happens.
Now, click the "Remove" button and the user control will disappear. It also
gets disposed.
Click the "Raise Event" button again, and the message box still appears,
even though the user control has gone. What I am trying to achieve is that
when the user control is destroyed, it no longer lives on to service events.
Thanks.
Charles
"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:eh******** ******@tk2msftn gp13.phx.gbl...
Hi Charles,
Is this something you want, (I use that click event with a "d" in the
textbox to remove the usercontrol have a look for that, just dirthy) (The
control is in the code now, you can paste it in a empty form/vb and it
should run).
Cor
Public Class Form1
Inherits System.Windows. Forms.Form
Public Sub New()
MyBase.New()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private components As System.Componen tModel.IContain er
Friend UserControlA(0) As WindowsApplicat ion10.UserContr ol1
Private Sub Form1_Load(ByVa l sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Me.SuspendLayou t()
ReDim UserControlA(Us erControlA.Leng th)
Dim Cease As Integer = UserControlA.Le ngth - 1
CreateNextContr ol(Cease)
ReDim UserControlA(Us erControlA.Leng th)
Cease = UserControlA.Le ngth - 1
CreateNextContr ol(Cease)
Me.ClientSize = New System.Drawing. Size(500, 500)
Me.ResumeLayout (False)
End Sub
Private Sub CreateNextContr ol(ByVal Cease As Integer)
Me.UserControlA (Cease) = New WindowsApplicat ion10.UserContr ol1
Me.UserControlA (Cease).Locatio n = New System.Drawing. Point _
((Cease - 1) * 250 + 1, 1)
Me.UserControlA (Cease).Size = New System.Drawing. Size(250, 250)
Me.UserControlA (Cease).TabInde x = Cease
AddHandler UserControlA(Ce ase).Click, _
AddressOf UserControlA_Cl ick
Me.Controls.Add (Me.UserControl A(Cease))
End Sub
Private Sub UserControlA_Cl ick(ByVal sender As _
System.Object, ByVal e As System.EventArg s)
If DirectCast(send er, TextBox).Text <> "" Then
If DirectCast(send er, TextBox).Text.S ubstring(0, 1).ToLower =
"d" Then
Me.Controls.Rem ove(DirectCast( sender, Control).Parent )
DirectCast(send er, Control).Parent .Dispose()
End If
End If
For Each ctr As Control In Me.Controls
If TypeOf ctr Is UserControl1 Then
DirectCast(ctr, UserControl1).T extBox1.Text = ""
End If
Next
DirectCast(send er, TextBox).Text = "I am here"
End Sub
End Class
Public Class UserControl1
Inherits System.Windows. Forms.UserContr ol
Public Shadows Event Click(ByVal sender As Object, _
ByVal e As System.EventArg s)
Public Sub New()
MyBase.New()
InitializeCompo nent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private components As System.Componen tModel.IContain er
Friend WithEvents TextBox1 As System.Windows. Forms.TextBox
Private Sub InitializeCompo nent()
Me.TextBox1 = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
Me.TextBox1.Loc ation = New System.Drawing. Point(0, 0)
Me.TextBox1.Mul tiline = True
Me.TextBox1.Siz e = New System.Drawing. Size(100, 100)
Me.TextBox1.Tab Index = 0
Me.TextBox1.Tex t = ""
Me.Controls.Add (Me.TextBox1)
Me.Name = "UserContro l1"
Me.Size = New System.Drawing. Size(101, 101)
Me.ResumeLayout (False)
End Sub
Private Sub TextBox1_Click( ByVal sender As Object, _
ByVal e As System.EventArg s) Handles TextBox1.Click
RaiseEvent Click(sender, e)
End Sub
End Class