Can you edit auto generated Partial Class Designer VB code?

M

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

--
 
F

Family Tree Mike

Mike said:
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
 
F

Family Tree Mike

Mike said:
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
 
J

Jack Jackson

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.
 
J

Jack Jackson

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.
 
C

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
 
C

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
 
M

Mike

Family said:
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?

-----
 
M

Mike

Family said:
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?

-----
 
M

Mike

Jack said:
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.

--
 
M

Mike

Jack said:
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.

--
 
F

Family Tree Mike

Mike said:
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.
 
F

Family Tree Mike

Mike said:
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.
 
J

J.B. Moreno

Cor Ligthert said:
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

J.B. Moreno

Cor Ligthert said:
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
 
F

Family Tree Mike

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



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.
 
F

Family Tree Mike

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



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.
 
M

Mike

J.B. Moreno said:
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

--
 
M

Mike

J.B. Moreno said:
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

--
 
J

J.B. Moreno

Mike said:
-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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top