2017-02-19 66 views
2

我试图在将高度和重量值插入文本框之前,单击保存后显示BMI结果。我该怎么办?自动计算在保存之前插入的值c#

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"); 
} 
+1

您是否尝试过在'txtWeight'和'txtHeight'文本框上创建'change'事件并更新那里的'lblbmi'文本? – MaKCbIMKo

+1

这是最简单的方法。 – MaKCbIMKo

+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"); 
    } 
+2

这是不好的,每次值改变时,它都会使db连接...... – DuckSoy

+0

是的。但我只是举了一个例子与之合作。但仍然是tahnks –

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(); 
    } 
} 
}