当前位置:编程学习 > C#/ASP.NET >>

C# TextBox 输入计算表达式的验证和计算



在百度框里面输入一个计算表达式以后他可以自动计算结果,现在我想要在TextBox里面做一个这个功能,不过要先验证表达式是否正确,在计算结果写回到Textbox,这个应该怎么做啊?
--------------------编程问答-------------------- #region 根据计算式计算建筑面积


    private int Result()
    {
        int Int_Result = 0;
        try
        {
            if (txt_Formula.Text.Trim() != "")
            {
                decimal Dec_Formula = decimal.Parse(new DataTable().Compute(txt_Formula.Text.Trim(), "").ToString().Trim());
                if (Dec_Formula > 0)
                {
                    txt_BuildArea.Text = Dec_Formula.ToString("f2");
                }
                else
                {
                    Int_Result = 2;
                }
            }
        }
        catch
        {
            Int_Result = 1;
        }
        return Int_Result;
    }
    #endregion

protected void btn_Cal_Click(object sender, EventArgs e)
    {
        int Int_Result = Result();//根据计算式计算面积


        if (Int_Result == 1)
        {
            Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('计算式有误!')</script>");
            txt_Formula.Focus();//面积计算式


        }
        else if (Int_Result == 2)
        {
            Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('建筑面积必须大于0!')</script>");
            txt_Formula.Focus();//面积计算式
        }
    } --------------------编程问答-------------------- http://www.piikee.net/go.php/page/1/18/ --------------------编程问答-------------------- onkeyup事件,加入正则验证。 --------------------编程问答--------------------
引用 3 楼 chengwen2402163 的回复:
onkeyup事件,加入正则验证。


这个正则表达式怎么写啊?好像没有见过验证运算表达式的正则表达式
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,