Where put initialization code in 2005

In VB 2003, when creating a winform, there was always a section that read like this:

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

In VB 2005 there is no such concept, so where do you put initialization code in VB 2005



Answer this question

Where put initialization code in 2005

  • gr8Kiwi

    Jim

    I couldnt agree with you more about putting you user code in the Form1.vb file and not the Form1.Designer.vb file.

    Sure you can modifiy the system generated code but this I would say was something you do with caution as soon as you start making chnages here - its sometimes difficult to trace problems because its just another partial class file to start searching through when debugging and youve got different behaviour from what would be expected in this file by a system generated process.

    This separation of files was made possible by partial classes and was really to allow the system generated files to be hidden rather than the VB 2003 idea of just having a region in same file that said - dont touch system generated or something like that.

    Touching the designer.vb file whilst easy is best left for very specific reasons by more seasoned developers. Putting you user initialization in a new constructor is the generally accepted place to put you class initialization code.


  • henk van noort

    2005 does include the concept of the initialization code. They have just moved it to a partial class file called MyForm.Designer.vb. The default implementation which calls InitializeComponent is included in the base class' constructor implementation by default.

    Since it is a partial form, you are welcome to put your own custom constructor either in the .Designer.vb file or directly in the view code portion of your form (MyForm.vb). Either way, this version would override the base class' implementation, thus your New should call InitializeComponent() and then any custom processing you need. I would recommend putting your custom New constructor in the standard code behind, not the .Designer.vb file to keep the generated information separate from your custom logic as a policy.

    http://devauthority.com/blogs/jwooley/default.aspx



  • cgreen1

    I'm sorry but based on the way you explained it I still do not know what to do.

    In 2003 it was easy to know where to place your code. Essentially the the code viewer said "Place your code here". In 2005, even after your explanation, I don't what to do.

    My understanding is that if I place any code in the myForm.Designer.vb, as soon as you add a object to the form, whatver your put in the contructor will be gone.

    Somebody else said they usually put their code in the "new event", but I see no new event. I don't mean to be dense but I still don't know where to place my code.


  • MaxOmilian

     WoodrowS wrote:

    In VB 2005 there is no such concept, so where do you put initialization code in VB 2005


    No, there is, in the Form.Designer.vb there is a sub-routine called "InitializeComponent()".

  • Alan Zhong

    Ok, try this: Create a new form. Go to the code view. Type the following:

    Public Sub New()

    Hit enter. You will see VB 2005 fill out the class as follows:

    Public Sub New()

    ' This call is required by the Windows Form Designer.

    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    End Sub

    Now, you have your custom constructor which calls InitializeComponent() properly and allows you to do your own custom initialization code. Note, your constructor should be in the Form1.vb file not Form1.Designer.vb, although it could just as easily be placed in the .Designer file. Where to place it is more a matter of design/architecture. I argue in favor of the form.vb rather than form.designer.vb file as I want my custom code to be in one place and leave the system generated code in a separate location. This doesn't mean that I don't touch the code in the .designer.vb file, I do that all the time. (then again, I have been known to alter VB6 .frm files with notpad.) I wouldn't recommend my junior programmers do that. YMMV

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx



  • tarne

    Do not put it in the MyForm.Designer.Vb as this form is regenerated when you make changes to the form.

    As VB2005 has partial classes - the contents of the designer.vb and the .vb file are partial classes on the same class. The designer.vb is really not intended for user code.

    If you want to create a constructor for an object put a sub called new in the user class Myform.vb . This will be run when you create a new instance of the form. You can create overloaded constructors for this new method as well.

    You can also put some code in one of the predefined event procedures such as form load for forms but these dont exist for other types of classes and a form is just a class.


  • John Q. Smith

    I usually use the new event.

  • Where put initialization code in 2005