C# 入門 & 実践 / C sharp

5-1. HelloWorld - Windowsアプリケーション -


お決まりのHelloWorldを書いてみましょう!

新規プロジェクトの作成から「Windowsアプリケーション」を選択し、好きな名前を付けます。



こんな画面が出てきます。フォームが一つありますね!



ツールボックスが左上に隠れているので出しましょう!



「Label」を選択し、Form1へ貼り付けます。
ラベルのプロパティを変更してHelloWorldと書いてみます。



「デバッグ」→「デバッグ開始」!



やりました!!!
とあまり達成感はありませんね^-^;

このときのソースはどうなっているのでしょうか?
Program.cs と Form1.cs があります。
namespace MyApplication
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.csを右クリックしてソース表示を選択すると以下のようなものが出てきます。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Form1.Designers.csというのを開いてみると

namespace MyApplication
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナ変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナで生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Century Gothic", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.label1.Location = new System.Drawing.Point(84, 68);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(110, 19);
            this.label1.TabIndex = 0;
            this.label1.Text = "Hello World";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
    }
}


partial : これは、2つのファイルにクラスを分けて書くときの宣言のようですね!

全部開いてみるとこんな感じです。Form1が呼ばれるとLabelを生成して自分も初期化といった感じですね。
この辺は手で書くことはとりあえず無いでしょう^-^;



5. Windows アプリケーション » 5-2. HelloWorld - Button -


C# 入門 & 実践 / C sharp