Windows Form Designer generated code

C

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?
 
L

lord.zoltar

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

RobinS

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

Martin

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
 
C

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

RobinS

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

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
 
R

RobinS

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

Linda Liu [MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

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

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

Linda Liu [MSFT]

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
 

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