Is there a way to only allow a user to input a maximum number of characters into a text box? I want the user to input a mark/grade and only be able to input 0 - 100. Below I have code that monitors the keystroke and only allows for numbers to be input, but I want to find a way to only allow the user to input a number with a minimum value of 0 and a maximum of 100.
private void TxtMark4_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '0' || e.KeyChar > '9' || e.KeyChar == ' ')
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
or I could use the following:
if (e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == ' ')
{
e.Handled = false;
}
else
{
MessageBox.Show("You Can Only Enter A Number!");
e.Handled = true;
}
But I would like to find a way to only allow three characters to be input maximum.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…