質問

Ctrl-V / Ctrl-Cなどの特殊なキーストロークをブロックせずに、特定の入力キーがTextBoxで使用されるのをブロックする最良の方法は何ですか?

たとえば、ユーザーがAまたはBまたはCなどの文字または数値のサブセットのみを入力できるようにします。

役に立ちましたか?

解決

keydown-eventを使用し、キーが許可されていない場合はe.cancelを使用してキーを停止します。複数の場所で実行する場合は、テキストボックスを継承するユーザーコントロールを作成し、それを処理するAllowedCharsまたはDisallowedCharsプロパティを追加します。時々使用するいくつかのバリエーションがあります。いくつかはお金のフォーマットと入力を許可し、いくつかは編集などに使用できます。

ユーザーコントロールとして実行することの良い点は、追加して独自のキラーテキストボックスに追加できることです。 ;)

他のヒント

Winformsに Masked Textbox コントロールを使用しました。詳細については、こちらで説明しています。基本的に、フィールドの条件に一致しない入力は許可されません。人々に数字以外を入力させたくない場合は、単に数字以外の入力を許可しません。

これは私が通常これをどのように扱うかです。

Regex regex = new Regex("[0-9]|\b");            
e.Handled = !(regex.IsMatch(e.KeyChar.ToString()));

これは、数字とバックスペースのみを許可します。問題は、この場合コントロールキーの使用が許可されないことです。その機能を維持したい場合は、独自のテキストボックスクラスを作成します。

Ctrl-V、Ctrl-C、Delete、またはBackspaceのProcessCmdKeyでキープレスの事前チェックを実行し、これらのキーのいずれでもない場合はさらに検証を実行することで、唯一の有効なソリューションが終了したことがわかりました正規表現を使用したKeyPressイベント。

これはおそらく最良の方法ではありませんが、私の状況では機能しました。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // check the key to see if it should be handled in the OnKeyPress method
    // the reasons for doing this check here is:
    // 1. The KeyDown event sees certain keypresses differently, e.g NumKeypad 1 is seen as a lowercase A
    // 2. The KeyPress event cannot see Modifer keys so cannot see Ctrl-C,Ctrl-V etc.
    // The functionality of the ProcessCmdKey has not changed, it is simply doing a precheck before the 
    // KeyPress event runs
    switch (keyData)
    {
        case Keys.V | Keys.Control :
        case Keys.C | Keys.Control :
        case Keys.X | Keys.Control :
        case Keys.Back :
        case Keys.Delete :
            this._handleKey = true;
            break;
        default:
            this._handleKey = false;
            break;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}


protected override void OnKeyPress(KeyPressEventArgs e)
{
    if (String.IsNullOrEmpty(this._ValidCharExpression))
    {
        this._handleKey = true;
    }
    else if (!this._handleKey)
    {
        // this is the final check to see if the key should be handled
        // checks the key code against a validation expression and handles the key if it matches
        // the expression should be in the form of a Regular Expression character class
        // e.g. [0-9\.\-] would allow decimal numbers and negative, this does not enforce order, just a set of valid characters
        // [A-Za-z0-9\-_\@\.] would be all the valid characters for an email
        this._handleKey = Regex.Match(e.KeyChar.ToString(), this._ValidCharExpression).Success;
    }
    if (this._handleKey)
    {
        base.OnKeyPress(e);
        this._handleKey = false;
    }
    else
    {
        e.Handled = true;
    }

}

テキストボックスにTextChangedイベントを使用できます。

    private void txtInput_TextChanged(object sender, EventArgs e)
    {
        if (txtInput.Text.ToUpper() == "A" || txtInput.Text.ToUpper() == "B")
        {
            //invalid entry logic here
        }
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top