2014-09-13 123 views
-1

我目前正在使用Visual Studio中的Windows窗體工作,並且有點卡住了。Catch異常和重置窗體窗體C#

我想測試用戶輸入是否是數字,如果輸入不是數字,會彈出一個小消息框並提醒用戶。 (我已經完成了這部分)。我遇到的問題是與錯誤繼續在Visual Studio

error

這是格式問題,彈出?我應該重新格式化我的代碼嗎?正如你可能看到我是新的C#(和編程)。

我的代碼:再次

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace wtgCalculator { 
public partial class Form1 : Form { 

    const double maleratio = 0.536; 
    const double femaleratio = 0.492; 


    public Form1() { 

     InitializeComponent(); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) { 

    } 

    private void Form1_Load(object sender, EventArgs e) { 

    } 

    private void label1_Click(object sender, EventArgs e) { 

    } 

    private void textBox1_TextChanged_1(object sender, EventArgs e) { 

    } 

    private void WaistNumericCheck() { 

     double waist; 
     string inpwaist = heighttextbox.Text; 

     if (double.TryParse(inpwaist, out waist)) { 

      MessageBox.Show("Value must be numeric");  //this is where the problem is 



     }          
    } 

    private void HeightNumericCheck() {   
     //todo 
    } 

    private void button1_Click(object sender, EventArgs e) { 

     //record inputs to variables 
     WaistNumericCheck(); 
     HeightNumericCheck(); 
     double height = double.Parse(heighttextbox.Text); 
     double waist = double.Parse(waisttextbox.Text);    
     //check is inputs are within boundry   
     CheckDimensions(height, waist); 
     //test 
     // ShowResult(height, waist);    

    } 


    private void CheckLimits(double height, double waist) { 

     double result = CalculateRatio(height, waist); 

     if (Female.Checked) { 

      if (result < femaleratio) { 

       MessageBox.Show("Your risk of obesity related cardiovasular is low"); 

      } 

      if (result > femaleratio) { 

       MessageBox.Show("Your risk of obesity related to cardiovascular is high"); 

      } 

     } 

     if (Male.Checked) { 

      if (result < maleratio) { 

       MessageBox.Show("Your risk of obesity related cardiovasular is low"); 

      } 

      if (result > maleratio) { 

       MessageBox.Show("Your risk of obesity related cardiovasular is High"); 


      } 
     } 


     //testing 
     MessageBox.Show(result.ToString()); 

    } 

    private void ShowResult(double height, double waist) { 

     //double result = CalculateRatio(height, waist); 

     //if (Female.Checked) { 

     // if (result < femaleratio) { 

     //  MessageBox.Show("Your risk of obesity related cardiovasular is low"); 

     // } 

     // if (result > femaleratio) { 

     //  MessageBox.Show("Your risk of obesity related to cardiovascular is high"); 

     // } 

     //} 

     //if (Male.Checked) { 



     //} 


    } 

    private static void CheckDimensions(double height, double waist) { 

     if (height <= 120) { 
      MessageBox.Show("Height must be greater than 120cm"); 
     } 

     if (waist <= 60) { 
      MessageBox.Show("Waist must be greater than 60cm"); 
     }  
    } 


    private void Gender_CheckedChanged(object sender, EventArgs e) { 
     button1.Enabled = true;       
    } 

    private static double CalculateRatio(double height, double waist) { 

     double finalratio = waist/height; 
     return finalratio; 

    } 



} 
} 

感謝,讓我知道,如果需要更多的信息。

回答

0

你沒有真正處理這個問題。 .Parse()假定如果該值無法轉換將拋出異常。 .TryParse()整點是爲了避免這一點。所以,你的方法需要被清理和編輯實際有所建樹:

private bool WaistNumericCheck(out double waist) 
{ 
    bool canParse = double.TryParse(heighttextbox.Text, out waist); 

    if (!canParse) { 
     MessageBox.Show("Value must be numeric"); 
    } 

    return canParse;      
} 

private bool HeightNumericCheck(out double height) 
{   
    // todo -> same pattern as above 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    double height = 0, waist = 0; 
    if(WaistNumericCheck(waist) && HeightNumericCheck(height)) 
    {      
     CheckDimensions(height, waist); 

     /* ... the rest of your code */ 
    }    
} 

這會修改你的測試方法返回一個真/基於解析是否成功僞造,並out是你的分析變量,以便你不必再解析它們。

我建議在某些時候,你查看MSDN文檔:http://msdn.microsoft.com/en-us/library/994c0zb1%28v=vs.110%29.aspx

+0

非常感謝您的答覆,這真的幫助! – user3496101 2014-09-14 03:28:05