如何使用VS 2016创建平均使用率计算器? [英] How to create an average usage calculator using VS 2016?

查看:122
本文介绍了如何使用VS 2016创建平均使用率计算器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有按钮的情况下使用VS 2016创建平均使用率计算器,

i有四个文本框。

文本框1将是第一个账单。

文本框2将是第二个账单。

文本框3将是第3个账单。

文本框4将是第4个账单。 />


我希望它每天,每两周和每周计算一次。

每个文本框都有不同的解决方案。
$ b每天$ b。

每周。

每两周。



如果每天。总计将分为365,

,如果是每周,总数将被分为56,

,最后如果每两周,总数将被分为26。 />
谢谢!



我的尝试:



private void dailyaverage_TextChanged(object sender,EventArgs e)

{



int intbill1 = Convert.ToInt32(bill1.Text);

int intbill2 = Convert.ToInt32(bill2.Text);

int intbill3 = Convert.ToInt32(bill3.Text);

int intbill4 = Convert.ToInt32(bill4.Text);

int intaverage =(intbill1 + intbill2 + intbill3 + intbill4)/ 365;

int intfortnight =(intbill1 + intbill2 + intbill3 + intbill4)/ 26;

int intweekly =(intbill1 + intbill2 + intbill3 + intbill4)/ 52;



string average = Convert.ToString (收入);

string weekly = Convert.ToString(intweekly);

string fortnight = Convert.ToString(intfortnight);

dailyaverage.Text = average;

weeklyaverage.Text =每周;

forthnightlyaverage.Text =两周;



if(bill1.Text ==)

{

bill1.Text =(0);



}

if(b) bill2.Text ==)

{

bill2.Text =(0);

}

if(bill3.Text ==)

{

bill3.Text =(0);

}

if(bill4.Text ==)

{

bill4.Text =(0);

}

how to create an average usage calculator using VS 2016 without a button,
i have four text box.
text box 1 will be the 1st bill.
text box 2 will be the 2nd bill.
text box 3 will be the 3rd bill.
text box 4 will be the 4th bill.

and i want it to be computed as daily,fortnightly and weekly.
so different solution for each text box.
daily.
weekly.
fortnightly.

if daily. total will be divided to 365,
and if weekly, the total will be divided to 56,
and lastly if fortnightly,the total will be divided to 26.
thanks!

What I have tried:

private void dailyaverage_TextChanged(object sender, EventArgs e)
{

int intbill1 = Convert.ToInt32(bill1.Text);
int intbill2 = Convert.ToInt32(bill2.Text);
int intbill3 = Convert.ToInt32(bill3.Text);
int intbill4 = Convert.ToInt32(bill4.Text);
int intaverage = (intbill1 + intbill2 + intbill3 + intbill4) / 365;
int intfortnight = (intbill1 + intbill2 + intbill3 + intbill4) / 26;
int intweekly = (intbill1 + intbill2 + intbill3 + intbill4) / 52;

string average = Convert.ToString(intaverage);
string weekly = Convert.ToString(intweekly);
string fortnight = Convert.ToString(intfortnight);
dailyaverage.Text = average;
weeklyaverage.Text = weekly;
forthnightlyaverage.Text = fortnight;

if (bill1.Text == "")
{
bill1.Text = ("0");

}
if (bill2.Text == "")
{
bill2.Text = ("0");
}
if (bill3.Text == "")
{
bill3.Text = ("0");
}
if (bill4.Text == "")
{
bill4.Text = ("0");
}

推荐答案

首先,始终明智地标记 - 这意味着知道主题会回应。 Visual-studio和VS2016无关紧要 - 您需要使用语言标记,在本例中为C#。



其次,永远不要使用Convert for user输入:用户犯错误,转换将导致异常,当他们这样做时会使你的应用程序崩溃。

相反,使用TryParse:

First off, always tag sensibly - it means that people who know the subject will respond. "Visual-studio" and VS2016" are irrelevant - you need to tag with the language, in this case "C#" instead.

Second, never use Convert for user input: users make mistakes, and Convert will throiw an exception with will crash your app when they do that.
Instead, use TryParse:
int iBill1;
if (!int.Tryparse(bill1.Text, out iBill1))
   {
   ... report problem to user ...
   return;
   }
int iBill2;
if (!int.Tryparse(bill2.Text, out iBill2))
   {
   ... report problem to user ...
   return;
   }
...
int datilyAverage = (iBill1 + iBill2 + iBill3 + iBill4) / 365;
...



但是......不要更新dailyaverage TextChanged事件中的dailyaverage.Text!如果你这样做,你将立即得到另一个TextChanged事件 - 因为你刚刚更改了它 - 你将在TextBox中设置一个新值,这将导致另一个TextChanged事件,...



或许,您想要做的是在每个billX文本框发生更改时执行该代码,而不是在dailyaverage更改时执行该代码!



如果你这样做,只需在检测到错误时将dailyaverage.Text设置为--waiting--,这样用户在输入时就不会收到大量的消息!


But ... don't update dailyaverage.Text inside the dailyaverage TextChanged event! If you do, you will immediately get another TextChanged event - because you just changed it - and you will set a new value in the TextBox, which will cause another TextChanged event, which...

Probably, what you want to do is to execute that code whenever one of your billX textboxes changes, not when the dailyaverage changes!

And if you do, just set the dailyaverage.Text to "--waiting--" when you detect an error, so the user doesn't get loads of messages while he's typing!


这篇关于如何使用VS 2016创建平均使用率计算器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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