Windows Form Designer Generated Code

pa3329

Member
Joined
Aug 30, 2006
Messages
9
Programming Experience
3-5
I am stepping through a tutorial and it says to add code under the InitializeComponent() line in the Windows Form Designer Generated Code section of the code. I have never used .NET 2005 (only .NET 2003) so I am not sure if it because of the version but I don't have that block on code in my Windows Form. Does anybody have any ideas why or how I get it?
Thanks in advanced!
 
The generated code in VB2005 is in the partial class attached to the form. Show all files in Solution Explorer to see the Form.Designer.vb file, if you click '+' at the form it is linked just below it in the tree. See screenshot.
 

Attachments

  • partialclass.jpg
    partialclass.jpg
    11.3 KB · Views: 55
You shouldn't need to enter the designer generated code for that. In VB 2005 a form has a default constructor. If you want to create your own constructor you simply do so and the default will be removed. You should open the regular code file for the form, type "public sub new" and press Enter. This will create a constructor stub that includes the InitializeComponent call and looks like this:
VB.NET:
    Public Sub New()

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

        ' Add any initialization after the InitializeComponent() call.

    End Sub
I repeat: do NOT edit any constructors in the designer-generated code.
 
It was something that I wanted run every time the form started and I remembered from 2003 that putting it in Form_Load() did not always work but if you put it under InitializeComponent() it would always work. I'll put it in Form_Load() and it is working but I'll try your tip about creating a new constructor. Thanks!
 
The constructor is executed when the form is created. The Load event handler is executed just before the form becomes visible for the first time. It's possible to create a form and it never become visible, but any form that does become visible will raise the Load event.
 
Back
Top