将文本框之间的数字相乘 [英] Multiply numbers between textboxes

查看:118
本文介绍了将文本框之间的数字相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从textBox5中删除数字时,代码错误输出;

int R = Convert.ToInt32(textBox4.Text);

// int I =转换。 ToInt32(textBox5.Text);



// int E = R * I;



// textBox6.Text = E.ToString();



我尝试的第二种方式如下:当我删除号码并尝试输入一个新号码时,两个代码都出错了。



int R = int.Parse(textBox4.Text);

int I = int.Parse(textBox5.Text);



int E = R * I;



textBox6.Text = E.ToString();



我尝试了什么:



描述中的两种方式

When I remove number from textBox5, the code errors out;
int R = Convert.ToInt32(textBox4.Text);
//int I = Convert.ToInt32(textBox5.Text);

//int E = R * I;

//textBox6.Text = E.ToString();

The second way I tried is below: Both codes error out when I remove number and try to put in a new number.

int R = int.Parse(textBox4.Text);
int I = int.Parse(textBox5.Text);

int E = R * I;

textBox6.Text = E.ToString();

What I have tried:

The two ways in the description

推荐答案

Convert.ToInt32和int.Parse仅适用于您知道文本可以转换为int的情况,因为您正在处理用户输入,您不知道是否它是有效的,所以使用int.Tr相反,yParse。



Convert.ToInt32 and int.Parse are only suitable if you know the text can be converted to an int, as you are working with user input you don't know if it is valid, so use int.TryParse instead.

int R = 0;
int I = 0;

if (!int.TryParse(textBox4.Text.Trim(), out R))
{
    // if the code gets here the conversion to int failed
    // you can show a message to the user, or give R a default value
    // do whatever your code requires

    R = 0;
}

if (!int.TryParse(textBox5.Text.Trim(), out I))
{
    // if the code gets here the conversion to int failed
    // you can show a message to the user, or give I a default value
    // do whatever your code requires

    I = 0;
}

int E = R * I;


从文本框中删除数字时,则为空string被传递给 ToInt32 方法,该方法引发错误,因为它无法将空字符串转换为整数值。

Int32.Parse 方法也可以。

你有几个选择:

- 允许在TextBox中输入空字符串并解释空字符串作为默认值零。

- 处理TextBox的验证事件,并且在有整数值之前不允许松散焦点那里(不是非常用户友好)。

- 有一个错误提供者发现缺失值。



有一条规则永远不会盲目信任用户输入,总是假设会有输入错误,因此代码可以处理所有测试用例。 TryParse 方法可能是测试输入值的不错选择。无论如何,比转换类更好的选择,这对于手头的任务来说不是一个非常好的工具。



希望这会有所帮助。
When you remove the number from the textbox, then an empty string is passed to the ToInt32 method, which raises the error because it cannot convert an empty string to a integer value.
Int32.Parse method does as well.
You have several options:
- allow input of empty strings in the TextBox and interpret empty strings as a default value of zero.
- handle the Validating event of the TextBox and do not allow to loose focus until there is an integer value in there (not very user friendly).
- have an error provider spotting the missing value.

There is one rule that is never blindly trust user inputs, always assume there will be input errors, so code to handle all test cases. The TryParse method could be a good choice to test for input values. A better option than the Convert class which is not a very good tool for the task at hand, anyway.

Hope this helps.


这篇关于将文本框之间的数字相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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