Form.InitializeComponent

InitializeComponentは、.NET Frameworkのクラスでもメソッドでもないプロジェクト作成時に自動追加されるメソッドであるが、ここで基本的にコントロールの初期化をする模様。

第3回 Visual Studio .NETのひな形コードを理解する (6/6):連載簡単!Visual Studio .NET入門 - @IT

Windowsアプリケーション
Windowsアプリケーション

Windowsアプリケーションで、プロジェクト作成。

Form1.cs
Form1.cs

右クリックで、

コードの表示
コードの表示

コードの表示。

InitializeComponent
InitializeComponent

Form1のコンストラクタでInitializeComponentが呼ばれているのがわかる。

定義へ移動
定義へ移動

定義へ移動すると、

Form1.Designer.cs
Form1.Designer.cs

Form1.Designer.csにあるInitializeComponentの定義へ移動。
this.Textに"Form1"をセットしてたりする。

デザイナに戻る
デザイナに戻る

デザイナに戻って、

Loadイベントハンドラ
Loadイベントハンドラ

Loadイベントハンドラと、

Activatedイベントハンドラ
Activatedイベントハンドラ

Activatedイベントハンドラを追加。

Labelを3つ配置
Labelを3つ配置

Labelを3つ配置。

メンバ変数iを0で初期化し、Form1_Loadで1増やしてlabel2にセット、Form1_Activatedでさらに1増やしてlabel3にセット。

Form1.Designer.csのInitializeComponentの最後でiを1増やしてlabel1にセット。

label1, label2, label3の順になってる
label1, label2, label3の順になってる

iの値がlabel1, label2, label3の順になっているので、InitializeComponent、Form1_Load、Form1_Activatedの順で呼ばれているのがわかる。

Sample/dotnet/Form/InitializeComponent/src/Form_ at master · bg1bgst333/Sample · GitHub