0

Custom を構築し、とMaskedTextBoxの値を変更し、 の顧客 EventHandler を追加しました。 クラスを構築し、それをフォームにドロップすると、カスタム クラスに追加したカスタム プロパティが表示されますが、変更は行われず、カスタム EventHandler も起動しません。 BeepOnErrorAsciiOnlyMaskInputRejected
BeepOnErrorAsciiOnly

誰かが私が間違っていたことを指摘できますか? 手動でフォームに追加すると、EventHandler は正常に動作します。

カスタム クラス;

public partial class BaseMaskedTextBox : MaskedTextBox
{
    public string gsOrigValue { get; set; }
    public string gsReadOnlyMode { get; set; }
    public bool gbIsString { get; set; }
...
    private void BaseMaskedTextBox_MaskInputRejected(Object sender, MaskInputRejectedEventArgs e)
    {
        System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
        messageBoxCS.AppendFormat("{0} = {1}", "Character Position", e.Position);
        messageBoxCS.AppendLine();
        messageBoxCS.AppendFormat("{0} = {1}", "Reason Rejected", e.RejectionHint);
        messageBoxCS.AppendLine();
        MessageBox.Show(messageBoxCS.ToString(), "Input Mask Invalid...");
    }

InitializeComponent():

this.BaseMaskedTextBox1.AsciiOnly = <b>true</b>;
this.BaseMaskedTextBox1.BeepOnError = <b>true</b>;
this.BaseMaskedTextBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.BaseMaskedTextBox1.Location = new System.Drawing.Point(0, 0);
this.BaseMaskedTextBox1.Name = "BaseMaskedTextBox1";
this.BaseMaskedTextBox1.Size = new System.Drawing.Size(100, 21);
this.BaseMaskedTextBox1.TabIndex = 0;
this.BaseMaskedTextBox1.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.BaseMaskedTextBox_MaskInputRejected);
this.ResumeLayout(false);
4

1 に答える 1

1

これをカスタム コンポーネントとして試し、フォームに CustomTextBox をドラッグ アンド ドロップしました。として正しく設定しtrueます。デザイナーにチェックインしました。また、ドラッグ アンド ドロップ後にイベント ハンドラを追加します。できます。以下のコードを参照してください。

public partial class CustomTextBox : MaskedTextBox
{
    public CustomTextBox()
    {
        InitializeComponent();

        this.BeepOnError = true;
        this.AsciiOnly = true;
        this.MaskInputRejected += new MaskInputRejectedEventHandler(CustomTextBox_MaskInputRejected);
    }
    /// <summary>
    /// Alas this event is not virtual in the base MS class like other events. Hence,
    /// we have to mark it as virtual now
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
   public virtual void CustomTextBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
        System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
        messageBoxCS.AppendFormat("{0} = {1}", "Character Position", e.Position);
        messageBoxCS.AppendLine();
        messageBoxCS.AppendFormat("{0} = {1}", "Reason Rejected", e.RejectionHint);
        messageBoxCS.AppendLine();
        MessageBox.Show(messageBoxCS.ToString(), "Input Mask Invalid...");
    }

}

// Derive a class like this and override the event where you delegate the request to the base class

 public class MyMaskedBox : CustomTextBox
    {
        public override void CustomTextBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            base.CustomTextBox_MaskInputRejected(sender, e);
        }
    }

フォーム上でドラッグ アンド ドロップMyMaskedBoxし、マスクを追加して、Numeric (5-Digits)文字を入力してみてください。基本クラスのハンドラ、つまり ofCustomTextBoxが呼び出され、基本クラスに追加された MessageBox が表示されます。

これで問題が解決し、目的の機能が実現します。

于 2012-06-16T18:36:00.710 に答える