1. 程式人生 > >用C#編寫的Windows應用程式如何控制TextBox中只能輸入數字?

用C#編寫的Windows應用程式如何控制TextBox中只能輸入數字?

textbox有個KeyPress事件

textbox改為對應的就可以用了                private   void   textBox_KeyPress(object   sender,   KeyPressEventArgs   e)                 {                         if   (textBox3.SelectionStart   ==   0)                         {                                 if   (e.KeyChar.CompareTo( '0 ')   ==   0   ||   e.KeyChar.CompareTo( '0 ')   <   0   ||   e.KeyChar.CompareTo( '9 ')   >   0) 

                                {                                         e.Handled   =   true;                                 }                         }                         else                         {                                 if   (e.KeyChar.CompareTo( '0 ')   <   0   ||   e.KeyChar.CompareTo( '9 ')   >   0) 
                                {                                         if   (e.KeyChar   !=   '/b ')                                                 e.Handled   =   true;                                 }                         }                 }