4

I am new to windows 8 phone. I am writing a calculator app that can only accept numbers in the textbox and just a single decimal point. how do I prevent users from inputting two or more decimal Points in the text box as the calculator cant handle that.

I have been using Keydown Event, is that the best or should I use Key up?

private void textbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {

}
4

14 に答える 14

4

これをKeyPressイベントに使用して、フォーム内のテキストボックスのキープレスイベントを設定できます.Designerはこのようにします

this.yourtextbox.KeyPress +=new System.Windows.Forms.KeyPressEventHandler(yourtextbox_KeyPress);

次に、フォームで使用します

//only number And single decimal point input فقط عدد و یک ممیز میتوان نوشت
        public void onlynumwithsinglepoint(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            { e.Handled = true; }
            TextBox txtDecimal = sender as TextBox;
            if (e.KeyChar == '.' && txtDecimal.Text.Contains("."))
            {
                e.Handled = true;
            }
        }

それから

private void yourtextbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            onlynumwithsinglepoint(sender, e);
        }
于 2015-01-22T15:08:46.470 に答える
1
private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
        {
            // Allow Digits and BackSpace char
        }
        else if (e.KeyChar == '.' && !((TextBox)sender).Text.Contains('.'))
        {
            //Allows only one Dot Char
        }
        else
        {
            e.Handled = true;
        }
    }
于 2016-06-17T07:29:35.350 に答える
1

シンプルに

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        char ch = e.KeyChar;
        decimal x;
        if (ch == (char)Keys.Back)
        {
            e.Handled = false;
        }
        else if (!char.IsDigit(ch) && ch != '.' || !Decimal.TryParse(textBox1.Text+ch, out x) )
        {
            e.Handled = true;
        }
    }
于 2015-07-16T12:09:16.603 に答える
0

asp:RegularExpressionValidatorコントローラーでこれを試してください

<asp:RegularExpressionValidator ID="rgx" 
ValidationExpression="[0-9]*\.?[0-9]" ControlToValidate="YourTextBox" runat="server" ForeColor="Red" ErrorMessage="Decimals only!!" Display="Dynamic"  ValidationGroup="lnkSave"></asp:RegularExpressionValidator>
于 2016-12-29T05:49:14.213 に答える
0
private void txtDecimal_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar!='.')
    {
        e.Handled = true;
    }
    if (e.KeyChar == '.' && txtDecimal.Text.Contains("."))
    {
        e.Handled = true;
    }
}
于 2014-01-19T15:24:06.200 に答える
0

数値テキストボックスまたは

これを試して

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
 {
  e.Handled = false;
 }
 else
 {
    e.Handled = true;
 }
}
于 2013-11-04T04:23:49.493 に答える
0

これは私が使ったものです。

 private void txtBoxBA_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        // only allow 0-9 and "."
        e.Handled = !((e.Key.GetHashCode() >= 48 && e.Key.GetHashCode() <= 57));

        // check if "." is already there in box.
        if (e.Key.GetHashCode() == 190)
            e.Handled = (sender as TextBox).Text.Contains(".");
    }
于 2016-06-20T17:20:32.813 に答える
0
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar != Convert.ToChar(Keys.Enter) && e.KeyChar != Convert.ToChar(Keys.Back) && e.KeyChar != Convert.ToChar(Keys.Delete) && e.KeyChar != '.' && e.KeyChar != '1' && e.KeyChar != '2' && e.KeyChar != '3' && e.KeyChar != '4' && e.KeyChar != '5' && e.KeyChar != '6' && e.KeyChar != '7' && e.KeyChar != '8' && e.KeyChar != '9' && e.KeyChar != '0')
        {
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }

    }
于 2017-01-05T15:33:37.833 に答える
0

これを見てください。これは私のために働きます。

 private void txtParciales_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(txtParciales.Text, @"\d+(\.\d{2,2})"))
                e.Handled = true;
            else e.Handled = false;
        }
于 2015-06-17T17:40:51.413 に答える