windows表单应用程序C#帮助 [英] windows form application C# help

查看:60
本文介绍了windows表单应用程序C#帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于创建一个工资单表格,用户可以输入他们每周工作的小时数,他们每小时赚多少钱,我想让计划确定他们在税前和税后赚多少钱。



但是我从小时文本框和每小时文本框中获取一个数字值,然后在答案标签中显示它,我得到的代码是:



txt_hours.Text = hours.ToString();

hours = Convert.ToInt32(txt_hours.Text);

hours = int.Parse(txt_hours.Text);





txt_hourlyr.Text = hourlyrate.ToString();

hourlyrate = Convert.ToInt32(txt_hourlyr.Text);

hourlyrate = int.Parse(txt_hourlyr.Text);



answer = Convert.ToString(小时*每小时);



answer = Convert.ToString(小时*小时);





有人可以帮我吗







更新:



i现在已经刮掉了那段代码,因为我走向了错误的方向,但我现在遇到的问题是答案=小时*每小时;不会将int类型转换为字符串



int hours;

int hourlyrate;

string answer; < br $>


hours = int.Parse(txt_hours.Text);

hourlyrate = int.Parse(txt_hourlyr.Text);



答案=小时*每小时;

lbl_weeklypay.Text = answer.ToString();< / pre>

i have been working on creating a payslip form where users can input how many hours they work a week, how much they earn an hour and i want to make the program work out how much they earn before and after taxes.

but i am having problems with getting a number value from the hours textbox and hourlyrate textbox and then displaying it in a the answer label the following code i have got is:

txt_hours.Text = hours.ToString();
hours = Convert.ToInt32(txt_hours.Text);
hours = int.Parse(txt_hours.Text);


txt_hourlyr.Text = hourlyrate.ToString();
hourlyrate = Convert.ToInt32(txt_hourlyr.Text);
hourlyrate = int.Parse(txt_hourlyr.Text);

answer = Convert.ToString(hours * hourlyrate);

answer = Convert.ToString(hours * hourlyrate);


can anyone help me please



UPDATE:

i have now scraped that code as i was going into the wrong direction, but the problem i have now is the "answer = hours * hourlyrate;" does not implicity convert type int to string

int hours;
int hourlyrate;
string answer;

hours = int.Parse(txt_hours.Text);
hourlyrate = int.Parse(txt_hourlyr.Text);

answer = hours * hourlyrate;
lbl_weeklypay.Text = answer.ToString();</pre>

推荐答案

哦......这看起来都像是真正的胡言乱语。看看你在做什么。在第一行中,您假设您有一些值 hours ,可能是 int 类型。在下一行中,您将从 Text 属性获取您获得的字符串,并将其转换回 int 。没有任何意义,你做了一次往返,充其量也可以获得同样的价值。好像它还不够,你再次将字符串转换为小时,因此初始小时值和前一行中获得的值都将被丢弃。非常好。基本逻辑怎么样?



另请注意 int.Parse 可以抛出和异常(但不是在这种情况下)你无意义的往返旅行)。我们无法确定您是否在正确的位置处理此异常,但建议您使用 int.TryParse 。这是将字符串解析为任何数值的方法;相反的操作是 ToString



我想,这就足够了;下面的行不是更好。我已经解释了要做什么,但为了真正帮助你,我会建议:停止做你正在做的事情。坚持使用UI和其他高级图标。获取一些关于语言,平台和通用编程的手册,并从头开始阅读所有内容,随时进行最简单的练习,更好的一些简单的仅限控制台的应用程序和一些类库程序集(总是使用它们和测试用法)。只有当你对此感到相当舒服时,再进行一轮,这次才能获得更高级的topis。等等...



-SA
Oh… It all looks like real gibberish. Look what you are doing. In first line, you assume that you have some value, hours, probably of the type int. In next line, you take the string you obtained, by some weird reason, from Text property, and convert it back to int. It makes no sense, you made a round trip and, at best, could get the same value. As if it wasn't enough, you convert the string to hours once again, so both the initial hours value and the value obtained in the previous line are discarded. Very nice. How about elementary logic?

Also note that int.Parse can throw and exception (but not in the case of your pointless round trip). We cannot see if you handle this exception in right place, but you are advised to use int.TryParse instead. This is how you parse a string into any numeric value; and the opposite operation would be ToString.

I think, this is enough; the lines below are no better. I already explained what to do, but to really help you, I would advise: stop doing what you are doing. Hold on with UI and other advanced icons. Grab some manual on language, platform and general programming and read it all, from the very beginning, doing simplest possible exercises as you go, better some simple console-only applications and some class library assemblies (always use them and test usage). Only when you feel quite comfortable with that, make another round, this time getting to a bit more advanced topis. And so on…

—SA


解决问题不要隐含转换键入int to string只需将 answer 的类型更改为 int



考虑使用float,double或decimal代替支持非整数输入。



.. 。并使用TryParse检查非数字输入。
To fix the problem with "does not implicity convert type int to string " just change the type of answer to int.

Consider also use float, double or decimal instead to support non-integer input.

... and use TryParse to check for non-numeric input.


这篇关于windows表单应用程序C#帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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