代码之家  ›  专栏  ›  技术社区  ›  Sai

如果文本框为空或已经为0,如何禁止用户在文本框中插入0?

  •  2
  • Sai  · 技术社区  · 7 年前

    TextBox 0 ,不允许按键

    我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Lucifer    7 年前

    实施前请参考 TextChanged KeyPress

     private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
     {
        e.Handled = (e.KeyChar == '0');
     }
    

    要处理文本框文本,您可以尝试的是

     private void txtBox_TextChanged(object sender, KeyPressEventArgs e)
     {
        if (String.IsNullOrEmpty(textbox.Text) || txtmoney.Text == "0")
        {
            // Do Something 
        }
     }
    

    txtBox.KeyPress += txtBox_KeyPress;
    txtBox.TextChanged += txtBox_TextChanged;
    
    推荐文章