0

TextBox にデータを入力する機会を与えないイベントがあります。テキストボックスにデータを入力しようとすると、テキストボックスはそれを実行できません:

private void Login_textbox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(textbox1.Text, @"^[a-zA-Z]+$"))
        e.Handled = true;
}

数字や記号ではない TextBox にデータを入力したいだけです。

4

2 に答える 2

3

次のコードを使用してみてください

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString() , @"^[a-zA-Z]+$")) 
         e.Handled = true;
}

ありがとう!

于 2012-09-05T04:30:24.707 に答える
2

c#を使用しているようです。

次に、従う必要のある手順:

1)テキストボックスのcausesValidationプロパティをtrueに設定します

2)原因検証用のイベントリスナーを設定します

myTextBox1.Validating +=
  new System.ComponentModel.CancelEventHandler(myTextBox1_Validating);
myTextBox1.Validated += 
  new System.EventHandler(myTextBox1_Validated);

3)これらのイベントハドラー機能を実装する

private void myTextBox1_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{        
   if(!CheckIfTextBoxNumeric(myTextBox1))
   {
       myLabel.Text =  "Has to be numeric";
       e.Cancel = true;
   }
}
private void myTextBox1_Validated(object sender,System.EventArgs e)
{
   myLabel.Text = "Validated first control";          
}

代わりにmaskedTextBoxを使用する場合は、http://msdn.microsoft.com/en-us/library/ms234064(v = vs.80).aspxを参照してください。

于 2012-09-05T04:18:36.667 に答える