2012-06-16 30 views
0

我構建了自定義MaskedTextBox,更改了BeepOnErrorAsciiOnly的值,併爲MaskInputRejected添加了一個客戶EventHandler。
我建立了類,當我將它放在窗體上時,我添加到自定義類的自定義屬性出現,但對BeepOnErrorAsciiOnly的更改沒有發生,自定義EventHandler也沒有發生。MaskedTextBox Inherit自定義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

我只是想這是一個自定義組件,拖 - 落下的表單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) &嘗試輸入的字符。基類處理程序即CustomTextBox將被調用,您將看到在基類中添加的MessageBox。

這將解決您的問題,您現在就可以實現所需的功能。

+0

非常感謝Angshuman Agarwal,加入了上面這兩個屬性的問題。我通過「設計」視圖設置它們。把上面的構造函數解決了。但你能幫我弄清楚爲什麼EventHandler不會開火嗎? – ggrewe1959

+0

它的工作原理..更新了答案。拖動n拖放自定義遮罩文本框後,只需從設計器添加事件處理程序即可。我剛剛檢查過。只需複製粘貼customtextbox代碼,然後在窗體上使用它。在窗體上拖動後添加拒絕事件處理程序。 –

+0

對,如果我手動將它添加到窗體,我可以讓EventHandler觸發。但我試圖從BaseClass中獲取EventHandler來觸發,而不必複製表單中的代碼。那有意義嗎? – ggrewe1959