C#窗体中添加文本框值 [英] Textbox value addition in C# windows form

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

问题描述

我有18个文本框

前3个文本框工资+金钱=总额

这是12个文本框所有扣除的总和

另一个文本框的另一个等式

工资 - 所有扣除额的总和=答案

然后回答/ 2 =第二季度

第二季度+钱=第一季

请帮助我新的c#



我尝试过的事情:



i have 18 text box
1st 3 text box for salaries + money = gross
the is 12 textbox the sum of all deduction
the anothe equation for the other textbox
salaries - sum of all deduction = answer
then answer / 2 = 2nd quarter
2nd quarte + money = 1st quarter
please help im new in c#

What I have tried:

private void button6_Click(object sender, EventArgs e)
{

    {

        double ro = Convert.ToDouble(textBox2.Text);
        double se = Convert.ToDouble(textBox3.Text);
        double ny = Convert.ToDouble(textBox4.Text);
        double re = Convert.ToDouble(textBox5.Text);
        double yn = Convert.ToDouble(textBox6.Text);
       double ar = Convert.ToDouble(textBox7.Text);
        double ac = Convert.ToDouble(textBox8.Text);
        double ce = Convert.ToDouble(textBox9.Text);
        double cu = Convert.ToDouble(textBox10.Text);
        double na = Convert.ToDouble(textBox11.Text);
        double so = Convert.ToDouble(textBox12.Text);
        double my = Convert.ToDouble(textBox13.Text);
        double ma = Convert.ToDouble(textBox14.Text);
        double ly = Convert.ToDouble(textBox15.Text);
        double la = Convert.ToDouble(textBox16.Text);
        double da = Convert.ToDouble(textBox17.Text);
        double ha = Convert.ToDouble(textBox18.Text);
        double za = Convert.ToDouble(textBox19.Text);
        if (textBox17.Text == "")
        {

            textBox17.Text = (re + yn + ar + ac + ce + cu + na + so + la + ma + ly + my).ToString();
        }
        if (textBox4.Text == "")
        {
            textBox4.Text = (se + ro).ToString();
            {
                if (textBox18.Text == "")
                {
                    textBox18.Text = (za + se).ToString();
                    {

                       if (textBox19.Text == "")
                       {
                            textBox19.Text = ((da - ny) / 2).ToString();
                           {


                            }
                        }

推荐答案

您的主要问题是,如果任何文本框为空白,您将收到错误:

Your main issue is that you will get an error if any of the textboxes are blank:
Quote:

An mscorlib.dll中发生类型'System.FormatException'的未处理异常

附加信息:输入字符串的格式不正确。

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.



不要使用 Convert.ToDouble (或者如果您打算使用它,请先检查文本框的内容)。



更好的方法是


Don't use Convert.ToDouble(or if you are going to use it then check the contents of the textbox first).

A better way would be

double ro,se,ny,re,yn,ar,ac,ce,cu;
double na,so,my,ma,ly,la,da,ha,za;

double.TryParse(textBox2.Text, out ro);
double.TryParse(textBox3.Text, out se);
double.TryParse(textBox4.Text, out ny);
// ... etc.



之后不清楚你要做什么,但要注意你只会更新 TextBox18 的文本,如果 TextBox4 空白。



尽量避免在不需要的地方使用额外的括号...它可以使您的代码更难以阅读,并且没有充分理由将代码推送到页面右侧。


It's not clear what you are trying to do after that, but be aware that you will only update the text for TextBox18 if TextBox4 is also blank.

Try to avoid extra brackets where they are not needed ...It can make your code more difficult to read and pushes the code to the right of the page for no good reason.

if (textBox4.Text == "")
{
    textBox4.Text = (se + ro).ToString();
    { // There is no need for this set of brackets
        if (textBox18.Text == "")
        {
            textBox18.Text = (za + se).ToString();
            { // There is no need for this set of brackets

               if (textBox19.Text == "")
               {
                    textBox19.Text = ((da - ny) / 2).ToString();
                   {


                    }
                }


这篇关于C#窗体中添加文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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