得到操作员错误c# [英] getting operator error c#

查看:80
本文介绍了得到操作员错误c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<textbox> list = new List<textbox>();

            //...
            list.Add(Price1);
            list.Add(Price2);
            list.Add(Price3);
            list.Add(Price4);
            list.Add(Price5);
            list.Add(Description1);
            list.Add(Description2);
            list.Add(Description3);
            list.Add(Description4);
            list.Add(Description5);
            list.Add(Quantity1);
            list.Add(Quantity2);
            list.Add(Quantity3);
            list.Add(Quantity4);
            list.Add(Quantity5);
            //...


            foreach (TextBox txt in list)
            {
                if (String.IsNullOrWhiteSpace(Description1.Text) == true)
                {
                    MessageBox.Show("Please provide a full set of inputs: Product description, product price,and product quantity.");
                }
                else
                {
                    Totalprice = Price1 + Price2 + Price3 + Price4 + Price5;
                    Totalquantity = Quantity1 + Quantity2 + Quantity3 + Quantity4 + Quantity5;
                    Grandtotal = Totalprice * Totalquantity;
                    Grandtotal.Text = Grandtotal.ToString();





获取:



getting:

Error	4	Operator '+' cannot be applied to operands of type 'System.Windows.Forms.TextBox' and 'System.Windows.Forms.TextBox'

推荐答案

使用以下代码,使用标志检查空文本框,

Use the following code, Use a flag to check empty textboxes,
bool IsAnyTextBoxEmpty =  false;
foreach (TextBox txt in list)
{
  if (String.IsNullOrWhiteSpace(txt.Text.Trim()) == true)
  {
     MessageBox.Show("Please provide a full set of inputs: Product description,  product price,and product quantity.");
     IsAnyTextBoxEmpty = true;
     txt.Focus();//to set cursor in empty textbox
  }
}

if(IsAnyTextBoxEmpty == true)
{
   double Totalprice = Convert.ToDouble(Price1.Text.Trim()) +
                       Convert.ToDouble(Price2.Text.Trim()) +
                       Convert.ToDouble(Price3.Text.Trim()) +
                       Convert.ToDouble(Price4.Text.Trim()) +
                       Convert.ToDouble(Price5.Text.Trim());
   double Totalquantity = Convert.ToDouble(Quantity1.Text.Trim()) +
                          Convert.ToDouble(Quantity2.Text.Trim()) +
                          Convert.ToDouble(Quantity3.Text.Trim()) +
                          Convert.ToDouble(Quantity4.Text.Trim()) +
                          Convert.ToDouble(Quantity5.Text.Trim());
   double Grandtotal = Totalprice * Totalquantity;
   Grandtotal.Text = Grandtotal.ToString();
}


嘿,

你不能直接在TextBox上应用运算符(+)。你需要使用TextBox .Text属性。

谢谢,

Sujay
Hey,
You can not apply operator(+) on TextBox directly.You need to use TextBox.Text property.
Thanks,
Sujay


这篇关于得到操作员错误c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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