469,575 Members | 1,631 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Windows Form Designer generated code

cj
In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated code
area as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many
parts of the old 2003 program but I'm not sure what to do with this
code. I know 2005 keeps the Windows Form Designer generated code
somewhere but I really don't want to add it there unless it is really
necessary. Apparently it can go in Form1_Load but I'm not sure that's
appropriate. Where should I put it in 2005? Pros and cons?
Jan 12 '07 #1
13 5025
In Visual Studio 2005, the designer generated code usually goes in a
seperat file. If you have a form named "MyForm", you'll get 3 files:
MyForm.vb, MyForm.Designer.vb, MyForm.resx. The code generated by the
designer goes in MyForm.Designer.vb. double-click on one and have a
look to see if you need to add it there.

cj wrote:
In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated code
area as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many
parts of the old 2003 program but I'm not sure what to do with this
code. I know 2005 keeps the Windows Form Designer generated code
somewhere but I really don't want to add it there unless it is really
necessary. Apparently it can go in Form1_Load but I'm not sure that's
appropriate. Where should I put it in 2005? Pros and cons?
Jan 12 '07 #2
The windows form designer-generated code is now in a separate file.
If you click on "Show All Files" in the Solution Explorer, you
will see myForm.Designer.vb.

Unless your code is throwaway code, you should never change that
file, because it is regenerated whenever you change the form.

I recommend putting the code in the Form_Load routine.

Robin S.
---------------------------------------------

"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated code
area as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many
parts of the old 2003 program but I'm not sure what to do with this
code. I know 2005 keeps the Windows Form Designer generated code
somewhere but I really don't want to add it there unless it is really
necessary. Apparently it can go in Form1_Load but I'm not sure that's
appropriate. Where should I put it in 2005? Pros and cons?

Jan 12 '07 #3
Hi CJ,

in 2005 if you type 'Public Sub New' in the .vb file and press return the
editor will auto-generate the code:-

Public Sub New()

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

' Add any initialization after the InitializeComponent() call.

End Sub

You can add your code after the 'Add any....' comment

HTH


"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated code area
as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many parts
of the old 2003 program but I'm not sure what to do with this code. I
know 2005 keeps the Windows Form Designer generated code somewhere but I
really don't want to add it there unless it is really necessary.
Apparently it can go in Form1_Load but I'm not sure that's appropriate.
Where should I put it in 2005? Pros and cons?

Jan 12 '07 #4
cj
in which .vb file. form1.vb or form1.designer.vb?

Martin wrote:
Hi CJ,

in 2005 if you type 'Public Sub New' in the .vb file and press return the
editor will auto-generate the code:-

Public Sub New()

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

' Add any initialization after the InitializeComponent() call.

End Sub

You can add your code after the 'Add any....' comment

HTH


"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
>In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated code area
as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many parts
of the old 2003 program but I'm not sure what to do with this code. I
know 2005 keeps the Windows Form Designer generated code somewhere but I
really don't want to add it there unless it is really necessary.
Apparently it can go in Form1_Load but I'm not sure that's appropriate.
Where should I put it in 2005? Pros and cons?

Jan 12 '07 #5
cj
In my testing it didn't erase code I put in form1.designer.vb when I
changed the form. However I understand that MS changed 2005 to separate
that code because we aren't supposed to need to change it.
RobinS wrote:
The windows form designer-generated code is now in a separate file.
If you click on "Show All Files" in the Solution Explorer, you
will see myForm.Designer.vb.

Unless your code is throwaway code, you should never change that
file, because it is regenerated whenever you change the form.

I recommend putting the code in the Form_Load routine.

Robin S.
---------------------------------------------

"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
>In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated code
area as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many
parts of the old 2003 program but I'm not sure what to do with this
code. I know 2005 keeps the Windows Form Designer generated code
somewhere but I really don't want to add it there unless it is really
necessary. Apparently it can go in Form1_Load but I'm not sure that's
appropriate. Where should I put it in 2005? Pros and cons?

Jan 12 '07 #6
form1.vb. I had a temporary brain meltdown and wasn't thinking,
but he's right, you can add your own constructor to your form.
It will put in the InitializeComponent for you (if it doesn't,
just add it), and then put in the rest of your code.

Robin S.
----------------------
"cj" <cj@nospam.nospamwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
in which .vb file. form1.vb or form1.designer.vb?

Martin wrote:
>Hi CJ,

in 2005 if you type 'Public Sub New' in the .vb file and press return
the editor will auto-generate the code:-

Public Sub New()

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

' Add any initialization after the InitializeComponent() call.

End Sub

You can add your code after the 'Add any....' comment

HTH


"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
>>In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated
code area as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize
control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many
parts of the old 2003 program but I'm not sure what to do with this
code. I know 2005 keeps the Windows Form Designer generated code
somewhere but I really don't want to add it there unless it is
really necessary. Apparently it can go in Form1_Load but I'm not
sure that's appropriate. Where should I put it in 2005? Pros and
cons?
Jan 12 '07 #7
cj
I probably should know but why would it matter if I had my code in
Form1_Load instead of Public Sub New?

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

RobinS wrote:
form1.vb. I had a temporary brain meltdown and wasn't thinking,
but he's right, you can add your own constructor to your form.
It will put in the InitializeComponent for you (if it doesn't,
just add it), and then put in the rest of your code.

Robin S.
----------------------
"cj" <cj@nospam.nospamwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
>in which .vb file. form1.vb or form1.designer.vb?

Martin wrote:
>>Hi CJ,

in 2005 if you type 'Public Sub New' in the .vb file and press return
the editor will auto-generate the code:-

Public Sub New()

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

' Add any initialization after the InitializeComponent() call.

End Sub

You can add your code after the 'Add any....' comment

HTH


"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated
code area as shown below.

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize
control")
End If

End Sub

I'm completing a 2005 program now that was so similar I copied many
parts of the old 2003 program but I'm not sure what to do with this
code. I know 2005 keeps the Windows Form Designer generated code
somewhere but I really don't want to add it there unless it is
really necessary. Apparently it can go in Form1_Load but I'm not
sure that's appropriate. Where should I put it in 2005? Pros and
cons?
Jan 12 '07 #8
One runs when the form is being instantiated, the other runs
*after* the form is instantiated and the controls are loaded.
So if this code below creates a new control, I'd put it in
the constructor (Sub New). If it can happen afterwards, I'd
put it in Form_Load.

This seems to be initializing some kind of control, so I'd
put it in the constructor.

Try it in each place, and see if it makes a difference.
No pain, no gain. ;-) (Or as I like to think of it,
No brains, no headaches.)

Robin S.
-------------------------------
"cj" <cj@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>I probably should know but why would it matter if I had my code in
Form1_Load instead of Public Sub New?

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

RobinS wrote:
>form1.vb. I had a temporary brain meltdown and wasn't thinking,
but he's right, you can add your own constructor to your form.
It will put in the InitializeComponent for you (if it doesn't,
just add it), and then put in the rest of your code.

Robin S.
----------------------
"cj" <cj@nospam.nospamwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
>>in which .vb file. form1.vb or form1.designer.vb?

Martin wrote:
Hi CJ,

in 2005 if you type 'Public Sub New' in the .vb file and press
return the editor will auto-generate the code:-

Public Sub New()

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

' Add any initialization after the InitializeComponent() call.

End Sub

You can add your code after the 'Add any....' comment

HTH


"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl...
In a project done in 2003 about a year ago I was told to add the
SocketWrench code below into the Windows Form Designer generated
code area as shown below.
>
#Region " Windows Form Designer generated code "
>
Public Sub New()
MyBase.New()
>
'This call is required by the Windows Form Designer.
InitializeComponent()
>
'Add any initialization after the InitializeComponent()
call
>
socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize
control")
End If
>
End Sub
>
I'm completing a 2005 program now that was so similar I copied
many parts of the old 2003 program but I'm not sure what to do
with this code. I know 2005 keeps the Windows Form Designer
generated code somewhere but I really don't want to add it there
unless it is really necessary. Apparently it can go in Form1_Load
but I'm not sure that's appropriate. Where should I put it in
2005? Pros and cons?

Jan 12 '07 #9
Hi Cj,

In VS.NET 2003, the part of 'Windows Form Designer generated code' contains
a constructor and the InitializeComponent method, and these code is located
in the form.vb file.

In VS 2005, there're some differences. Firstly, the part of 'Window Form
Designer generated code' does not contain a constructor. Secondly, these
code are moved to a separate file -- form.designer.vb, in order to make the
code of the form look neat.

As Martin has suggested, we could add a constructor in the form.vb file by
ourselves.

Constructor is called before the Load event handler of the form. You could
put your initialization code either in the constructor or the Load event
handler of the form. It depends on what kind of the initialization code is.

If you'd like to create an object that has a precedence relation to other
objects in the form or do some crucial checking, I suggest that you place
the initialization code in the constructor. Otherwise, you could place the
code in the constructor or the Load event handler.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jan 15 '07 #10
cj
Hummm, I guess the Public Sub New is while the form is being instigated
and Form1_load is after? It work in Form1_load. It is a 3rd party com
object that raises and event when a TCP/IP connection is attempted to
the machine. I'd say I want it started only after the program is up and
running so I guess form1_load is ok but it probably doesn't kill
anything to be in Sub New.

RobinS wrote:
One runs when the form is being instantiated, the other runs
*after* the form is instantiated and the controls are loaded.
So if this code below creates a new control, I'd put it in
the constructor (Sub New). If it can happen afterwards, I'd
put it in Form_Load.

This seems to be initializing some kind of control, so I'd
put it in the constructor.

Try it in each place, and see if it makes a difference.
No pain, no gain. ;-) (Or as I like to think of it,
No brains, no headaches.)

Robin S.
-------------------------------
"cj" <cj@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>I probably should know but why would it matter if I had my code in
Form1_Load instead of Public Sub New?

socket = New SocketWrenchCtl.SocketWrench
If socket.Initialize("ELMO") <0 Then
Throw New System.Exception("Unable to initialize control")
End If

RobinS wrote:
>>form1.vb. I had a temporary brain meltdown and wasn't thinking,
but he's right, you can add your own constructor to your form.
It will put in the InitializeComponent for you (if it doesn't,
just add it), and then put in the rest of your code.

Robin S.
----------------------
"cj" <cj@nospam.nospamwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
in which .vb file. form1.vb or form1.designer.vb?

Martin wrote:
Hi CJ,
>
in 2005 if you type 'Public Sub New' in the .vb file and press
return the editor will auto-generate the code:-
>
Public Sub New()
>
' This call is required by the Windows Form Designer.
InitializeComponent()
>
' Add any initialization after the InitializeComponent() call.
>
End Sub
>
You can add your code after the 'Add any....' comment
>
HTH
>
>
>
>
"cj" <cj@nospam.nospamwrote in message
news:u5**************@TK2MSFTNGP03.phx.gbl.. .
>In a project done in 2003 about a year ago I was told to add the
>SocketWrench code below into the Windows Form Designer generated
>code area as shown below.
>>
>#Region " Windows Form Designer generated code "
>>
> Public Sub New()
> MyBase.New()
>>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>>
> 'Add any initialization after the InitializeComponent()
>call
>>
> socket = New SocketWrenchCtl.SocketWrench
> If socket.Initialize("ELMO") <0 Then
> Throw New System.Exception("Unable to initialize
>control")
> End If
>>
> End Sub
>>
>I'm completing a 2005 program now that was so similar I copied
>many parts of the old 2003 program but I'm not sure what to do
>with this code. I know 2005 keeps the Windows Form Designer
>generated code somewhere but I really don't want to add it there
>unless it is really necessary. Apparently it can go in Form1_Load
>but I'm not sure that's appropriate. Where should I put it in
>2005? Pros and cons?

Jan 15 '07 #11
cj
It's a TCP/IP server program and the control is a 3rd party com object
that raises and event when a TCP/IP connection is attempted to the
machine. I'd say I want it started only after the program is up and
running so I guess form1_load is ok.

Linda Liu [MSFT] wrote:
Hi Cj,

In VS.NET 2003, the part of 'Windows Form Designer generated code' contains
a constructor and the InitializeComponent method, and these code is located
in the form.vb file.

In VS 2005, there're some differences. Firstly, the part of 'Window Form
Designer generated code' does not contain a constructor. Secondly, these
code are moved to a separate file -- form.designer.vb, in order to make the
code of the form look neat.

As Martin has suggested, we could add a constructor in the form.vb file by
ourselves.

Constructor is called before the Load event handler of the form. You could
put your initialization code either in the constructor or the Load event
handler of the form. It depends on what kind of the initialization code is.

If you'd like to create an object that has a precedence relation to other
objects in the form or do some crucial checking, I suggest that you place
the initialization code in the constructor. Otherwise, you could place the
code in the constructor or the Load event handler.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Jan 15 '07 #12
Hi Cj,

Thank you for your response.

Since it's a TCP/IP server program and you'd like the 3rd party COM object
to start only after the program is up and running, I think it's better to
place the initialization code in the form1_load.
Sincerely,
Linda Liu
Microsoft Online Community Support

Jan 16 '07 #13
cj
Thanks!

Linda Liu [MSFT] wrote:
Hi Cj,

Thank you for your response.

Since it's a TCP/IP server program and you'd like the 3rd party COM object
to start only after the program is up and running, I think it's better to
place the initialization code in the form1_load.
Sincerely,
Linda Liu
Microsoft Online Community Support
Jan 16 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ayende Rahien | last post: by
1 post views Thread by Scott Davies | last post: by
reply views Thread by Scott Davies | last post: by
4 posts views Thread by Rod Gill | last post: by
5 posts views Thread by Fredrik Melin | last post: by
8 posts views Thread by TomC | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.