获取C中文本框的等式 [英] Get an equation to a text box in C

查看:64
本文介绍了获取C中文本框的等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表格。计算bmi(我知道目前的表格不是怎么做的)



我一直在抛出异常。



我尝试过:



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间 BmiPatientInfo
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void btnSave_Click( object sender,EventArgs e)
{
string name = txtName.Text;
十进制重量;
decimal height;
decimal bmi;

weight = decimal .Parse(txtWeight.Text);
height = decimal .Parse(txtHeight.Text);
bmi =体重+身高;

bmi = decimal .Parse(txtBmi.Text);
}

private void btnExit_Click( object sender,EventArgs e)
{
this .Close();
}
}
}

解决方案

Decimal.Parse 可以抛出,你必须正确捕捉抛出的异常(例如向用户提示一个消息框)。



顺便说一下

Quote:

bmi = weight + height;

你知道这不是正确的BMI公式。



Quote:

bmi = decimal.Parse(txtBmi。文本);

计算bmi值然后立即使用文本框内容重新分配它的目的是什么?


不要使用 decimal.Parse ,使用 decimal.TryParse ,以便在转换失败时通知您。你还有两行:

 bmi = weight + height; 

bmi = decimal .Parse(txtBmi.Text);



你是确定第二行是正确的方式?或者它应该是这样的:

 txtBmi.Text =  string  .Format(  BMI:{0},bmi); 


< blockquote>你没告诉我们抛出了什么样的异常。



但是对于你发布的代码,在解析字符串以获取小数值时可能会抛出它。请参阅 Decimal.Parse Method(String)(System) [< a href =https://msdn.microsoft.com/en-us/library/cafs243z(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ] for可能抛出的异常。



还有一种转换方法不会抛出execptions但提供返回值来指示解析是否成功:Decimal.TryParse Method(System) [ ^ ]。



如果解析失败,您可以使用它代替并显示某种消息。


I have a form. To calculate bmi (I am aware the current form is not how to do it)

I keep getting exceptions thrown.

What I have tried:

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 BmiPatientInfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            decimal weight;
            decimal height;
            decimal bmi;

            weight = decimal.Parse(txtWeight.Text);
            height = decimal.Parse(txtHeight.Text);
            bmi = weight + height;

            bmi = decimal.Parse(txtBmi.Text);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

解决方案

Since Decimal.Parse can throw, you have to correctly catch the thrown exception (e.g. prompt a message box to the user).

By the way

Quote:

bmi = weight + height;

You know this is NOT the right BMI formula.

Quote:

bmi = decimal.Parse(txtBmi.Text);

What's the purpose of computing the bmi value and then immediately reassign it using the text box content?


Do not use decimal.Parse, use decimal.TryParse, so you are notified if the conversion fails. You also have two lines:

bmi = weight + height;
 
bmi = decimal.Parse(txtBmi.Text);


Are you sure the second line is the right way round? Or should it be something like:

txtBmi.Text = string.Format("BMI: {0}", bmi);


You did not tell us what kind of exception is thrown.

But for your posted code it is probably thrown when parsing the strings to get decimal values. See Decimal.Parse Method (String) (System)[^] for the exceptions that might be thrown.

There is also a conversion method that does not throw execptions but provides a return value to indicate if parsing was successful: Decimal.TryParse Method (System)[^].

You might use this instead and show some kind of message if parsing fails.


这篇关于获取C中文本框的等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆