1

I was wondering what this part of a c# winform does.

public home()
{
    InitializeComponent();
}
Max McCarthy
  • 50
  • 1
  • 4
  • 9
  • 2
    Does this answer your question? [What does InitializeComponent() do, and how does it work in WPF?](https://stackoverflow.com/questions/245825/what-does-initializecomponent-do-and-how-does-it-work-in-wpf) – styx Dec 17 '20 at 08:50
  • 5
    Well I'm assuming that this is in a class called `home`... that's just a constructor. – Jon Skeet Dec 17 '20 at 08:51
  • `InitializeComponent()` is created automatically by the forms designer and actually creates the components and sets their properties. Any change made in the designer is reflected in `InitializeComponent` – Panagiotis Kanavos Dec 17 '20 at 08:56
  • 1
    BTW the naming convention for classes is to capitalize the first letter, eg `class Home` instead of `class home` – Panagiotis Kanavos Dec 17 '20 at 08:57
  • 2
    What do you mean by "this part"? The constructor part (`public home() { }`) or the `InitializeComponent()` call? Read [ask], be explicit and show your research. – CodeCaster Dec 17 '20 at 08:59

1 Answers1

1

InitializeComponent is a method automatically written for you by the Form Designer when you create/change your forms.

Every Forms file (e.g. Form1.cs) has a designer file (e.g. Form1.designer.cs) that contains the InitializeComponent method, the override of the generic Form.Dispose, and the declaration of all of your User Interface objects like buttons, textboxes, labels and the Form itself.

The InitializeComponent method contains the code that creates and initializes the user interface objects dragged on the form surface with the values provided by you (the programmer) using the Property Grid of the Form Designer. Due to this fact do not ever try to interact with the form or the controls before the call to InitializeComponent. Also, you will find here, the plumbing required to link the controls and form events to the specific event handlers you have written to respond to the user actions.

The code contained in Form1.cs and the Form1.Designer.cs files is part of the same class thanks to the concept of partial classes that could keep two or more files of your code together like a single block of code.

Of course, due to the high numbers of changes executed by the Form Designer, it is a really good advice to not try to modify manually this method, while, sometime, I find useful to add code to the Dispose method with the purpose to destroy some unmanaged objects created in the form lifetime.

Abhay shah
  • 76
  • 5