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
WindowsApplication7.
<code>
Option Explicit On
Option Strict On
Public Class Form1
Inherits System.Windows.Forms.Form
Private components As System.ComponentModel.IContainer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend UserControlA As WindowsApplication7.UserControl1
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.UserControlA = New WindowsApplication7.UserControl1
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(240, 16)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Remove"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(240, 56)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 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 = "UserControlA"
'
'Form1
'
Me.AutoScaleBaseSize = 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.UserControlA)
Me.Name = "Form1"
Me.ResumeLayout(False)
End Sub
Public Event MyEvent(ByVal sender As Object, ByVal e As EventArgs)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ClientSize = New System.Drawing.Size(500, 500)
AddHandler MyEvent, AddressOf UserControlA.UserControl_EventHandler
End Sub
Private Sub OnEvent()
RaiseEvent MyEvent(Me, New EventArgs)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Controls.Remove(UserControlA)
UserControlA.Dispose()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
OnEvent()
End Sub
End Class
Public Class UserControl1
Inherits System.Windows.Forms.UserControl
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
Me.TextBox1.Location = New System.Drawing.Point(0, 0)
Me.TextBox1.Multiline = True
Me.TextBox1.Size = New System.Drawing.Size(100, 100)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
Me.Controls.Add(Me.TextBox1)
Me.Name = "UserControl1"
Me.Size = New System.Drawing.Size(101, 101)
Me.ResumeLayout(False)
End Sub
Public Sub UserControl_EventHandler(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**********@planet.nl> wrote in message
news:eh**************@tk2msftngp13.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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend UserControlA(0) As WindowsApplication10.UserControl1
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.SuspendLayout()
ReDim UserControlA(UserControlA.Length)
Dim Cease As Integer = UserControlA.Length - 1
CreateNextControl(Cease)
ReDim UserControlA(UserControlA.Length)
Cease = UserControlA.Length - 1
CreateNextControl(Cease)
Me.ClientSize = New System.Drawing.Size(500, 500)
Me.ResumeLayout(False)
End Sub
Private Sub CreateNextControl(ByVal Cease As Integer)
Me.UserControlA(Cease) = New WindowsApplication10.UserControl1
Me.UserControlA(Cease).Location = New System.Drawing.Point _
((Cease - 1) * 250 + 1, 1)
Me.UserControlA(Cease).Size = New System.Drawing.Size(250, 250)
Me.UserControlA(Cease).TabIndex = Cease
AddHandler UserControlA(Cease).Click, _
AddressOf UserControlA_Click
Me.Controls.Add(Me.UserControlA(Cease))
End Sub
Private Sub UserControlA_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
If DirectCast(sender, TextBox).Text <> "" Then
If DirectCast(sender, TextBox).Text.Substring(0, 1).ToLower =
"d" Then
Me.Controls.Remove(DirectCast(sender, Control).Parent)
DirectCast(sender, Control).Parent.Dispose()
End If
End If
For Each ctr As Control In Me.Controls
If TypeOf ctr Is UserControl1 Then
DirectCast(ctr, UserControl1).TextBox1.Text = ""
End If
Next
DirectCast(sender, TextBox).Text = "I am here"
End Sub
End Class
Public Class UserControl1
Inherits System.Windows.Forms.UserControl
Public Shadows Event Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
Me.TextBox1.Location = New System.Drawing.Point(0, 0)
Me.TextBox1.Multiline = True
Me.TextBox1.Size = New System.Drawing.Size(100, 100)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
Me.Controls.Add(Me.TextBox1)
Me.Name = "UserControl1"
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.EventArgs) Handles TextBox1.Click
RaiseEvent Click(sender, e)
End Sub
End Class