views:

221

answers:

3

How to implement the Dispose(boolean) at a UserControl... when the VS Designer already implemented it with a DebuggerNonUserCode attribute? Will my modifications on this method removed?

(code from UserControl.Designer.vb)

<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
+3  A: 

You need to remove the Dispose method from the designer file and add it to your source file.
You should probably also remove the DebuggerNonUserCode attribute.

At least in C#, the designer will not automatically put the Dispose back into the designer file, and I'd be shocked if the VB designer did.

SLaks
aha... without *DebuggerNonUserCode*...
serhio
+1  A: 

One solution would be to encapsulate any disposable Types you are using in classes derived from System.ComponentModel.Component or which implement System.ComponentModel.IComponent.

You can then add them to the IContainer that is instantiated by the designer-generated code, and they will be disposed along with other components.

E.g.

class MyDisposableComponent : IComponent
{
    ... implementation
}

class MyUserControl : UserControl
{
    MyDisposableComponent myDisposableComponent;

    ...

    void SomeMethod()
    {
        myDisposableComponent = new MyDisposableComponent();
        components.Add(myDisposableComponent);
        // myDisposableComponent will be disposed automatically when the
        // IContainer components is disposed by the designer-generated
        // Dispose implementation.
    }

    ...
}
Joe
+1  A: 

If you make modifications to that method in the .Designer.vb file, they will not be overwritten. The DebuggerNonUserCode attribute simply means that if you are debugging that code, you won't allowed to step into it. It will always step over.

Nick