2014-09-13 2 views
-1

저는 Visual Studio에서 Windows 양식을 사용하여 첫 번째 작업을하고 있으며 조금만 붙어 있습니다.예외 잡기 및 리셋 창 양식 C#

사용자 입력이 숫자인지 테스트하고, 입력이 숫자가 아니면 작은 메시지 상자가 나타나 사용자에게 경고합니다. (나는이 부분을 해왔다.) 내가 데 문제는 오류가 비주얼 스튜디오

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()이면 변환 할 수없는 경우이 예외를 throw한다고 가정합니다. .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