1

.1 つの値と数字のみを許可するためのテキストボックスの検証が必要です。私のテキストボックスの値は、数値と1つの.値のみを取る必要があることを意味します。値は 123.50 のようになります。値の末尾に値を追加するためのコードを使用して.ooいます。.50私のコードは

double x;
double.TryParse(tb.Text, out x);
tb.Text = x.ToString(".00");

キーボードからすべてのキーを取得していますが、数字と 1 つの値のみを取得したいと考えてい.ます。

4

8 に答える 8

7

テキスト ボックスにControl.KeyPressイベント ハンドラを追加します。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar))  //bypass control keys
    {
        int dotIndex = textBox1.Text.IndexOf('.');
        if (char.IsDigit(e.KeyChar))     //ensure it's a digit
        {   //we cannot accept another digit if
            if (dotIndex != -1 &&  //there is already a dot and
                //dot is to the left from the cursor position and
                dotIndex < textBox1.SelectionStart &&
                //there're already 2 symbols to the right from the dot
                textBox1.Text.Substring(dotIndex + 1).Length >= 2)
            {
                e.Handled = true;
            }
        }
        else //we cannot accept this char if
            e.Handled = e.KeyChar != '.' || //it's not a dot or
                        //there is already a dot in the text or
                        dotIndex != -1 ||   
                        //text is empty or
                        textBox1.Text.Length == 0 || 
                        //there are more than 2 symbols from cursor position
                        //to the end of the text
                        textBox1.SelectionStart + 2 < textBox1.Text.Length;
    }
}

次のように、デザイナーまたはコンストラクターで行うことができます。

public Form1()
{
    InitializeComponent();
    //..other initialization
    textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
}

また、テキストの最後だけでなく、任意の位置に数字を挿入できるように、いくつかのチェックを追加しました。ドットと同じ。ドットから右に 2 桁を超えないように制御します。TextBox.SelectionStart プロパティを使用して、テキスト ボックス内のカーソルの位置を取得しました。詳細については、このスレッドを確認してください:テキストボックス内のカーソルの位置を見つけるにはどうすればよいですか?

于 2012-12-08T06:02:59.593 に答える
2

textBox の keyPress イベントで単純にこれを行うことができます...

        e.Handled = !char.IsDigit(e.KeyChar)&&(e.KeyChar != '.') && !char.IsControl(e.KeyChar);
        if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
        {
            e.Handled = true;
        }
于 2015-04-14T16:30:02.390 に答える
1

これを試してください

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
               && !char.IsDigit(e.KeyChar)
               && e.KeyChar != '.')
            e.Handled = true;

        // only allow one decimal point
        if (e.KeyChar == '.'
            && textBox1.Text.IndexOf('.') > -1)
            e.Handled = true;
    }
于 2012-12-11T09:43:19.337 に答える
0
 if (textBox.Text!="")
        {
            string txt = textBox.Text;
            if (e.KeyChar.ToString().Any(Char.IsNumber) || e.KeyChar == '.')
            {
                textBox.Text = rate;
            }
            else
            {
                MessageBox.Show("Number Only", "Warning");
                textBox.Text = "";
            }
        }

テストしたコード

于 2018-09-25T12:09:02.587 に答える
-1
if(e.KeyChar.Equals('\b'))
 {
  e.Handled = false;
 }
else
 if (!char.IsControl(e.KeyChar)
           && !char.IsDigit(e.KeyChar)
           && e.KeyChar != '.')
  {
        e.Handled = true;
  }
 else
    // only allow one decimal point
    if (e.KeyChar == '.'
        && textBox1.Text.IndexOf('.') > -1)
  {        
    e.Handled = true;  
  }
于 2012-12-08T09:33:37.110 に答える