2017-02-19 68 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(); 
    } 
} 
}