0

我已经构建了一个 Custom MaskedTextBox,更改了 和 的值,BeepOnErrorAsciiOnly为 . 添加了一个客户 EventHandler MaskInputRejected。我构建了这个类,当我把它放在我的表单上时,我添加到自定义类的自定义属性会出现,但是对和
的更改不会,自定义 EventHandler 也不会触发。 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 拖放到了 FORM 上。它确实将其正确设置为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)并尝试输入字符。基类处理程序 ie ofCustomTextBox将被调用,您将看到在基类中添加的 MessageBox。

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

于 2012-06-16T18:36:00.710 回答