如何使文本框仅接受数字? [英] How do I make a text box only accept numbers?

查看:77
本文介绍了如何使文本框仅接受数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在编写一个计算器代码,用于计算物理方程的任何值,给出其他值。虽然这部分似乎正在工作,但我已设法将TryParse合并到转换中,以便将任何非数值设置为零,我很难弄清楚如何防止符号或字母的输入。



我真正需要的是一个代码,如果符号或字母插入文本框中将发送消息,或者是一个拒绝接受的文本框除了数值之外的其他输入。



任何人都可以帮助我吗?任何建议将不胜感激。



以下是我当前代码的示例。



谢谢。



我的尝试:



Hi there,

I'm currently writing a code for a calculator meant to work out any value of a physics equation given every other value. While this part appears to be working and I've managed to incorporate TryParse into the conversion so that it will set any non-numerical value to zero, I am having difficulty figuring out how to prevent the entry of symbols or letters.

What I really need is either a code that will send a message if a symbol or letter is inserted into the text box or a text box that will refuse to accept an input other than a numerical value.

Can anyone help me? Any advice would be greatly appreciated.

Below is a sample of my current code.

Thank you.

What I have tried:

public partial class Form1 : Form
{
    double P1, P2, V1, V2, U1, U2, v1, v2, h1, h2, Q, W, Z, g;

   //*******************************************************************
    public Form1()
    {
        InitializeComponent();

        Z = 0;
        P1 = P2 = V1 = V2 = U1 = U2 = v1 = v2 = h1 = h2 = Q = W = Z;
        g = 9.81;
    }

   //*******************************************************************
    private void txtBox_P2In_TextChanged(object sender, EventArgs e)
    {
        double.TryParse(txtBox_P2In.Text, out P2);
    }

   //*******************************************************************
    private void txtBox_P1In_TextChanged(object sender, EventArgs e)
    {
        double.TryParse(txtBox_P1In.Text, out P1);
    }

   //*******************************************************************
    private void CalcButton1_Click(object sender, EventArgs e)
    {
        if (radioButtonP1.Checked == true)
        {
            P1 = (((P2 * V2) + U2 + ((v2 * v2) / 2) + (g * h2) + W - Q - (g * h1) - ((v1 * v1) / 2) - U1) / V1);
            txtBox_P1Out.Text = P1.ToString();
        }
        else if (radioButtonP1.Checked == false)
        {
            txtBox_P1Out.Text = null;
        }
    }
}

推荐答案

您好会员13068046



如果您仍需要帮助:



1)删除 TextChanged 事件处理程序。

2)添加 KeyPress 事件处理程序。

3)代码为:
Hi Member 13068046,

If you still need help:

1) Remove the TextChanged event handler.
2) Add KeyPress event handler.
3) Code as:
private void txtBox_P1In_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar))
    {
        MessageBox.Show("Only numbers please!");
        e.Handled = true;
    }
}

'e.Handled = True'指示编译器您已明确处理输入,并通过进一步的操作(显示字符)来减轻编译器的职责。

'e.Handled = True' instructs the compiler that you have explicitly taken care of the input, and hereby relieving the compiler of its duty with further actions (displaying the chracter).


您可以使用下面的代码。



You can use below code.

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



虽然您正在使用计算类型应用程序,但您可能希望允许一个。(点)使用以下代码 keypress 事件 txtBox_P1In




and though you are working on calculation type application you may would like to allow one .(dot) use below to code in keypress event of txtBox_P1In

if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
     {
       e.Handled = true;
     }


这篇关于如何使文本框仅接受数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆