Home All Groups Group Topic Archive Search About

Can you edit auto generated Partial Class Designer VB code?

Author
17 May 2009 3:40 PM
Mike
Folks, I think this is a simple yes or no answer.

I see .NET creates XXXXX.Designer.VB partial classes for main code
class and component classes.

My question is can I edit them?

In these classes, it has this note for the last private method
InitializeComponent():

  'NOTE: The following procedure is required by the Windows Form
         Designer
  'It can be modified using the Windows Form Designer.
  'Do not modify it using the code editor.
  Private Sub InitializeComponent()

But at the top of the class, for the other functions, in particular
like New(), there is no note.

Does this mean I can safely add/edit this file as long I leave the
InitializeComponent method alone?

I ask because when you create a component, I want to add more
constructor initialization so I when add a New constructor   to the
class the compiler does not like it because its already defined in the
partial class.

I don't want to lost work by editing a file you not suppose to.

Thanks

--

Author
17 May 2009 4:05 PM
Family Tree Mike
Show quote Hide quote
"Mike" <unkn***@unknown.tv> wrote in message
news:e59neXw1JHA.3812@TK2MSFTNGP05.phx.gbl...
> Folks, I think this is a simple yes or no answer.
>
> I see .NET creates XXXXX.Designer.VB partial classes for main code class
> and component classes.
>
> My question is can I edit them?
>
> In these classes, it has this note for the last private method
> InitializeComponent():
>
>  'NOTE: The following procedure is required by the Windows Form
>         Designer
>  'It can be modified using the Windows Form Designer.
>  'Do not modify it using the code editor.
>  Private Sub InitializeComponent()
>
> But at the top of the class, for the other functions, in particular like
> New(), there is no note.
>
> Does this mean I can safely add/edit this file as long I leave the
> InitializeComponent method alone?
>
> I ask because when you create a component, I want to add more constructor
> initialization so I when add a New constructor   to the class the compiler
> does not like it because its already defined in the partial class.
>
> I don't want to lost work by editing a file you not suppose to.
>
> Thanks
>
> --
>
>
>

I don't think it is advised to put any code in the .Designer.vb file.

In the case you describe, I would put a call to a second initialization
routine which is contained in the UserControl1.vb code.  Put the call into
the public sub New() code in the main code.

public class UserControl1
  private sub MyInitRoutine()
  end sub

  public sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    MyInitRoutine()
  end sub
end class

--
Mike
Author
17 May 2009 7:21 PM
Mike
Family Tree Mike wrote:

Show quoteHide quote
> I don't think it is advised to put any code in the .Designer.vb file.
>
> In the case you describe, I would put a call to a second initialization
> routine which is contained in the UserControl1.vb code.  Put the call
> into the public sub New() code in the main code.
>
> public class UserControl1
>  private sub MyInitRoutine()
>  end sub
>
>  public sub New()
>    ' This call is required by the Windows Form Designer.
>    InitializeComponent()
>
>    ' Add any initialization after the InitializeComponent() call.
>    MyInitRoutine()
>  end sub
> end class

Thats the thing, you can't add a public sub New() of the same
signature that are already magically added to the partial class.

The MyComponent.vb has this;

public class MyComponent
     public sub new()
     end sub
end class

The MyComponent.Designer.vb has

Partial Class MyComponent
     Inherits System.ComponentModel.Component

    <System.Diagnostics.DebuggerNonUserCode()> _
     Public Sub New(ByVal container As System.ComponentModel.IContainer)
         MyClass.New()

         'Required for Windows.Forms Class Composition Designer support
         If (container IsNot Nothing) Then
             container.Add(Me)
         End If

     End Sub

     <System.Diagnostics.DebuggerNonUserCode()> _
     Public Sub New()
         MyBase.New()

         'This call is required by the Component Designer.
         InitializeComponent()

     End Sub
.....
end class

The compiler error is:

    error BC30269: 'Public Sub New()' has multiple definitions with
    identical signatures.

But if you create a new constructor with a different signature, it is
never called.

See what I am talking about?

-----
Author
17 May 2009 9:17 PM
Family Tree Mike
Show quote Hide quote
"Mike" <unkn***@unknown.tv> wrote in message
news:O9WwHTy1JHA.5772@TK2MSFTNGP02.phx.gbl...
>
> Thats the thing, you can't add a public sub New() of the same signature
> that are already magically added to the partial class.
>
> The MyComponent.vb has this;
>
> public class MyComponent
>     public sub new()
>     end sub
> end class
>
> The MyComponent.Designer.vb has
>
> Partial Class MyComponent
>     Inherits System.ComponentModel.Component
>
>    <System.Diagnostics.DebuggerNonUserCode()> _
>     Public Sub New(ByVal container As System.ComponentModel.IContainer)
>         MyClass.New()
>
>         'Required for Windows.Forms Class Composition Designer support
>         If (container IsNot Nothing) Then
>             container.Add(Me)
>         End If
>
>     End Sub
>
>     <System.Diagnostics.DebuggerNonUserCode()> _
>     Public Sub New()
>         MyBase.New()
>
>         'This call is required by the Component Designer.
>         InitializeComponent()
>
>     End Sub
> ....
> end class
>
> The compiler error is:
>
>    error BC30269: 'Public Sub New()' has multiple definitions with
>    identical signatures.
>
> But if you create a new constructor with a different signature, it is
> never called.
>
> See what I am talking about?
>
> -----


Interesting.  I don't think I have ever seen the "New()" put into the
..Designer.vb file.

--
Mike
Author
17 May 2009 9:40 PM
Family Tree Mike
Show quote Hide quote
"Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message
news:4610AFFC-CB70-471D-950E-1E66B07D1528@microsoft.com...
> "Mike" <unkn***@unknown.tv> wrote in message
> news:O9WwHTy1JHA.5772@TK2MSFTNGP02.phx.gbl...
>>
>> Thats the thing, you can't add a public sub New() of the same signature
>> that are already magically added to the partial class.
>>
>> The MyComponent.vb has this;
>>
>> public class MyComponent
>>     public sub new()
>>     end sub
>> end class
>>
>> The MyComponent.Designer.vb has
>>
>> Partial Class MyComponent
>>     Inherits System.ComponentModel.Component
>>
>>    <System.Diagnostics.DebuggerNonUserCode()> _
>>     Public Sub New(ByVal container As System.ComponentModel.IContainer)
>>         MyClass.New()
>>
>>         'Required for Windows.Forms Class Composition Designer support
>>         If (container IsNot Nothing) Then
>>             container.Add(Me)
>>         End If
>>
>>     End Sub
>>
>>     <System.Diagnostics.DebuggerNonUserCode()> _
>>     Public Sub New()
>>         MyBase.New()
>>
>>         'This call is required by the Component Designer.
>>         InitializeComponent()
>>
>>     End Sub
>> ....
>> end class
>>
>> The compiler error is:
>>
>>    error BC30269: 'Public Sub New()' has multiple definitions with
>>    identical signatures.
>>
>> But if you create a new constructor with a different signature, it is
>> never called.
>>
>> See what I am talking about?
>>
>> -----
>
>
> Interesting.  I don't think I have ever seen the "New()" put into the
> .Designer.vb file.
>
> --
> Mike



OK, duh!!!   I just saw it by adding the new sub when I was viewing the
..designer.vb code.  If this is what you did, then I would presume you should
edit it there.

--
Mike
Author
17 May 2009 4:58 PM
Jack Jackson
On Sun, 17 May 2009 11:40:35 -0400, Mike <unkn***@unknown.tv> wrote:

Show quoteHide quote
>Folks, I think this is a simple yes or no answer.
>
>I see .NET creates XXXXX.Designer.VB partial classes for main code
>class and component classes.
>
>My question is can I edit them?
>
>In these classes, it has this note for the last private method
>InitializeComponent():
>
>  'NOTE: The following procedure is required by the Windows Form
>         Designer
>  'It can be modified using the Windows Form Designer.
>  'Do not modify it using the code editor.
>  Private Sub InitializeComponent()
>
>But at the top of the class, for the other functions, in particular
>like New(), there is no note.
>
>Does this mean I can safely add/edit this file as long I leave the
>InitializeComponent method alone?
>
>I ask because when you create a component, I want to add more
>constructor initialization so I when add a New constructor   to the
>class the compiler does not like it because its already defined in the
>partial class.
>
>I don't want to lost work by editing a file you not suppose to.
>
>Thanks

Yes, you can modify code outside of the InitializeComponent() method.
Sometimes it is necessary, but I always worry when I do it because I
have never found any documenation about what is legal and what is not.

If I need to modify the New() or Dispose() methods, I move the entire
method to the base .vb file.
Author
17 May 2009 7:24 PM
Mike
Jack Jackson wrote:
>
> Yes, you can modify code outside of the InitializeComponent() method.
> Sometimes it is necessary, but I always worry when I do it because I
> have never found any documenation about what is legal and what is not.

Right, I would be too with things that are auto created.

> If I need to modify the New() or Dispose() methods, I move the entire
> method to the base .vb file.

Thats exactly where I am at, thinking maybe its better duplicate the
logic in the base.

Thanks for the response.

--
Author
17 May 2009 6:38 PM
Cor Ligthert[MVP]
You want a simple Yes or No answer, difficult to give because as most
important reason for this the partial class is created.

They most probably would not have done that as the answer was Yes.

Cor
Author
17 May 2009 9:36 PM
J.B. Moreno
In article <ujYO16x1JHA.4***@TK2MSFTNGP06.phx.gbl>, Cor Ligthert[MVP]
<Notmyfirstn***@planet.nl> wrote:

> You want a simple Yes or No answer, difficult to give because as most
> important reason for this the partial class is created.
>
> They most probably would not have done that as the answer was Yes.

Actually, the answer is just the opposite. A simple YES.

Given that prior to partial classes, there was no option but to mix
auto-generated and manually created text, and that they still have to
deal with such code, it's obvious that you can mix new code and auto
generated in the same file.


In fact the whole point of the comment after the InitializeComponent
call is that you can put your own code after it...

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

--
J.B. Moreno
Author
17 May 2009 11:28 PM
Mike
J.B. Moreno wrote:
Show quoteHide quote
> In article <ujYO16x1JHA.4***@TK2MSFTNGP06.phx.gbl>, Cor Ligthert[MVP]
> <Notmyfirstn***@planet.nl> wrote:
>
>> You want a simple Yes or No answer, difficult to give because as most
>> important reason for this the partial class is created.
>>
>> They most probably would not have done that as the answer was Yes.
>
> Actually, the answer is just the opposite. A simple YES.
>
> Given that prior to partial classes, there was no option but to mix
> auto-generated and manually created text, and that they still have to
> deal with such code, it's obvious that you can mix new code and auto
> generated in the same file.
>
>
> In fact the whole point of the comment after the InitializeComponent
> call is that you can put your own code after it...
>
>             'This call is required by the Windows Form Designer.
>             InitializeComponent()
>
>             'Add any initialization after the InitializeComponent() call

Thanks J.B.

It appears to me that you can cut out all but the private component
member and the InitializeComponent() function and paste the new and
dispose functions into the main component class.

So the .Designer.vb code only really needs this:

Partial Class MyComponent
   Inherits System.ComponentModel.Component

   'Required by the Component Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Component Designer
   'It can be modified using the Component Designer.
   'Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
      components = New System.ComponentModel.Container()
   End Sub

End Class

--
Author
18 May 2009 5:30 AM
J.B. Moreno
Mike <unkn***@unknown.tv> wrote:

Show quoteHide quote
> J.B. Moreno wrote:
> > Cor Ligthert[MVP] <Notmyfirstn***@planet.nl> wrote:
> >
> >> You want a simple Yes or No answer, difficult to give because as most
> >> important reason for this the partial class is created.
> >>
> >> They most probably would not have done that as the answer was Yes.
> >
> > Actually, the answer is just the opposite. A simple YES.
> >
> > Given that prior to partial classes, there was no option but to mix
> > auto-generated and manually created text, and that they still have to
> > deal with such code, it's obvious that you can mix new code and auto
> > generated in the same file.
-snip-
> Thanks J.B.
>
> It appears to me that you can cut out all but the private component
> member and the InitializeComponent() function and paste the new and
> dispose functions into the main component class.
>
> So the .Designer.vb code only really needs this:

The Designer.vb file can be eliminated entirely. I'd have to actually
try it to find out if it can both exist AND not contain any of the
auto-generated code.  But I'd be inclined to say yes, with the caveat
that if it exists it's obviously going to be the preferred place to
place control declarations and the InitializeComponent sub (and that
both are entirely recreated whenever there is a change to the form).

I have one more comment on editing the auto-gnerated code. You're
asking about editing AROUND the auto-generated code, not only can you
edit around the auto-generated code, you can edit the auto-generated
code itself, but beware when doing so: the auto-generated code is both
destination AND source.  Changes made to it WILL show up when you open
the designer and if you mess it up enough, you WON'T be able to fix it
through the designer.

--
J.B. Moreno
Author
18 May 2009 10:56 AM
Mike
J.B. Moreno wrote:

Show quoteHide quote
>> So the .Designer.vb code only really needs this:
>
> The Designer.vb file can be eliminated entirely. I'd have to actually
> try it to find out if it can both exist AND not contain any of the
> auto-generated code.  But I'd be inclined to say yes, with the caveat
> that if it exists it's obviously going to be the preferred place to
> place control declarations and the InitializeComponent sub (and that
> both are entirely recreated whenever there is a change to the form).
>
> I have one more comment on editing the auto-gnerated code. You're
> asking about editing AROUND the auto-generated code, not only can you
> edit around the auto-generated code, you can edit the auto-generated
> code itself, but beware when doing so: the auto-generated code is both
> destination AND source.  Changes made to it WILL show up when you open
> the designer and if you mess it up enough, you WON'T be able to fix it
> through the designer.

Its safer to just leave things as expected and do it programmatically
if all possible.

I want to study nested classes. Something like this offers
constructor/destructor logic to the component class:

    Public Class MyComponent
       Private nestedObject as new Constructor
        Public class Constructor
           public sub new()
           end sub
           protected overrides sub finalize()
           end sub
        End Class
    End Class

When MyComponent is instantiated, it will initialized its members.
Thus the nested Constructor class  is instantiated preparing a New()
and Finalize().   Finalize() will be called here.

Make sense?

--
Author
18 May 2009 1:06 PM
Mike
Mike wrote:

Show quoteHide quote
> I want to study nested classes. Something like this offers
> constructor/destructor logic to the component class:
>
>    Public Class MyComponent
>       Private nestedObject as new Constructor
>        Public class Constructor
>           public sub new()
>           end sub
>           protected overrides sub finalize()
>           end sub
>        End Class
>    End Class
>
> When MyComponent is instantiated, it will initialized its members. Thus
> the nested Constructor class  is instantiated preparing a New() and
> Finalize().   Finalize() will be called here.
>
> Make sense?
>
> --

Just found an entire MSDN section on the usage of nested classed for
components.

    Nested Classes in Components
    http://msdn.microsoft.com/en-us/library/cbwxw0ye(VS.71).aspx

After reading it, I think the improvement to the .Designer.vb class
that will help resolve this issue, making it less a concern whether
you can edit or not the .designer.vb code is for the generated code
template to add a empty inheritable "destructor" or Delete() that is
called by the dispose method.

That is what i did for my SDK abstract class.

  Public Class CWildcatAbstract
     Implements IDisposable

     Private _disposed As Boolean    = False

     Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
     End Sub

     Protected Overloads Overridable Sub Dispose(disposing As Boolean)
        If Not _disposed Then
           If disposing Then
              ' add releasing of managed resources
               _disposed = true
           End If
           ' add releasing of unmanaged resources
           Delete()
           _disposed = True

        End If
     End Sub

     Protected Overrides Sub Finalize()
        Dispose(False)
     End Sub

     Public Overridable Function Delete() as boolean
        return false
     end function

  End Class

So now a class that inherits this can create a Delete method that will
be called come destruction/cleanup time.

  Public Class CWildcat : inherits CWildcatAbstract

     Public Overrides Function Delete() as boolean
        ... do whatever cleanup ...
        return true
     end function
  End class

--
Author
18 May 2009 8:21 PM
Mike
Mike wrote:

Show quoteHide quote
> Just found an entire MSDN section on the usage of nested classed for
> components.
>
>    Nested Classes in Components
>    http://msdn.microsoft.com/en-us/library/cbwxw0ye(VS.71).aspx
>
> After reading it, I think the improvement to the .Designer.vb class that
> will help resolve this issue, making it less a concern whether you can
> edit or not the .designer.vb code is for the generated code template to
> add a empty inheritable "destructor" or Delete() that is called by the
> dispose method.
>
> ...
>
> So now a class that inherits this can create a Delete method that will
> be called come destruction/cleanup time.
>
>  Public Class CWildcat : inherits CWildcatAbstract
>
>     Public Overrides Function Delete() as boolean
>        ... do whatever cleanup ...
>        return true
>     end function
>  End class
>
> --

HA!

Well folks, the above is what C# does when you create a COMPONENT!

It puts the constructors in the main component class and in the
..designer.cs file it just has the stuff creating and initializing the
component.  This is how the VB.NET component creation should be as
well. Apparently, the VB.NET design team folks did not catch up with
the c# design team folks on this one.

One solution is to change the templates in:

Common7\IDE\ItemTemplatesCache\VisualBasic\1033\Component.zip\Component.Designer.vb
Common7\IDE\ItemTemplatesCache\VisualBasic\1033\Component.zip\Component.vb

or create new templates that exposes the constructor, destructors to
the main componentclass.

----