文本框为空并且程序检测到

Text box is null and having the program detecting that

您好,我是事件驱动编程的新手,希望能得到一些帮助。

我已经安排了一些工作,我顺利完成了,但有一个缺点,在我的下面的代码中:我有一个文本框,如果输入文本,它会添加 2.00,然而它不断地增加 2.00 到总数。

我曾尝试使用 if 语句,但是没有用,我对整件事感到非常恼火。

代码不完整,因为我想尽可能自己完成。

decimal sizeCost = 0.00m;
const decimal extraSmall = 4.50m;
const decimal Small = 5.00m;
const decimal meduim = 5.50m;
const decimal large = 6.00m;
const decimal extraLarge = 7.00m;

string red;
string blue;
string white;
string green;
string purple;
string yellow;
string colour;

decimal fabCost = 0.00m;
const decimal cotton = 1.00m;
const decimal lycra = 2.50m;

int quanty = 0;
const decimal printingCost = 2.00m;

decimal cost = 0;
decimal totalCost1 = 0;
decimal totalCost = 0;
string surname = "";

private void buttonOrder_Click(object sender, EventArgs e)
{
    totalCost = sizeCost * quanty; 
    totalCost1 = fabCost * quanty;

   //fix this
   if (string.IsNullOrWhiteSpace(textBox1.Text))
   {
      cost = cost - 2.00m;
   }

   cost = fabCost + sizeCost + printingCost;
   labelDisplayCost.Text = cost.ToString("c");
   MessageBox.Show("The total cost is" + cost );
}

private void numericUpDownQuant_ValueChanged(object sender, EventArgs e)
{
    quanty = Convert.ToByte(numericUpDownQuant.Value);
}

这总是会火的,因为它永远是真的

//fix this
 if (string.IsNullOrWhiteSpace(textBox1.Text))
 {
    cost = cost - 2.00m;
 }

这不需要

//fix this
 if (!string.IsNullOrWhiteSpace(textBox1.Text))
 {
    cost = cost - 2.00m;
 }

!将反转命令以检查它是否有文本,然后您可以将 2.00 添加到成本中。

这相当于说

if(true)
{
//do something
}