在c#中保存之前插入的自动计算值

时间:2017-02-19 05:04:57

标签: c#

我在点击保存之前,在将高度和重量的值插入文本框后尝试显示BMI结果。我该怎么办?

protected void btnSave_Click(object sender, EventArgs e)
{     
    Boolean transSuccess = false;      
    String myScript = "";       
    String sqlStmt = "";      

    OracleConnection conOra = new OracleConnection(conOraStr);
    conOra.Open();
    OracleTransaction transOra;
    transOra = conOra.BeginTransaction();

    double weight = Convert.ToDouble(txtWeight.Text);
    double height = Convert.ToDouble(txtHeight.Text);
    double bmi; 

    bmi = Math.Round(( weight / ( height * height))* 10000, 1);
    lblbmi.Text = String.Format(bmi.ToString(),"0,0");
}

2 个答案:

答案 0 :(得分:0)

这是你可以这样做的方式

protected void txtWeight_TextChanged(object sender, EventArgs e)
  {     
    Boolean transSuccess = false;      
    String myScript = "";       
    String sqlStmt = "";      

    OracleConnection conOra = new OracleConnection(conOraStr);
    conOra.Open();
    OracleTransaction transOra;
    transOra = conOra.BeginTransaction();

        double weight = Convert.ToDouble(txtWeight.Text);
        double height = Convert.ToDouble(txtHeight.Text);
        double bmi; 

        bmi=Math.Round(( weight / ( height * height))* 10000, 1);

        lblbmi.Text= String.Format(bmi.ToString(),"0,0");
  }
protected void txtHeight_TextChanged(object sender, EventArgs e)
  {     
    Boolean transSuccess = false;      
    String myScript = "";       
    String sqlStmt = "";      

    OracleConnection conOra = new OracleConnection(conOraStr);
    conOra.Open();
    OracleTransaction transOra;
    transOra = conOra.BeginTransaction();

        double weight = Convert.ToDouble(txtWeight.Text);
        double height = Convert.ToDouble(txtHeight.Text);
        double bmi; 

        bmi=Math.Round(( weight / ( height * height))* 10000, 1);

        lblbmi.Text= String.Format(bmi.ToString(),"0,0");
  }

答案 1 :(得分:0)

这是一个快速编写,您需要将其集成到您的代码中。

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    double height;
    double weight;
    double bmi;


    public Form1()
    {
        InitializeComponent();
    }


    private void calculate()
    {
        bmi = Math.Round((weight / (height * height)) * 10000, 1);

    }
    private void updateResult()
    {
        calculatedLabel.Text = String.Format(bmi.ToString(), "0,0"); ;
    }
    private void weightTxtbx_TextChanged(object sender, EventArgs e)
    {
        try
        {
            weight = Convert.ToDouble(weightTxtbx.Text);
        }
        catch
        {
            // stop app from crashing if input is not a number
        }
        calculate();
        updateResult();
    }

    private void heightTxtbx_TextChanged(object sender, EventArgs e)
    {
        try
        {
            height = Convert.ToDouble(heightTxtbx.Text);
        }
        catch
        {
            // stop app from crashing if input is not a number
        }
        calculate();
        updateResult();
    }
}
}