简体   繁体   English

MaskedTextBox继承自定义EventHandler

[英]MaskedTextBox Inherit Custom EventHandler

I have built a Custom MaskedTextBox , changing the values of BeepOnError and AsciiOnly , as well as adding a customer EventHandler for MaskInputRejected . 我建立了一个自定义MaskedTextBox ,改变的值BeepOnErrorAsciiOnly ,以及增加客户对事件处理MaskInputRejected
I build the class and when I drop it on my Form, the custom properties I added to the Custom class appear, but the changes to the BeepOnError and AsciiOnly do not, nor does the custom EventHandler fire. 我构建了该类,并将其放到Form上时,出现了添加到Custom类的自定义属性,但是对BeepOnErrorAsciiOnly的更改却没有,自定义EventHandler也不会触发。

Can someone point out what I did wrong? 有人可以指出我做错了什么吗? The EventHandler works fine if I manually add it to the form. 如果我将其手动添加到表单中,则EventHandler可以正常工作。

Custom class; 自定义类;

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...");
    }

In InitializeComponent( ): 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);

I just tried this as a custom component and dragged - dropped CustomTextBox on the FORM. 我只是尝试将其作为自定义组件,然后将其拖放到FORM上的CustomTextBox中。 It does set it correctly as true . 它确实将其正确设置为true I checked in the Designer. 我签入了Designer。 Also, add the event handler after you drag and drop. 另外,在拖放后添加事件处理程序。 It works. 有用。 See the code below. 请参见下面的代码。

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);
        }
    }

On the Form, now Drag and Drop MyMaskedBox , add a Mask say Numeric (5-Digits) & try entering characters. 在“表单”上,现在拖放MyMaskedBox ,添加一个说Numeric (5-Digits)的蒙版,然后尝试输入字符。 The base class handler ie of CustomTextBox will be called and you will see the MessageBox added in the base class. 基类处理程序(即CustomTextBox将被调用,您将看到在基类中添加了MessageBox。

This will solve your issue and you will achieve the desired functionality now. 这将解决您的问题,并且您现在将获得所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM