Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本框中删除数字会产生错误_C#_Textbox_Unhandled Exception - Fatal编程技术网

C# 从文本框中删除数字会产生错误

C# 从文本框中删除数字会产生错误,c#,textbox,unhandled-exception,C#,Textbox,Unhandled Exception,我有一个文本框,我想在其中放置一个数字,程序应该通过将其转换为十进制来读取它,但是,在对该数字执行所需的数学运算后,如果我将其从文本框中删除,它会立即产生错误: 未处理格式异常(输入字符串格式不正确) 这发生在我试图将文本转换为十进制的行上 private void readW_TextChanged(object sender, EventArgs e) { string _W = readW.Text; _Wd = Convert.ToDecimal(_W); } 听起来你

我有一个
文本框
,我想在其中放置一个数字,程序应该通过将其转换为十进制来读取它,但是,在对该数字执行所需的数学运算后,如果我将其从
文本框
中删除,它会立即产生错误:

未处理格式异常(输入字符串格式不正确)

这发生在我试图将文本转换为十进制的行上

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal(_W);
}

听起来你这样做是因为数字不能转换成十进制。毫不奇怪,这会导致转换失败。尝试改用
Decimal.TryParse

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    Decimal.TryParse(_W, out _Wd);
}
这将防止转换失败时出现异常。它还将返回一个bool,只有在转换成功时,才能有条件地使用该bool执行其他操作,例如:

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    if(Decimal.TryParse(_W, out _Wd))
    {
        Console.WriteLine("Valid decimal entered!");
    } 
}

听起来你这样做是因为数字不能转换成十进制。毫不奇怪,这会导致转换失败。尝试改用
Decimal.TryParse

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    Decimal.TryParse(_W, out _Wd);
}
这将防止转换失败时出现异常。它还将返回一个bool,只有在转换成功时,才能有条件地使用该bool执行其他操作,例如:

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    if(Decimal.TryParse(_W, out _Wd))
    {
        Console.WriteLine("Valid decimal entered!");
    } 
}

听起来你这样做是因为数字不能转换成十进制。毫不奇怪,这会导致转换失败。尝试改用
Decimal.TryParse

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    Decimal.TryParse(_W, out _Wd);
}
这将防止转换失败时出现异常。它还将返回一个bool,只有在转换成功时,才能有条件地使用该bool执行其他操作,例如:

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    if(Decimal.TryParse(_W, out _Wd))
    {
        Console.WriteLine("Valid decimal entered!");
    } 
}

听起来你这样做是因为数字不能转换成十进制。毫不奇怪,这会导致转换失败。尝试改用
Decimal.TryParse

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    Decimal.TryParse(_W, out _Wd);
}
这将防止转换失败时出现异常。它还将返回一个bool,只有在转换成功时,才能有条件地使用该bool执行其他操作,例如:

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    if(Decimal.TryParse(_W, out _Wd))
    {
        Console.WriteLine("Valid decimal entered!");
    } 
}
你得到

未处理格式异常(输入字符串格式不正确)

因为
string.Empty
无法转换为
decimal

如果解析失败,您可以使用通知:

bool success = decimal.TryParse(_W, out _Wd);
if (success) {
    // Use the result
}
else {
    // Depending on your needs, do nothing or show an error
}
请注意,
\u W
字符串。空的
可能是您希望忽略的条件,而其他解析失败可能会导致错误消息。如果是这样,您的
else
可能看起来像

else {
    if (!string.IsNullOrEmpty(_W)) ShowAnErrorMessageSomehow();
}
你得到

未处理格式异常(输入字符串格式不正确)

因为
string.Empty
无法转换为
decimal

如果解析失败,您可以使用通知:

bool success = decimal.TryParse(_W, out _Wd);
if (success) {
    // Use the result
}
else {
    // Depending on your needs, do nothing or show an error
}
请注意,
\u W
字符串。空的
可能是您希望忽略的条件,而其他解析失败可能会导致错误消息。如果是这样,您的
else
可能看起来像

else {
    if (!string.IsNullOrEmpty(_W)) ShowAnErrorMessageSomehow();
}
你得到

未处理格式异常(输入字符串格式不正确)

因为
string.Empty
无法转换为
decimal

如果解析失败,您可以使用通知:

bool success = decimal.TryParse(_W, out _Wd);
if (success) {
    // Use the result
}
else {
    // Depending on your needs, do nothing or show an error
}
请注意,
\u W
字符串。空的
可能是您希望忽略的条件,而其他解析失败可能会导致错误消息。如果是这样,您的
else
可能看起来像

else {
    if (!string.IsNullOrEmpty(_W)) ShowAnErrorMessageSomehow();
}
你得到

未处理格式异常(输入字符串格式不正确)

因为
string.Empty
无法转换为
decimal

如果解析失败,您可以使用通知:

bool success = decimal.TryParse(_W, out _Wd);
if (success) {
    // Use the result
}
else {
    // Depending on your needs, do nothing or show an error
}
请注意,
\u W
字符串。空的
可能是您希望忽略的条件,而其他解析失败可能会导致错误消息。如果是这样,您的
else
可能看起来像

else {
    if (!string.IsNullOrEmpty(_W)) ShowAnErrorMessageSomehow();
}

请尝试此代码。但请确保用户只能在文本框中键入数字。谢谢

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal("0"+_W);
}

请尝试此代码。但请确保用户只能在文本框中键入数字。谢谢

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal("0"+_W);
}

请尝试此代码。但请确保用户只能在文本框中键入数字。谢谢

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal("0"+_W);
}

请尝试此代码。但请确保用户只能在文本框中键入数字。谢谢

private void readW_TextChanged(object sender, EventArgs e)
{
    string _W = readW.Text;
    _Wd = Convert.ToDecimal("0"+_W);
}

但是,当我使用“Decimal.TryParse(_W,out _Wd);”时,它会将文本转换为十进制,就像前面的代码一样,只是添加了TryParse的函数,对吗?@CésarAmorim您根本不需要使用
convert.ToDecimal
——TryParse执行与
convert.ToDecimal
相同的转换,除了它不会抛出
FormatExceptions
,返回值是一个
bool
,告诉您转换是否成功。但是当我使用“Decimal.TryParse(_W,out _Wd);”时,它会像前面的代码一样将文本转换为十进制,只是添加了TryParse函数,这是正确的吗?@CésarAmorim您根本不需要使用
Convert.ToDecimal
-TryParse执行与
Convert.ToDecimal
相同的转换,只是它不会抛出
FormatExceptions
,返回值是一个
bool
,告诉您转换是否成功。但是当我使用“十进制.锥巴色(_W,out _Wd);“它将文本转换为十进制,就像前面的代码一样,只是添加了TryParse函数,对吗?@CésarAmorim您不需要使用
convert.ToDecimal
——TryParse执行与
convert.ToDecimal
相同的转换,只是它不会抛出
FormatExceptions
和返回值is a
bool
告诉您转换是否成功。但是当我使用“Decimal.TryParse(\u W,out\u Wd);“它将文本转换为十进制,就像前面的代码一样,只是添加了TryParse函数,对吗?@CésarAmorim您不需要使用
convert.ToDecimal
——TryParse执行与
convert.ToDecimal
相同的转换,只是它不会抛出
FormatExceptions
和返回值i这是鳕鱼