C# 禁用文本框中的负值的步骤
我正在处理windows窗体。。我需要我的文本框不接受负值..我该怎么做。。C# 禁用文本框中的负值的步骤,c#,winforms,textbox,C#,Winforms,Textbox,我正在处理windows窗体。。我需要我的文本框不接受负值..我该怎么做。。 是否有任何属性可用于执行相同操作…是的,您可以在textbox的textchanged事件中编写以下代码部分 if(textBox1.Text.Length >= 2) { int acceptednumber = Convert.ToInt32(textBox1.Text); if(acceptednumber < 0) { textBox1.Text = ""; MessageBox.Show("-ve
是否有任何属性可用于执行相同操作…是的,您可以在textbox的textchanged事件中编写以下代码部分
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
if(textBox1.Text.Length>=2)
{
int acceptednumber=Convert.ToInt32(textBox1.Text);
如果(接受编号<0)
{
textBox1.Text=“”;
MessageBox.Show(“-ve值不允许”);
}
其他的
{
textBox1.Text=textBox1.Text;
}
}
是的,您可以在textbox的textchanged事件中编写以下代码部分
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
if(textBox1.Text.Length>=2)
{
int acceptednumber=Convert.ToInt32(textBox1.Text);
如果(接受编号<0)
{
textBox1.Text=“”;
MessageBox.Show(“-ve值不允许”);
}
其他的
{
textBox1.Text=textBox1.Text;
}
}
您需要编写文本框的按键事件,如:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
您还可以使用数字上下控件来防止负值
更新:
参考:Sai Kalyan Akshinthala
我的代码不会处理复制/粘贴的情况。用户可以通过复制/粘贴输入负值。所以我认为Sai Kalyan Akshinthala的答案对这种情况是正确的,除了长度>=2的一个小变化
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
}
private void textBox1\u TextChanged(对象发送者,事件参数e)
{
如果(textBox1.Text.Length>=2)
{
int acceptednumber=Convert.ToInt32(textBox1.Text);
如果(接受编号<0)
{
textBox1.Text=“”;
MessageBox.Show(“-ve值不允许”);
}
其他的
{
textBox1.Text=textBox1.Text;
}
}
}
您需要编写文本框的按键事件,如:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
您还可以使用数字上下控件来防止负值
更新:
参考:Sai Kalyan Akshinthala
我的代码不会处理复制/粘贴的情况。用户可以通过复制/粘贴输入负值。所以我认为Sai Kalyan Akshinthala的答案对这种情况是正确的,除了长度>=2的一个小变化
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
}
private void textBox1\u TextChanged(对象发送者,事件参数e)
{
如果(textBox1.Text.Length>=2)
{
int acceptednumber=Convert.ToInt32(textBox1.Text);
如果(接受编号<0)
{
textBox1.Text=“”;
MessageBox.Show(“-ve值不允许”);
}
其他的
{
textBox1.Text=textBox1.Text;
}
}
}
只需使用最小值,模式将不允许输入负值
min=“0”pattern=“^[0-9]+$”在输入类型中,只需使用min,pattern将不允许输入负值
输入类型中的min=“0”pattern=“^[0-9]+$”这是正确的方法。如果他们复制/粘贴或拖放文本,拦截按键没有帮助。这是正确的方法。如果他们复制/粘贴或拖放文本,则拦截按键没有帮助。当用户复制-ve值并粘贴到文本框中时,您的代码将不起作用。当用户复制-ve值并粘贴到文本框中时,您的代码将不起作用。