C# 用户输入-数据验证
我通过表单中的文本框接受用户的字符串值。然后在查询数据库表时使用它,尽管它是一个字符串,但我要求输入的字符必须是整数 我尝试过各种方法,比如C# 用户输入-数据验证,c#,winforms,user-input,validation,C#,Winforms,User Input,Validation,我通过表单中的文本框接受用户的字符串值。然后在查询数据库表时使用它,尽管它是一个字符串,但我要求输入的字符必须是整数 我尝试过各种方法,比如INT32和TryParse函数。但是,我在尝试执行IF-ELSE或TRY-CATCH时遇到问题,以阻止执行任何操作,直到输入“可接受” 允许只在文本框中输入整数,或者识别除整数以外的任何内容并导致执行失败的最简单方法是什么?使用NumericUpDown控件而不是TextBox使用NumericUpDown控件而不是TextBox是的,您可以使用: 您也可
INT32
和TryParse
函数。但是,我在尝试执行IF-ELSE
或TRY-CATCH
时遇到问题,以阻止执行任何操作,直到输入“可接受”
允许只在文本框中输入整数,或者识别除整数以外的任何内容并导致执行失败的最简单方法是什么?使用NumericUpDown控件而不是TextBox使用NumericUpDown控件而不是TextBox是的,您可以使用: 您也可以使用。是的,您可以使用:
您还可以使用。您可以编写自己的类,该类继承自TextBox:
public class NumericTextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
var key = e.KeyChar + "";
if (key == "\b")
return;
double number;
string newText = Text.Remove(SelectionStart, SelectionLength).Insert(SelectionStart, key);
if (newText.Length == 1 && key == "-")
return;
if (!double.TryParse(newText, NumberStyles.Float, CultureInfo.InvariantCulture, out number))
{
e.Handled = true;
}
}
public double Value
{
get { return Text.Length == 0 ? 0 : double.Parse(Text, CultureInfo.InvariantCulture); }
}
}
您可以编写从TextBox继承的自己的类:
public class NumericTextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
var key = e.KeyChar + "";
if (key == "\b")
return;
double number;
string newText = Text.Remove(SelectionStart, SelectionLength).Insert(SelectionStart, key);
if (newText.Length == 1 && key == "-")
return;
if (!double.TryParse(newText, NumberStyles.Float, CultureInfo.InvariantCulture, out number))
{
e.Handled = true;
}
}
public double Value
{
get { return Text.Length == 0 ? 0 : double.Parse(Text, CultureInfo.InvariantCulture); }
}
}
作为另一种选择,您可以使用TextBox TextChanged事件
int.trype每次更改值时对文本进行分割,如果不起作用,只需清除文本框(或保留最后一个有效的值,并在失败时还原为该值)。另一种方法是使用文本框TextChanged事件
int.trype每次更改值时都将文本分割,如果无效,只需清除文本框(或保留最后一个有效的值,并在失败时还原为该值)。我知道三种可能的方法:
private void txb_TextChanged(object sender, EventArgs e)
{
int selStart = txb.SelectionStart;
string result = txb.Text;
// remove all that aren't digits
result = Regex.Replace(result, @"[^0-9]", string.Empty);
txb.Text = result;
// move cursor
if (selStart > txb.Text.Length)
txb.Select(txb.Text.Length, 0);
else txb.Select(selStart, 0);
}
public class IntegerTextBox : TextBox
{
private Keys[] int_allowed = {
Keys.D1,
Keys.D2,
Keys.D3,
Keys.D4,
Keys.D5,
Keys.D6,
Keys.D7,
Keys.D8,
Keys.D9,
Keys.D0,
Keys.NumPad0,
Keys.NumPad1,
Keys.NumPad2,
Keys.NumPad3,
Keys.NumPad4,
Keys.NumPad5,
Keys.NumPad6,
Keys.NumPad7,
Keys.NumPad8,
Keys.NumPad9,
Keys.Back,
Keys.Delete,
Keys.Tab,
Keys.Enter,
Keys.Up,
Keys.Down,
Keys.Left,
Keys.Right
};
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Modifiers == Keys.Control) return;
if (!int_allowed.Contains(e.KeyCode))
{
e.SuppressKeyPress = true;
}
}
}
}
我知道三种可能的方法:
private void txb_TextChanged(object sender, EventArgs e)
{
int selStart = txb.SelectionStart;
string result = txb.Text;
// remove all that aren't digits
result = Regex.Replace(result, @"[^0-9]", string.Empty);
txb.Text = result;
// move cursor
if (selStart > txb.Text.Length)
txb.Select(txb.Text.Length, 0);
else txb.Select(selStart, 0);
}
public class IntegerTextBox : TextBox
{
private Keys[] int_allowed = {
Keys.D1,
Keys.D2,
Keys.D3,
Keys.D4,
Keys.D5,
Keys.D6,
Keys.D7,
Keys.D8,
Keys.D9,
Keys.D0,
Keys.NumPad0,
Keys.NumPad1,
Keys.NumPad2,
Keys.NumPad3,
Keys.NumPad4,
Keys.NumPad5,
Keys.NumPad6,
Keys.NumPad7,
Keys.NumPad8,
Keys.NumPad9,
Keys.Back,
Keys.Delete,
Keys.Tab,
Keys.Enter,
Keys.Up,
Keys.Down,
Keys.Left,
Keys.Right
};
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Modifiers == Keys.Control) return;
if (!int_allowed.Contains(e.KeyCode))
{
e.SuppressKeyPress = true;
}
}
}
}
NumericUpDown允许其他字符,例如,.-NumericUpDown允许其他字符,例如-