C#:Winフォーム:テキストボックス
参考
主なイベント
KeyPress
入力制限
MouseUp
全選択
SelectionChanged
Tips
キャレット
//************************************************* /// <summary> /// キャレット情報 /// </summary> /// <param name="txt"></param> //************************************************* private void mGet_CaretInfo(TextBox txt) { Console.WriteLine(txt.SelectionStart); // 開始位置 Console.WriteLine(txt.SelectionLength); // 選択数 Console.WriteLine(txt.SelectedText); // 選択文字列 }
文字入力制限
TextBoxに数字しか入力できないようにする - .NET Tips (VB.NET,C#...)"
テキストボックスに入力できる文字数(最大桁数)を制限する [C#] TextBox | JOHOBASE"
テキストボックスで文字の入力を制限する - テキストボックスで数値のみ入力を受け付ける - C#プログラミング"
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // 指定のみイベントを発行 if ((e.KeyChar <'0' || '9' < e.KeyChar ) && (e.KeyChar < 'A' || 'F' < e.KeyChar) && (e.KeyChar < 'a' || 'f' < e.KeyChar) && e.KeyChar != '\b') { e.Handled = true; } }
ASCIIコード指定
//****************************************************** /// <summary> /// /// </summary> /// <param name="sender"></param> //****************************************************** /// <param name="e"></param> private void txt01_KeyPress(object sender, KeyPressEventArgs e) { // 指定のみイベントを発行 if (((int)e.KeyChar < Convert.ToInt32("20",16) || Convert.ToInt32("60", 16) < (int)e.KeyChar) && ((int)e.KeyChar < Convert.ToInt32("7B", 16) || Convert.ToInt32("7E", 16) < (int)e.KeyChar)) { e.Handled = true; } }
全選択
private void textBox1_MouseUp(object sender, MouseEventArgs e) { ((TextBox)sender).SelectAll(); }
Undo
((TextBox)sender).Undo();
フォーカスが外れたとき
private void textBox1_Leave(object sender, EventArgs e) { }