/// <summary> /// 入力値を変更しているときに走行します。 /// </summary> /// <param name="e">入力値許可イベントオブジェクト。</param> protected virtual void OnTextChangeValidation(TextChangeValidationEventArgs e) { if (this.TextChangeValidation != null) { this.TextChangeValidation(this, e); } }
/// <summary> /// テキスト変更中、バイト数チェック、許可文字チェックを行います。 /// </summary> /// <param name="sender"></param> /// <param name="e">入力値許可イベントオブジェクト。</param> private void MetLimitedTextBox_TextChangeValidation(object sender, TextChangeValidationEventArgs e) { int afterByteCount = this.ByteEncoding.GetByteCount(e.After); // 最大バイト長制御 if (this.MaxByteLength > 0 && afterByteCount > this.MaxByteLength) { e.Cancel = true; return; } // 複数行許可の場合は改行コードを除去した文字列をチェック対象とする string checkValue = e.Input.ToString(); if (Multiline) { checkValue = checkValue.Replace(Environment.NewLine, ""); } // 許可文字チェック if (!this.isValidChars(checkValue)) { e.Cancel = true; return; } }
private void metTextBox3_TextChangeValidation(object sender, Metroit.Windows.Forms.TextChangeValidationEventArgs e) { Console.WriteLine("validtion:" + e.After); if (e.After == "test") { e.Cancel = true; } if (e.After == "あ") { e.Cancel = true; } }
/// <summary> /// テキスト変更中、バイト数チェック、許可文字チェックを行います。 /// </summary> /// <param name="e">入力値許可イベントオブジェクト。</param> protected override void OnTextChangeValidation(TextChangeValidationEventArgs e) { // 文字の長さチェック if (!this.IsValidTextLength(e.After)) { e.Cancel = true; return; } // 許可文字チェック if (!this.IsValidChars(e.Input)) { e.Cancel = true; return; } base.OnTextChangeValidation(e); }
/// <summary> /// キーが押された時の動作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MetTextBox_KeyPress(object sender, KeyPressEventArgs e) { // 文字キー押下時、TextChangingを走行させる。 // BackSpace, Ctrl+X, Ctrl+Z, Ctrl+Vは何もしない if (e.KeyChar == '\b' || e.KeyChar == '\u0018' || e.KeyChar == '\u001a' || e.KeyChar == '\u0016') { return; } // Multiline でない時にEnter, Ctrl+Enter, Shift+Enter押下時は何もしない if (!this.Multiline && (e.KeyChar == '\r' || e.KeyChar == '\n')) { return; } // IME入力時、WM_CHAR, WM_IME_ENDCOMPOSITIONあたりでそれぞれ1文字ずつKeyPressが走行する // いずれかの文字が拒否されたら無条件に全文字を拒否する if (this.isDenyImeKeyChar) { e.Handled = true; return; } var inputText = this.createInputString(e.KeyChar.ToString()); // 入力後のテキストを取得 var afterText = this.createTextAfterInput(e); // TextChangingイベントの発行 var args = new TextChangeValidationEventArgs(); args.Cancel = false; args.Before = base.Text; args.Input = inputText; args.After = afterText; this.OnTextChangeValidation(args); // キャンセルされたら入力を拒否する if (args.Cancel) { this.isDenyImeKeyChar = true; e.Handled = true; } }
private void metTextBox1_TextChangeValidation(object sender, Metroit.Windows.Forms.TextChangeValidationEventArgs e) { //Console.WriteLine("TextBox TextChangeValidation"); }
private void metTextBox4_TextChangeValidation(object sender, Metroit.Windows.Forms.TextChangeValidationEventArgs e) { Console.WriteLine("Validtin:" + e.After); }
/// <summary> /// フォーカスを失った時、数値をフォーマット化する。 /// NOTE: NumericUpDown コントロールに準拠し、ウィンドウから離れた場合に値が確定されるようにする。 /// </summary> /// <param name="e"></param> protected override void OnLostFocus(EventArgs e) { this.leavedSelectionStart = this.SelectionStart; this.leavedSelectionLength = this.SelectionLength; // 小数記号、負数記号しか入力がない場合は空文字を見なす if (base.Text == "." || base.Text == "-") { this.textFormatting = true; base.Text = ""; this.textFormatting = false; } // 入力値を保存し、フォーマットされた値で描画しなおす if (string.IsNullOrEmpty(base.Text)) { if (this.AcceptNull) { this.value = null; } else { this.value = MinValue; } } else { this.value = decimal.Parse(base.Text); } // NOTE: isValidRangeValue() によって値が範囲外の場合は強制的に最小値/最大値とする if (value > MaxValue) { this.value = this.MaxValue; } if (value < MinValue) { this.value = this.MinValue; } // 内部値と異なるもしくは、フォーカスを得た時に値が入っており、nullに変更した場合に変化があったとする if ((this.value != this.internalValue) || (this.value == null && this.internalValue == null && this.enterValue != null)) { this.internalValue = this.value; // 入力値チェックイベント var e2 = new TextChangeValidationEventArgs(); e2.Cancel = false; e2.Before = this.value.ToString(); e2.Input = value.ToString(); e2.After = value.ToString(); this.OnTextChangeValidation(e2); if (e2.Cancel) { throw new DeniedTextException( ExceptionResources.GetString("DeniedTextException"), value.ToString()); } OnValueChanged(new EventArgs()); } this.textFormatting = true; base.Text = this.createFormattedText(this.value); this.textFormatting = false; this.ChangeDisplayColor(); base.OnLostFocus(e); }