Often I am asked how to do something in code that is done using VS.NET designer. And answer is actually very simply since in most cases everything that you do using VS.NET designer, ends up being serialized into code. InitializeComponent() method in Visual Studio.NET C# or VB.NET is method that is automatically created and managed by Windows Forms designer and it defines everything you see on the form. Everything done on the form in VS.NET using designers generates code. Every single control added and property set will generate code and that code goes into InitializeComponent() method. So when you create anything using VS.NET designer, it will end up in this method. This means you can use this to your advantage to learn how to create any UI element using code.

When you run the app (or open the form in VS.NET), code from InitializeComponent() method creates and configures controls just as you did in the designer. So every single change you make gets reflected in InitializeComponent() method. This means that doing things at design-time and run-time is essentially the same. Note that you should not modify this method manually since it might confuse the VS.NET designer. However you should use it to learn how to do things from code and how to setup controls and components correctly.

In C# access to this method is always visible through form constructor. Simply position the cursor in InitializeComponent method call and press F12 in VS.NET and Form .designer file will open to show the implementation of the InitializeComponent method.

In VB.NET the Visual Studio hides the access to InitializeComponent method. Actual method implementation is in Form.designer.vb file for that form. File is accessible via Solution Explorer once you have clicked on the Show All Files toolbar button which you can find on top of the Solution Explorer window. Following video shows how to do this:

So if you want to find out how to create controls or setup things from code you can easily discover how to do that by doing what you want to do from code in VS.NET designer, say adding controls to the form and changing their properties, then you go to InitializeComponent() method and look through code that is generated and which shows how to create everything you see on the form. The easiest is to start with new form and simply add the controls to it and change their properties then go to InitializeComponent method to see how its done from code.

Professional looking applications made easy with DotNetBar for WinForms, Silverlight and WPF User Interface components. Click here to find out more.