TextBox と Button を貼り付ける

Windows モードから幾つかのプログラムを抜粋して CLI で動かします。
TextBox と Button を貼り付ける を CLI で動かしてみましょう。

CLI で動かす

  1. C:\Data\C#\BAT\win\ のフォルダーに TextButton.cs の名前で utf-8(BOM 有り)でタイプして格納して下さい。
    /*★ ボタンのクリックで TEXT を取得して表示する    前田 稔 ★*/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    // ツールを貼り付けるとメソッドが追加される
    public class MyForm : Form
    {
        private Button button1;
        private TextBox textBox1;
        private string str = "C#";
    
        public MyForm()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(MyHandler);
        }
    
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(83, 205);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 19);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(94, 231);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "MyForm";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font f = new Font("MS 明朝", 40);
            g.DrawString(str, f, Brushes.Blue, new PointF(10F, 80F));
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            str = textBox1.Text;
            Invalidate();
            MessageBox.Show(str,"Button Click");
        }
    }
    
    class form03
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. スタートメニューから[すべてのプログラム][Visual C++ 2005 Express Edition][Visual Studio Tools] [Visual Studio 2005 コマンド プロンプト] から起動します。
    C:\Data\C#\BAT\win のフォルダーに移動して、直接 csc コマンドを叩きます。
    TextButton.exe を実行してボタンをクリックすると、TextBox の Text が MessageBox に表示されます。
    >CD C:\Data\C#\BAT\win
    >CSC TextButton.cs
    >TextButton.exe          
    
  3. ボタンをクリックすると、button1_Click() が呼ばれます。
    TextBox の Text を取得して Invalidate() でウインドウに印字します。
    次に MessageBox にも Text を表示します。
        private void button1_Click(object sender, EventArgs e)
        {
            str = textBox1.Text;
            Invalidate();
            MessageBox.Show(str,"Button Click");
        }
    

超初心者のプログラム入門(C#)