Windows フォーム アプリケーションのテキスト ボックスへの入力を検証したい、つまり、入力された数値が 1 未満または 24 を超える場合、または整数以外の文字が入力された場合に、メッセージ ボックスにエラーを表示したい。どうすればこれを行うことができますか?
5955 次
5 に答える
3
私は次のようなものを考えます:
//make sure that we have a valid number in the text box with no other characters
//loop through the string, examining each character
for (int i = 0; i < txtHour.Text.Length; i++)
{
//if this character isn't a number, then don't close the form or continue
if (!char.IsNumber(txtHour.Text[i]))
{
MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
return;
}
}
//now that we know we have a valid number, convert the string to int and make sure it's not less than 1 or greater than 24
int testInt = Convert.ToInt32(txtHour.Text);
if (testInt < 1 || testInt > 24)
{
MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
return;
}
コメントで要求したメソッドの例については、次のようなことができます。
//////////////////////////////////////////////////////////////////////
//in your main code:
if (!isValidHour(textBox1.Text))
MessageBox.Show("Value for field must be a number from 1 to 24");
if (!isValidHour(textBox2.Text))
MessageBox.Show("Value for field must be a number from 1 to 24");
//////////////////////////////////////////////////////////////////////
///method to validate if text field is an INT from 1 to 24
bool isValidHour (string stringToValidate)
{
//make sure that we have a valid number in the text box with no other characters
//loop through the string, examining each character
for (int i = 0; i < stringToValidate.Length; i++)
{
//if this character isn't a number, then don't close the form or continue
if (!char.IsNumber(stringToValidate[i]))
{
//MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
return false;
}
}
//now that we know we have a valid number, convert the string to int and make sure it's not less than 1 or greater than 24
int testInt = Convert.ToInt32(stringToValidate);
if (testInt < 1 || testInt > 24)
{
//MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24");
return false;
}
return true;
}
于 2013-10-28T19:27:48.690 に答える
1
プライベート メソッドを追加し、検証が必要なテキスト ボックス コントロールを渡すことで、必要な場所でそれを呼び出すことができます。
private void ValidateText(TextBox textbox)
{
int value;
bool isConverted = Int32.TryParse(textbox.Text.Trim(), out value);
if (!isConverted)
{
MessageBox.Show("Only numbers allowed");
return;
}
if (value < 1 || value > 24)
{
MessageBox.Show("Please enter a value between 1-24");
}
}
Validating txtHour by invoking above method
ValidateText(txtHour);
于 2013-10-29T09:07:43.733 に答える
1
try{
if((int)item.value >= 1 && (int)item.value <=25){
//match.
}else{
//error.
}
}catch (Exception e){
//type error
}
//or---
var itemValue = default(int);
if(int.TryParse(item.value, out itemValue)){
if(itemValue >= 1 && itemValue <= 25){
//match.
}else{
//error.
}
}else{
//item.value is not numeric.
}
于 2013-10-28T17:38:00.330 に答える
0
これは非常に単純化された実装ですが、これにより、求めている検証が達成されます。
int hoursEntered;
bool isInteger;
isInteger = int.TryParse(txtHour.Text, out hoursEntered);
if (hoursEntered < 1 || hoursEntered > 24 && isInteger == true)
MessageBox.Show("Your number is either less than 1, greater than 24 or you didn't enter a number", "Warning!", MessageBoxButtons.OK);
ただし、ここで考慮すべき点がいくつかあります。このコードを作成し、ボタン クリックのイベント ハンドラーに関連付けました。それがあなたの実装でない場合は、OnKeyPress/OnKeyUp/OneKeyDown
イベントを使用して検証を行うことを強く検討します。それ以外の場合は、このコードを、既に配置されているボタン クリック イベント ハンドラーにコピーするだけです。
于 2013-10-28T19:00:46.810 に答える
0
OnKeyPress/OnKeyDown/OnKeyUp イベントでチェックを行うことを検討してください。
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
于 2013-10-28T17:35:50.703 に答える