Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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'
Posted
Updated 29-Sep-15 20:12pm
v2
Comments
VR Karthikeyan 30-Sep-15 1:56am    
Hi,

This error thrown because, we can't perform this addition between two object instances. To answer your question, just clarify the following,

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);

In the above code, tell me the type of "list". And also clarify about the types of
Price1
Price2
Price3...
VR Karthikeyan 30-Sep-15 1:58am    
I think,
[ Price1...Price5, ]
[ Description1...Description5, ]
[ Quantity1...Quantity5 ]
these are textboxes, am i right?

Use the following code, Use a flag to check empty textboxes,
C#
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();
}
 
Share this answer
 
v2
Comments
CPallini 30-Sep-15 2:51am    
5.
VR Karthikeyan 30-Sep-15 3:00am    
what do you mean by 5?
CPallini 30-Sep-15 3:15am    
My vote to your answer.
VR Karthikeyan 30-Sep-15 3:49am    
Thank you...
aarif moh shaikh 30-Sep-15 3:33am    
Good answer .. +5
Hey,
You can not apply operator(+) on TextBox directly.You need to use TextBox.Text property.
Thanks,
Sujay
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900