自定義Text控件控件,使其衹能輸入"0123456789"

自定義Text控件控件,使其衹能輸入"0123456789"

自定義一個Text控件,在其中加入以下代碼:

        #region 重寫事件
        
string sKeys = "0123456789";
        
protected override void OnKeyPress(KeyPressEventArgs e)
        {
            
base.OnKeyPress(e);

            
if ((this.SelectedText == this.Text) && this.SelectedText != "")
            {
                
if (sKeys.IndexOf(e.KeyChar) < 0)
                {
                    e.Handled 
= true;
                }
                
return;
            }          
        }   
        
#endregion


原文地址:https://www.cnblogs.com/scottckt/p/885891.html