my task is that, ask the user to write a number in a textbox between 1 and 5. I have to handle the bad cases, like the user write a wrong number or write character instead number and this have to run as long as the program get a correct number. This is what I have right now.
int right_number = int.Parse(number.Text);
try
{
if (right_number > 5 || right_number < 1)
{
MessageBox.Show("Warning");
}
}
catch (NotFiniteNumberException)
{
MessageBox.Show("Warning");
}
CodePudding user response:
This would be the cleanest way to validate your input:
bool IsInputValid(string input, out string warningMessage)
{
if (!int.TryParse(input, out var integerValue))
{
warningMessage = "input could not be parsed to an integer";
return false;
}
if (integerValue < 1 || integerValue > 5)
{
warningMessage = "input is not between one and five";
return false;
}
warningMessage = string.Empty;
return true;
}
The calling code would look something like this:
void TextChanged(string newText)
{
if (!IsInputValid(newText, out var warningMessage))
{
MessageBox.Show(warningMessage);
}
//handle valid input here
}
CodePudding user response:
The cleanest way to solve this is to use a NumericUpDown control with the following properties:
MinValue
= 1MaxValue
= 5Value
= (a good starting default)DecimalPlaces
= 0Increment
= 1
NumericUpDown doesn't accept non numeric input, or anything outside its ranges, so all those cases you are worried about just.. disappear. The user can also use up/down keys to adjust the value
CodePudding user response:
If user is supposed to enter 1, 2, 3, 4, 5
we can just prevent incorrect input (without annoying MessageBoxes etc.) with a help of KeyPress
event handler:
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e) {
// We allow commands like BackSpace, Shift Tab etc.
e.Handled = e.KeyChar >= ' ';
if (sender is Control control) {
// If user press 1..5 we put it into the TextBox
if (e.KeyChar >= '1' && e.KeyChar <= '5')
control.Text = e.KeyChar.ToString();
}
}
To prevent user pasting something from ClipBoard we can add TextChanged
event handler:
private void myTextBox_TextChanged(object sender, EventArgs e) {
if (sender is Control control) {
string text = control.Text.Trim();
if (text.Length == 1 && text[0] >= '0' && text[0] <= '5')
control.Text = text;
else
control.Text = "";
}
}
Now user can only do legal actions:
- Press digit in
1..5
range (incorrect value is ignored) - Delete
- Paste digit in
1..5
range (incorrect paste clears the text box)
CodePudding user response:
HTML supports regex patterns for validating inputs using the "pattern" attribute. You can then add an event listener to run when a user enters a value. The validity of the input can be checked with checkValidity()
HTML
<input pattern="[0-5]">
JS
var input = document.querySelector("input");
input.addEventListener('change', (event) =>
{
var valid = event.target.checkValidity();
//do something
});
CodePudding user response:
if (int.TryParse(number, out right_number))
{
if (right_number > 5 || right_number < 1)
{
MessageBox.Show("Warning");
}
}
else
{
MessageBox.Show("Warning");
}