我構建了自定義MaskedTextBox
,更改了BeepOnError
和AsciiOnly
的值,併爲MaskInputRejected
添加了一個客戶EventHandler。
我建立了類,當我將它放在窗體上時,我添加到自定義類的自定義屬性出現,但對BeepOnError
和AsciiOnly
的更改沒有發生,自定義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);
非常感謝Angshuman Agarwal,加入了上面這兩個屬性的問題。我通過「設計」視圖設置它們。把上面的構造函數解決了。但你能幫我弄清楚爲什麼EventHandler不會開火嗎? – ggrewe1959
它的工作原理..更新了答案。拖動n拖放自定義遮罩文本框後,只需從設計器添加事件處理程序即可。我剛剛檢查過。只需複製粘貼customtextbox代碼,然後在窗體上使用它。在窗體上拖動後添加拒絕事件處理程序。 –
對,如果我手動將它添加到窗體,我可以讓EventHandler觸發。但我試圖從BaseClass中獲取EventHandler來觸發,而不必複製表單中的代碼。那有意義嗎? – ggrewe1959