MaskedTextBox继承自定义EventHandler

时间:2012-06-16 18:18:48

标签: c# winforms

我构建了自定义MaskedTextBox,更改了BeepOnErrorAsciiOnly的值,并为MaskInputRejected添加了一个客户EventHandler。
我构建了类,当我将它放在我的表单上时,会出现我添加到Custom类的自定义属性,但BeepOnErrorAsciiOnly的更改不会,自定义的EventHandler也不会触发。

有人可以指出我做错了什么吗?如果我手动将它添加到表单中,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);

1 个答案:

答案 0 :(得分:1)

我只是尝试将其作为自定义组件并拖动 - 在FORM上删除了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)&amp;尝试输入字符。将调用基类处理程序,即CustomTextBox,您将看到基类中添加了MessageBox。

这将解决您的问题,您现在可以实现所需的功能。