简体   繁体   English

C#忽略空的文本框

[英]C# ignore empty textboxes

I have ha a WinForms app with some textboxes . 我有一个带有某些textboxes的WinForms应用程序。 At the moment the app is simple and i only want to test how it will work. 目前该应用程序很简单,我只想测试它的工作方式。 It will get mutch more complex in the future. 将来它将变得更加复杂。 What i tried to do create a rule to ignore empty textboxes or if empty use the value 0. The code i have is this one: 我试图创建一个规则来忽略空textboxes ,如果为空则使用值0。我拥有的代码是以下代码:

    decimal ata;
    decimal a1;
    decimal a2;
    decimal a3;
    decimal a4;
    decimal a5;
    decimal j = 0;

    a1 = decimal.TryParse(textBox1.Text, out j);
    a2 = decimal.Parse(textBox2.Text);
    a3 = decimal.Parse(textBox4.Text);
    a4 = decimal.Parse(textBox10.Text);
    a5 = decimal.Parse(textBox24.Text);
    ata = a1 + a2 + a3 + a4 + a5;

resposta.Text = ata.ToString();

i get an error "Cannot implicitly convert type 'bool' to 'decimal' in the line: 我收到错误消息“无法在行中将类型'布尔'隐式转换为'十进制':

 a1 = decimal.TryParse(textBox1.Text, out j);

Can anyone help me with this problem. 谁能帮助我解决这个问题。

Thanks in advance, 提前致谢,

Use decimal.TryParse(textBox1.Text, out j); 使用decimal.TryParse(textBox1.Text, out j); instead of a1 = decimal.TryParse(textBox1.Text, out j); 而不是a1 = decimal.TryParse(textBox1.Text, out j); . Your value is returned in j 您的值在j返回

You declared 你宣布

decimal a1;

Then you tried: 然后您尝试了:

a1 = decimal.TryParse(textBox1.Text, out j);

decimal.TryParse returns a bool not a decimal decimal.TryParse返回布尔值而不是十进制数

j

Is taking the parsed value 正在获取解析的值

The TryParse method returns a bool value to indicate whether conversion succeeded or not. TryParse方法返回bool值,以指示转换是否成功。 The real value is returned in the second parameter ( out parameter). 实际值在第二个参数( out参数)中返回。

You could try: 您可以尝试:

if (Decimal.TryParse(textBox1.Text, out j))
   a1 = j;

This only sets a1 if parsing was successful. 仅当解析成功时才设置a1 Otherwise a1 keeps its previous value. 否则a1保持其先前值。

the return value TryParse is a bool . 返回值TryParse是一个bool It indicates whether the conversion was successful or not. 它指示转换是否成功。 Usually you would use it like this: 通常,您会这样使用它:

decimal j;

if (decimal.TryParse(textBox1.Text, out j))
{
    // here the conversion worked and it is save to use j
}

You check whether it was successful and in this case you can use the value of the converted variable, otherwise it will remain null . 您检查它是否成功,在这种情况下,您可以使用转换后的变量的值,否则它将保持为null

Here is the documentation 这是文档

In respect to the value that is passed via the out keyword the documentation says: ( s is equivalent to textBox1.Text and result is equivalent to j ) 关于通过out关键字传递的值,文档说:( s等效于textBox1.Textresult等效于j

When this method returns, result contains the Decimal number that is equivalent to the numeric value contained in s, if the conversion succeeded, or is zero if the conversion failed. 当此方法返回时,如果转换成功,则result包含与s中包含的数值等效的十进制数;如果转换失败,则为零。 The conversion fails if the s parameter is null or String.Empty, is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. 如果s参数为null或String.Empty,不是有效格式的数字,或者表示小于MinValue或大于MaxValue的数字,则转换失败。 This parameter is passed uninitialized; 该参数未初始化传递; any value originally supplied in result will be overwritten. 结果最初提供的任何值都将被覆盖。

The way you want to use the TryParse() method is like so 您想要使用TryParse()方法的方式是这样的

decimal a1 = 0;
bool parsed = decimal.TryParse(textBox1.Text, out a1);

if(parsed)
{
    //your arithmetic
}
else
{
    //throw error
}

The error is because TryParse() returns a boolean, not a decimal. 该错误是因为TryParse()返回的是布尔值,而不是十进制数。 You need to write explicit if statements to test if the textboxes are empty. 您需要编写显式的if语句来测试文本框是否为空。 You should consider wrapping this in a method. 您应该考虑将其包装在方法中。

You are trying to assign a bool value to a decimal variable that's why getting this error. 您试图将布尔值分配给十进制变量 ,这就是为什么会出现此错误的原因。 Decimal.TryParse() will return bool value, Please take a look here you can use this value to check parsing is successful or not. Decimal.TryParse()将返回bool值,请在这里看看您可以使用此值检查解析是否成功。

You will get result in your out parameter ie j. 您将在out参数(即j)中得到结果。

One of the nice things of object oriented programming is reusability of code in similar situations. 面向对象编程的优点之一是在类似情况下代码的可重用性。

You mentioned you are going to use some kind of "nullable decimal text boxes" a lot of times. 您提到您将多次使用某种“可空的十进制文本框”。 Why not create a class for it? 为什么不为它创建一个类呢? You'll only have to create the class once, and everyone who needs your nullable decimal text box (especially you with all your text boxes) can reuse this one code. 您只需要创建一次该类,并且每个需要可为空的十进制文本框(尤其是您所有的文本框)的人都可以重用此代码。

Bonus: if you ever need to change the behaviour of this kind of text boxes, for instance get a yellow background when empty, all your special text boxes instantaneously behave the same. 奖励:如果您需要更改此类文本框的行为,例如在空白时显示黄色背景,则所有特殊文本框的行为都将立即相同。

  • To make it possible to ignore empty text boxes you need a property IsEmpty 为了能够忽略空文本框,您需要一个属性IsEmpty
  • Property Value returns the Value of the text box as a decimal or 0M if IsEmpty 属性值以十进制或0M(如果IsEmpty)返回文本框的值
  • What do you want to return if not empty but text is not a decimal? 如果不为空但文本不是小数,您要返回什么?

    public class MyNullableTextBox : TextBox { public bool IsEmpty {get{return String.IsNullOrEmpty(base.Text);} } 公共类MyNullableTextBox:TextBox {公共布尔IsEmpty {获取{返回String.IsNullOrEmpty(base.Text);}}

     public decimal Value { get { if (this.IsEmpty) return 0M; else { decimal parsedValue; bool parsed = Decimal.TryParse(base.Text, out parsedValue); if (!parsed) // decide what you want in this case else return parsedValue; } } } 

    } }

The nice thing is, that once you've added this (as a reference) to your solution, it can be found in the Toolbox and used in your forms designer as if it was a regular text box 令人高兴的是,一旦将此(作为参考)添加到解决方案中,就可以在工具箱中找到它,并在表单设计器中使用它,就好像它是常规文本框一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM