继承System.Windows.Forms.TextBox控件类,然后定制其KeyPress事件句柄,如this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPress);。在该方法private void OnKeyPress(object sender, KeyPressEventArgs e)中,限制自能接受数字和编辑键。 具体代码参考: 
public class MaskedTextBox : System.Windows.Forms.TextBox 

private bool m_decimalOnly; 
private System.Windows.Forms.ErrorProvider errorProvider1; public bool DecimalOnly 

get {return m_decimalOnly;} 
set {m_decimalOnly = value;} 
} private void InitializeComponent() 

this.errorProvider1 = new System.Windows.Forms.ErrorProvider(); this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.BlinkIfDifferentError; 
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPress); 
} private void OnKeyPress(object sender, KeyPressEventArgs e) 

MaskedTextBox sd = (MaskedTextBox)sender; 
if(sd.m_decimalOnly) 
sd.MaskDecimal(e); 
} private void MaskDecimal(KeyPressEventArgs e) 

if(Char.IsDigit(e.KeyChar) || e.KeyChar == 8) 

//Indicate the System.Windows.Forms.Control.KeyPress event is not handled 
e.Handled = false; 
errorProvider1.SetError(this,""); 

else 

e.Handled = true; 
errorProvider1.SetError(this,"Only valid for Digital and dot"); 

} public MaskedTextBox() 

InitializeComponent(); 
} protected override void OnPaint(PaintEventArgs pe) 

// TODO: Add custom paint code here // Calling the base class OnPaint 
base.OnPaint(pe);