WisdomSoft - for your serial experiences.

6.3 テキスト入力

ユーザーが入力した任意のテキストを受け取るにはテキストボックスを使います。

6.3.1 テキストボックス

テキストボックスは、ユーザーが任意の文字列を入力することができるコントロールです。テキストボックスを表示するには System.Windows.Controls.TextBox クラスを利用します。

System.Windows.Controls.TextBox クラス
System.Object 
   System.Windows.Threading.DispatcherObject 
     System.Windows.DependencyObject 
       System.Windows.Media.Visual 
         System.Windows.UIElement 
           System.Windows.FrameworkElement 
             System.Windows.Controls.Control 
               System.Windows.Controls.Primitives.TextBoxBase 
                System.Windows.Controls.TextBox
[LocalizabilityAttribute(LocalizationCategory.Text)] 
[ContentPropertyAttribute("Text")] 
public class TextBox : TextBoxBase, IAddChild

TextBox の基本的な設計はボタンと似ています。テキストを入力するコントロールを抽象化する TextBoxBase 抽象クラスが存在し、TextBox はこの抽象クラスを実装しています。TextBox のコンストラクタは、パラメータを受け取りません。

TextBox クラスのコンストラクタ
public TextBox ()

TextBox オブジェクトは、何かを表示する目的ではなく、ユーザーにテキストを入力してもらうことが目的なので UIElement を設定することはできません。このクラスは ContentControl を継承していないことに注目してください。テキストボックスが管理するテキストは、Text プロパティから設定・取得することができます。

TextBox クラス Text プロパティ
[LocalizabilityAttribute(LocalizationCategory.Text)] 
public string Text { get; set; }

Text プロパティに文字列を設定すれば、その文字列がテキストボックス上のテキストとして表示されます。また、ユーザーが入力したテキストも Text プロパティに反映されます。

コード1
using System;
using System.Windows;
using System.Windows.Controls;

class Test {
	[STAThread]
	public static void Main() {
		TextBox textBox = new TextBox();
		textBox.Text = "入力してください";
		textBox.Margin = new Thickness(10);

		StackPanel panel = new StackPanel();
		panel.Children.Add(textBox);

		Window wnd = new Window();
		wnd.Content = panel;

		Application app = new Application();
		app.Run(wnd);
	}
}
実行結果
コード1 実行結果

コード1は、「入力してください」というテキストを設定した状態で表示したテキストボックスです。プロパティに設定した文字列が、そのままテキストボックス上のテキストとして表示されることが確認できます。Text プロパティは、テキストボックス上のテキストと完全に同期しているため、ユーザーが書き換えれば Text プロパティの文字列も変更されます。

既定の状態では、テキストボックスには 1 行のテキストしか入力できません。長文の入力を要求する場合は AcceptsReturn プロパティを true に設定してください。

TextBoxBase クラス AcceptsReturn プロパティ
public bool AcceptsReturn { get; set; }

このプロパティが true の場合、テキスト入力コントロールが複数行をサポートすることを表します。既定では false が設定されているため、1 行しか入力できません。

コード2
using System;
using System.Windows;
using System.Windows.Controls;

class Test {
	[STAThread]
	public static void Main() {
		TextBox textBox = new TextBox();
		textBox.Text = "入力してください";
		textBox.AcceptsReturn = true;
		textBox.Height = 200;
		textBox.Margin = new Thickness(10);

		StackPanel panel = new StackPanel();
		panel.Children.Add(textBox);

		Window wnd = new Window();
		wnd.Content = panel;

		Application app = new Application();
		app.Run(wnd);
	}
}
実行結果
コード2 実行結果

コード2は、AcceptsReturn を true に設定し、複数行の入力を許可したテキストボックスを表示します。

一般的なフォーム処理では、TextBox クラスはユーザーが入力したテキストを保存する目的にのみ使われ、Text プロパティの値をプログラム側で読み取って処理するのは「OK」ボタンなど、フォーム全体入力が終了したことを表すアクションが発生してからとなります。しかし、ユーザーが入力するテキストをリアルタイムで監視したいという要求もあるでしょう。そのような場合は TextChanged イベントを使います。

TextBoxBase クラス TextChanged イベント
public event TextChangedEventHandler TextChanged

TextChanged イベントは、テキストボックスの Text プロパティの値が変更されると発生します。このイベントには System.Windows.Controls.TextChangedEventHandler デリゲート型のオブジェクトを設定します。

System.Windows.Controls.TextChangedEventHandler デリゲート
public delegate void TextChangedEventHandler (
    Object sender,
    TextChangedEventArgs e
)

このデリゲートの e パラメータは、テキスト変更イベントの情報を提供する System.Windows.Controls.TextChangedEventArgs クラス型のオブジェクトを受け取ります。

System.Windows.Controls.TextChangedEventArgs クラス
System.Object 
   System.EventArgs 
     System.Windows.RoutedEventArgs 
      System.Windows.Controls.TextChangedEventArgs
public class TextChangedEventArgs : RoutedEventArgs

TextChanged イベントは、ユーザーがキーボードから新しい文字を打ち込む度に発生します。

コード3
using System;
using System.Windows;
using System.Windows.Controls;

class Test : Window {
	[STAThread]
	public static void Main() {
		Window wnd = new Test();
		Application app = new Application();
		app.Run(wnd);
	}

	private Label label;
	private TextBox textBox;

	public Test() {
		label = new Label();
		label.Content = "テキストを入力してください";

		textBox = new TextBox();
		textBox.AcceptsReturn = true;
		textBox.Margin = new Thickness(10);
		textBox.Height = 200;
		textBox.TextChanged += textBoxTextChanged;

		StackPanel panel = new StackPanel();
		panel.Children.Add(label);
		panel.Children.Add(textBox);

		Content = panel;
	}

	private void textBoxTextChanged(Object sender, TextChangedEventArgs e) {
		label.Content = textBox.Text;
	}
}
実行結果
コード3 実行結果

コード3は、テキストボックスと上部のラベルが同期するプログラムです。テキストボックス内に適当な文字を入力すると、同時に上部のラベルのテキストも変更されます。これは、TextChanged イベントによって textBoxTextChanged() メソッドが呼び出されているためです。textBoxTextChanged() メソッド内では、テキストボックスの Text プロパティをラベルのコンテンツに設定しています。