如何添加textbox2和textbox4的值? [英] How can I add the value of textbox2 and textbox4?

查看:80
本文介绍了如何添加textbox2和textbox4的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加textbox2和textbox4的值,当我点击button1 total时,总和将显示在textbox9中。



我应该在button1中输入什么代码?



我尝试了什么:



 命名空间登录
{
public partial class Form3:Form
{
public Form3()
{
InitializeComponent();
}

private void button2_Click( object sender,EventArgs e)
{
// 清除文本框和总标签/
textBox2.Text = ;
textBox4.Text = ;
textBox9.Text = ;
textBox1.Text = ;
textBox3.Text = ;
// 重置焦点
textBox9.Focus();
textBox2.Focus();
textBox4.Focus();
textBox3.Focus();
textBox1.Focus();

}

private void button4_Click(< span class =code-keyword> object sender,EventArgs e)
{
DialogResult iLogOut;

iLogOut = MessageBox.Show( 你要退出吗? 离开?,MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (iLogOut == DialogResult.Yes)
{
Application.Exit();
}
}

private void button5_Click( object sender,EventArgs e)
{
try
{
const double fries_small = 25 ;
const double fries_large = 40 ;
const double fries_bucket = 70 ;
double friesQuantity;
double friesTotal; // fries amount * cost
friesQuantity = INT .Parse(textBox1.Text);
if (checkBox1.Checked == true
{
friesTotal = friesQuantity * fries_small;
textBox2.Text = friesTotal.ToString( c);
}
else if (checkBox2.Checked == true
{
friesTotal = friesQuantity * fries_large;
textBox2.Text = friesTotal.ToString( c);
}
else if (checkBox3.Checked == true
{
friesTotal = friesQuantity * fries_bucket;
textBox2.Text = friesTotal.ToString( c);
}

else
{
MessageBox.Show( 请填写订单);
}
}

catch (例外)
{
MessageBox.Show(< span class =code-string>
请填写订单);
}
}



private void Form3_Load( object sender,EventArgs e)
{
MessageBox.Show( 提供原味的薯条和价格实惠的饮品,立即订购!);
}

private void button6_Click( object sender,EventArgs e)
{
try
{
const double bev_water = 20 ;
const double bev_icedtea = 25 ;
const double bev_coke = 30 ;
double bevQuantity = int .Parse(textBox3.Text);
double beveragesTotal; // 饮料金额*费用

if (checkBox4.Checked == true
{
beveragesTotal = bevQuantity * bev_water;
textBox4.Text = beveragesTotal.ToString( c);
}
else if (checkBox5.Checked == true
{
beveragesTotal = bevQuantity * bev_icedtea;
textBox4.Text = beveragesTotal.ToString( c);
}
else if (checkBox6.Checked == true
{
beveragesTotal = bevQuantity * bev_coke;
textBox4.Text = beveragesTotal.ToString( c);
}
else
{
MessageBox.Show( 请填写订单);
}
}
catch (例外)
{
MessageBox.Show( 请填写订单);
}
}

private void button1_Click( object sender,EventArgs e)
{

}

}

}

解决方案

Hi Sheena,



代码本身需要一个很多好的重塑。



你需要做的第一件事是给文本框和按钮一个好名字。请记住,一个好名字应该充分描述他们所采取的行动;它减少了评论的工作。



建议例如:<$ p$p> button2 - >重命名为'ResetBtn'
button4 - >重命名为'ExitBtn'
button5 - >重命名为'FriesOrderBtn'
button6 - >重命名为'BeveragesOrderBtn'
button1 - >重命名为'TotalPriceBtn'
checkBox1 - >重命名为'SmallFriesCheckBox'
.....................
................ .....
等。

以下代码在文本框之间进行不必要的跳转:

 //重置焦点
textBox9.Focus();
textBox2.Focus();
textBox4.Focus();
textBox3.Focus();
textBox1.Focus();

这可能只是最后一行;前四行导致控件之间不必要的跳转。

 textBox1.Focus(); 

您需要做的另一件事是将所有常量放在代码的顶部;如果你把它们放在方法中,它看起来不专业。常量是在编译时创建的,而不是在运行时创建的。它不像是你移动到方法时创建的;它们是在程序开始时创建的。



另一个建议是,将代码包装在 try-catch 块。你永远不知道在运行时可能会发生什么。



最后,你手上的问题 - 添加薯条和饮料的价格。您已经使用过该解决方案(int.Parse(textBox1.Text)),您可以使用相同的。

 private void button1_Click(object sender,EventArgs e)
{
// Check如果计算薯条和饮料的价格。如果没有那么返回。
if(string.IsNullOrEmpty(textBox2.Text)|| string.IsNullOrEmpty(textBox4.Text))
return;

try
{
int Total = int.Parse(textBox2.Text)+ int.Parse(textBox4.Text);
textBox9.Text = Total.ToString();
}
catch(FormatException Ex)
{
MessageBox.Show(期待薯条和饮料价格框中的数字。
错误消息:+ Ex。信息);
}
}


1。将货币字符串转换为数字

2.将两个数字相加

3.将其转换回货币



  double  drinksTotal,friesTotal; 

double .TryParse(textBox4.Text,System.Globalization.NumberStyles.Currency,
System.Globalization.CultureInfo.CurrentCulture。 NumberFormat, out beveragesTotal);

double .TryParse(textBox2.Text,System.Globalization.NumberStyles.Currency,
System.Globalization.CultureInfo.CurrentCulture。 NumberFormat, out friesTotal);

textBox9.Text =(beveragesTotal + friesTotal).ToString( c);


首先将文本框值转换为数值(如int,double。)

Ex- int val1 = Convert。 ToInt32(TextBox2.Text);

int val2 = Convert.ToInt32(TextBox2.Text);



第二次添加这些值...使用临时变量

ex- int result = val1 + val2;



三将此结果分配到TextBox

Ex- TextBox3.Text = result.ToString();





仅限一行



TextBox3.Text =(Convert.ToInt32(TextBox2.Text)+ Convert.ToInt32(TextBox2.Text));


I want to add the values of textbox2 and textbox4 and the sum will show in textbox9 when I click the button1 total.

what code should I put in button1?

What I have tried:

namespace LogIn
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Clear the Textboxes and the total labels/
            textBox2.Text="";
            textBox4.Text="";
            textBox9.Text = "";
            textBox1.Text="";
            textBox3.Text="";
            // reset the focus
            textBox9.Focus();
            textBox2.Focus();
            textBox4.Focus();
            textBox3.Focus();
            textBox1.Focus();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult iLogOut;

            iLogOut = MessageBox.Show("Do you want to Log Out?", "Leave?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (iLogOut == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                const double fries_small = 25;
                const double fries_large = 40;
                const double fries_bucket = 70;
                double friesQuantity;
                double friesTotal; // fries amount * cost
                friesQuantity = int.Parse(textBox1.Text);
                if (checkBox1.Checked == true)
                {
                    friesTotal = friesQuantity * fries_small;
                    textBox2.Text = friesTotal.ToString("c");
                }
                else if (checkBox2.Checked == true)
                {
                    friesTotal = friesQuantity * fries_large;
                    textBox2.Text = friesTotal.ToString("c");
                }
                else if (checkBox3.Checked == true)
                {
                    friesTotal = friesQuantity * fries_bucket;
                    textBox2.Text = friesTotal.ToString("c");
                }
                
                else
                {
                    MessageBox.Show("Fill the order form please");
                }
            }

            catch (Exception)
            {
                MessageBox.Show("Please fill out the order form");
            }
        }



        private void Form3_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Original flavored fries and affordable drinks available, Order Now!");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
        const double bev_water = 20;
        const double bev_icedtea = 25;
        const double bev_coke = 30;
        double bevQuantity = int.Parse(textBox3.Text);
        double beveragesTotal; // beverage amount * cost
        
            if (checkBox4.Checked == true)
                {
                    beveragesTotal = bevQuantity * bev_water;
                    textBox4.Text = beveragesTotal.ToString("c");
                }
                else if (checkBox5.Checked == true)
                {
                    beveragesTotal = bevQuantity * bev_icedtea;
                    textBox4.Text = beveragesTotal.ToString("c");
                }
                else if (checkBox6.Checked == true)
                {
                    beveragesTotal = bevQuantity * bev_coke;
                    textBox4.Text = beveragesTotal.ToString("c");
                }
             else
                {
                    MessageBox.Show("Fill the order form please");
                }
            }
             catch(Exception)
            {
                MessageBox.Show("Please fill out the order form");
             }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        }

    }

解决方案

Hi Sheena,

The code itself needs a lot of good reshaping.

The first thing you need to do is give a good name to the textboxes and buttons. Remember, a good name should sufficiently describe the actions they are up to; it reduces commenting efforts.

Suggestions E.g.:

button2 -> Rename as 'ResetBtn'
button4 -> Rename as 'ExitBtn'
button5 -> Rename as 'FriesOrderBtn'
button6 -> Rename as 'BeveragesOrderBtn'
button1 -> Rename as 'TotalPriceBtn'
checkBox1 -> Rename as 'SmallFriesCheckBox'
.....................
.....................
etc.

The following code makes unnecessary jumps between the textboxes:

// reset the focus
textBox9.Focus();
textBox2.Focus();
textBox4.Focus();
textBox3.Focus();
textBox1.Focus();

This can simply be just the last line; the first four lines are causing unnecessary trips between the controls.

textBox1.Focus();

Another thing you need to do is bring all constants at the top of the code; it looks unprofessional if you put them inside methods. Constants are created at compile time, not at runtime. It is not like they are created when you move to the method; they are created at the start of the program.

Another suggestion is, wrap your code in try-catch blocks. You never know what might happen in runtime.

Finally, the problem at your hand - adding the price of fries and beverages. You already used the solution (int.Parse(textBox1.Text)), you can use the same.

private void button1_Click(object sender, EventArgs e)
{
	// Check if prices for fries and beverages are calculated yet. If not then return.
	if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox4.Text))
        return;

	try
    {
        int Total = int.Parse(textBox2.Text) + 	int.Parse(textBox4.Text);
	    textBox9.Text = Total.ToString();
    }
    catch (FormatException Ex)
    {
        MessageBox.Show("Expecting numbers in the price boxes of fries and beverages.
                         Error msg: " + Ex.Message);
    }
}


1. convert the currency string into number
2. sum the two number
3. convert it back into currency

double beveragesTotal, friesTotal;

double.TryParse(textBox4.Text, System.Globalization.NumberStyles.Currency, 
System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out beveragesTotal);

double.TryParse(textBox2.Text, System.Globalization.NumberStyles.Currency, 
System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out friesTotal);

textBox9.Text = (beveragesTotal + friesTotal).ToString("c");


First Convert textbox value into numeric value(like int,double.)
Ex- int val1 = Convert.ToInt32(TextBox2.Text);
int val2 = Convert.ToInt32(TextBox2.Text);

Second Add those value...using a temp variable
Ex- int result = val1+val2;

Third Assign this result into TextBox
Ex- TextBox3.Text= result.ToString();

Or
In Only One Line

TextBox3.Text = (Convert.ToInt32(TextBox2.Text) + Convert.ToInt32(TextBox2.Text));


这篇关于如何添加textbox2和textbox4的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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