捕获异常并重置windows窗体C#
我目前正在VisualStudio中使用windows窗体完成我的第一项作业,有点卡住了 我试图测试用户输入是否为数字,如果输入不是数字,会弹出一个小消息框并提醒用户。(我已经完成了这部分)。我遇到的问题是,错误仍然会在VisualStudio中出现 这是格式问题吗?我应该重新格式化我的代码吗?正如你可能看到的,我是C#(和编程)新手 我的代码:捕获异常并重置windows窗体C#,c#,forms,exception,C#,Forms,Exception,我目前正在VisualStudio中使用windows窗体完成我的第一项作业,有点卡住了 我试图测试用户输入是否为数字,如果输入不是数字,会弹出一个小消息框并提醒用户。(我已经完成了这部分)。我遇到的问题是,错误仍然会在VisualStudio中出现 这是格式问题吗?我应该重新格式化我的代码吗?正如你可能看到的,我是C#(和编程)新手 我的代码: using System; using System.Collections.Generic; using System.ComponentMode
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间wtgCalculator{
公共部分类Form1:Form{
常数双马来比=0.536;
常数双女性比率=0.492;
公共表格1(){
初始化组件();
}
私有void textBox1\u TextChanged(对象发送方,事件参数e){
}
私有void Form1\u加载(对象发送方、事件参数e){
}
私有无效标签1_单击(对象发送方,事件参数e){
}
私有void textBox1\u TextChanged\u 1(对象发送方,事件参数e){
}
私人数字支票(){
双腰;
字符串inpHeild=heighttextbox.Text;
if(双倍三角帆(内腰、外腰)){
MessageBox.Show(“值必须是数字”);//这就是问题所在
}
}
私有void HeightNumericCheck(){
//待办事项
}
私有无效按钮1\u单击(对象发送者,事件参数e){
//记录变量的输入
数字检查();
HeightNumericCheck();
double height=double.Parse(heighttextbox.Text);
double-sheeld=double.Parse(beartextbox.Text);
//检查输入是否在边界内
检查尺寸(身高、腰围);
//试验
//展示结果(身高、腰围);
}
私人无效支票限额(双高、双腰){
双倍结果=计算(身高、腰围);
如果(女性,选中){
如果(结果<女性比例){
MessageBox.Show(“你患肥胖相关心血管疾病的风险很低”);
}
如果(结果>女性比例){
MessageBox.Show(“你患心血管疾病相关肥胖的风险很高”);
}
}
如果(男性,选中){
如果(结果<比例){
MessageBox.Show(“你患肥胖相关心血管疾病的风险很低”);
}
如果(结果>比例){
MessageBox.Show(“你患肥胖相关心血管疾病的风险很高”);
}
}
//测试
Show(result.ToString());
}
私人空间展示结果(双高、双腰){
//双倍结果=计算(身高、腰围);
//如果(女性,选中){
//如果(结果<女性比例){
//MessageBox.Show(“你患肥胖相关心血管疾病的风险很低”);
// }
//如果(结果>女性比例){
//MessageBox.Show(“你患心血管疾病相关肥胖的风险很高”);
// }
//}
//如果(男性,选中){
//}
}
私人静态空隙检查尺寸(双高、双腰){
如果(height您没有真正正确地处理此问题。.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
s您解析的变量,这样您就不必再次解析它们
我建议您查看MSDN文档:非常感谢您的回复,这真的很有帮助!