非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

C#: テキストエディタ上で開発するためのプログラムテンプレート

      2016/06/11

[ad#top-1]
 

テキストエディタでプログラミングする場合、最初からゴリゴリ書くわけではなく、ある程度自分でテンプレートを用意しておいてコピペを繰り返したりします。ホントの最初の入口は、です。

今回は基本的なテンプレートを紹介します。

using System;
using System.Drawing;
using System.Windows.Forms;
class MainWindow{
   [STAThread]
   static void Main(){
      Application.Run(new myForm());
   }
}
class myForm : Form{
   public myForm(){
      this.StartPosition = FormStartPosition.CenterScreen;
      //this.ShowInTaskbar = false;   //タスクバーに配置しない
      this.Width = 400;
      this.Height = 250;
      this.Text = “Title”;
      this.setComponents();
   }
   //ボタンを押したときのイベント
   private void button1_Click(object sender, EventArgs e){
      //Application.Exit();   //アプリの終了
      this.Close();         //フォーム閉じる。メインフォームならアプリ終了
   }
   //テキストボックスでキーが押されたとき
   private void keyDowned(object sender, KeyEventArgs e){
      //Ctrl+Zキー
      if(e.KeyCode == Keys.Z && e.Control){}
      //Enterキー
      if(e.KeyCode == Keys.Enter){}
      //Tabキー
      if(e.KeyCode == Keys.Tab){}
   }
   private void setComponents(){
      Font font = new Font(“Meiryo UI”, 10);
      new Label(){
         Font = font,
         Location = new Point(50, 50),
         Text = “ラベル”,
         AutoSize = true,
         Parent = this,
      };
      TextBox textBox1 = new TextBox(){
         Font = font,
         Location = new Point(150, 50),
         Size = new Size(170, 30),
         //PasswordChar = ‘*’,   //パスワード入力にする場合
         //Multiline = true,      //多数行タイプ
         //BorderStyle = BorderStyle.FixedSingle,  //シングル枠
         //Dock = DockStyle.Fill,   //画面目一杯に配置
         Parent = this,
      };
      textBox1.KeyDown += new KeyEventHandler(keyDowned);
      Button button1 = new Button(){
         Font = font,
         Text = “ボタン”,
         Size = new Size(100, 30),
         Location = new Point(50, 100),
         Parent = this,
      };
      button1.Click += new EventHandler(this.button1_Click);
   }
}

 

基本的なところですが、一番使用頻度が高いラベルとテキストボックスとボタンを配置したものです。

これをDOSプロンプトで、

C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /platform:x86 /target:winexe /out:Test.exe *.cs

ってやれば、Test.exeが生成され、実行すると↓こうなります。

g


 
[ad#ad-1]

スポンサーリンク

 - C#応用編