How do I make a textbox that only accepts numbers?

后端 未结 30 1581
梦如初夏
梦如初夏 2020-11-21 06:05

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I\'ve done this kind of validation by overloading the KeyPress event

30条回答
  •  粉色の甜心
    2020-11-21 06:46

    In button click you can check text of textbox by for loop:

    char[] c = txtGetCustomerId.Text.ToCharArray();
    bool IsDigi = true;
    
    for (int i = 0; i < c.Length; i++)
         {
           if (c[i] < '0' || c[i] > '9')
          { IsDigi = false; }
         }
     if (IsDigi)
        { 
         // do something
        }
    

提交回复
热议问题