5

重複の可能性:
数字のみを受け入れるテキストボックスを作成するにはどうすればよいですか?

文字列として保存したい電話番号があります。

私はこれを使用して読んだ

txtHomePhone.Text

私が必要だと思うのは、ある種の数値ですが、それを機能させることができません

if (txtHomePhone.Text == //something.IsNumeric)
{
    //Display error
}
else
{
    //Carry on with the rest of program ie. Add Phone number to program.
}

数値のみを入力できるようにする最善の方法は何ですか?

4

6 に答える 6

10

txtHomePhoneを表すためTextBox、イベントを使用して、KeyPress許可したい文字を受け入れ、許可したくない文字を拒否することができますtxtHomePhone

public Form1()
{
    InitializeComponent();
    txtHomePhone.KeyPress += new KeyPressEventHandler(txtHomePhone_KeyPress);
}
private void txtHomePhone_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The  character represents a backspace
    {
        e.Handled = false; //Do not reject the input
    }
    else
    {
        e.Handled = true; //Reject the input
    }
}

注意:次の文字 (表示されていません)は、バックスペースを表します。
注意: を使用して、特定の文字を常に許可または禁止することができますe.Handled
注意-: 、(またはを 1 回だけ使用する場合は、条件ステートメントを作成できます)。これらの文字を特定の位置に入力できるようにする場合は、正規表現を使用することをお勧めします。

if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The  character represents a backspace
{
    e.Handled = false; //Do not reject the input
}
else
{
    if (e.KeyChar == ')' && !txtHomePhone.Text.Contains(")"))
    {
        e.Handled = false; //Do not reject the input
    }
    else if (e.KeyChar == '(' && !txtHomePhone.Text.Contains("("))
    {
        e.Handled = false; //Do not reject the input
    }
    else if (e.KeyChar == '-' && !textBox1.Text.Contains("-"))
    {
        e.Handled = false; //Do not reject the input
    }
    else if (e.KeyChar == ' ' && !txtHomePhone.Text.Contains(" "))
    {
        e.Handled = false; //Do not reject the input
    }
    else
    {
        e.Handled = true;
    }
}

ありがとう、
これがお役に立てば幸いです:)

于 2012-11-06T14:52:41.523 に答える
7

ここで Windows フォームを使用していると仮定しています。MaskedTextBoxを見てください。文字の入力マスクを指定できます。

txtHomePhone.Mask = "##### ### ###";

これにより入力値を制限できるため、値を整数に安全に解析できます。

注: WPF を使用している場合、基本ライブラリに MaskedTextBox はないと思いますが、同様の機能を提供する可能性のある拡張機能がNuGetで利用できます。

于 2012-11-06T14:37:06.097 に答える
4

数値が入力されたかどうかを確認するには、次を使用できます。Integer.TryParse

int num;
bool isNum = Integer.TryParse(txtHomePhone.Text.Trim(), out num);

if (!isNum)
    //Display error
else
    //Carry on with the rest of program ie. Add Phone number to program.

ただし、電話番号は必ずしも数字だけではないことに注意してください。マスクされたテキストボックスについては、Trevor Pilley の回答を参照してください。

于 2012-11-06T14:36:56.087 に答える
3

これを試して

if (!txtHomePhone.Text.All(c=> Char.IsNumber(c)))
{
    //Display error
}
else
{
    //Carry on with the rest of program ie. Add Phone number to program.
}
于 2012-11-06T14:36:17.473 に答える
0

これを行うための最良の方法は、ユーザーがテキスト ボックスに数字キーを入力することのみを許可することです。ご協力ありがとうございます。

于 2012-11-06T14:53:20.840 に答える
0

通常、davenewza が推奨するように Integer.TryParse を使用しますが、別の方法として、C# の VisualBasic IsNumeric 関数を使用することもできます。

Microsoft.VisualBasic.dll ファイルへの参照を追加し、次のコードを使用します。

if (Microsoft.VisualBasic.Information.IsNumeric(txtHomePhone.Text))
{
    //Display error
}
else
{
    //Carry on with the rest of program ie. Add Phone number to program.
}
于 2012-11-06T14:48:14.247 に答える