Artur
Artur

Reputation:

Where and when is InitializeComponent called in Windows Forms control in VB.NET?

I'm doing a Windows Forms project in VB.NET, but VB.NET is completely new to me, I'm primarily a C# developer.

In C# Windows Forms, a user control's InitializeComponent is called from the form's/control's constructor. When I create same scenario in VB.NET I don't get a constructor, and I can't locate a place where InitializeComponent is called.

I need to call my code between InitializeComponent and when the control's Load event is raised, preferably still in the control's constructor. How do I do this in VB.NET?

Upvotes: 11

Views: 31269

Answers (2)

Rad
Rad

Reputation: 8379

In VB.NET the constructor is called New and has the following basic signature.

Public Sub New()
End Sub

You can of course override it and add custom parameters.

Visual Studio 2008, BTW, will remind you to put the InitializeComponent() method in the constructor in case you forget, as omitting that will lead to strange behaviors of your controls.

Upvotes: 1

Kostas Konstantinidis
Kostas Konstantinidis

Reputation: 13717

Go to View Code in your form, and from the right drop down and select "New Method".

There you can see where InitializeComponent is called and insert your logic.

Your code, if your form is empty, should look like this:

Public Class Form1

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

        ' Add any initialization after the InitializeComponent() call.
    End Sub

End Class

Upvotes: 11

Related Questions