テキストボックスの内容をメッセージウィンドウに表示するサンプルです。
-------------------------------------------------------
using System;
using System.Windows.Forms;
class WinSample : Form
{
private Button mybutton;
private TextBox mytextbox;
public static void Main()
{
Application.Run(new WinSample());
}
public WinSample()
{
this.mybutton = new Button();
this.mytextbox = new TextBox();
this.SuspendLayout();
this.Text = "Hello World!";
// Enterキーにボタンを関連づける
this.AcceptButton = this.mybutton;
this.mybutton.Location = new System.Drawing.Point(200, 200);
this.mybutton.Text = "入力確認";
this.mybutton.Click += new EventHandler(this.mb_Click);
// テキストボックスの表示位置設定
this.mytextbox.Location = new System.Drawing.Point(100, 50);
// テキストボックスとボタンをフォームのコントロールに追加
this.Controls.AddRange(new Control[] { this.mytextbox, this.mybutton});
this.ResumeLayout(false);
}
private void mb_Click(object sender, EventArgs e)
{
// テキストボックスの内容をメッセージボックスに表示
MessageBox.Show(this.mytextbox.Text);
}
}
-------------------------------------------------------
テキストボックスに入力した後でEnterキーを押すと、入力確認ボタンを押したときと同じように動作しますが、これはAcceptButtonプロパティにボタンを設定しているためです。
また、複数のコントロールを追加するのにAddRangeメソッドを利用しています。
あおい情報システム株式会社 小野修司(どっとねっとふぁん)