2 Replies - 1586 Views - Last Post: 14 June 2011 - 06:30 AM Rate Topic: -----

#1 Jack_Valentine   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-June 11

adding new price values to old one in tetxbox in C#

Posted 13 June 2011 - 10:15 AM

Gurus, please am new to C# programming and right now, am having a problem with adding new price values to an old one and using this to update a TotalSales TextBox.
the aim is to keep adding up new price values of ordered items to the old one previously in the textbox (no matter the amount of time orders were made) so that the gross price of the Total sales can at the end of a day can be seen from the TotalSales
textbox at the end of a daily sales period. i've tried to use the (+=) method but it doesn't add new values to old ones instead, it adds them to the textbox with a coma separating them. i included the relevant part of the code below.

if (boxes[i].Checked == true)

                    select += SecondMenuList[i].ToString() + "   :  " + "\t\t" +
                        Check[i].ToString("c") + "\r\n";

                    Price += Check[i];

                    TotalPrice = Price.ToString("c");

                    txtTotalPrice.Text += TotalPrice;

the second queston:

The gist is this way:

string[] SecondMenuList = new string[6];

SecondMenuList = {"carrier", "ball", "copper", "batton", "Krest", "Yatch"};

is declared as the main array.

Then the stream of inputs from a microcontroller read from the serialport buffer

is arranged into an array with the code;

string[] PortVal = { port.ReadExisting() };

of which PortVal should be in the form:

PortVal = {cop,yat,car,bat};

after this is done, i now want to compare the elements present in PortVal at any

index to see if they match the first three alphabet letters

of the elements in the SecondMenuList array at any index of the array:

 for (int i = 0; i < 6; i++)
                    if (PortVal[i] == SecondMenuList[i])//but this only checks if the elements are equal
                        //lines of code

so please if there is anyway to work this through, help me out.


edit: Please, use code tags when posting code.


This post has been edited by Curtis Rutland: 13 June 2011 - 10:25 AM

Is This A Good Question/Topic? 0
  • +

Replies To: adding new price values to old one in tetxbox in C#

#2 modi123_1   User is offline

  • Suitor #2
  • member icon

Reputation: 14039
  • View blog
  • Posts: 56,188
  • Joined: 12-June 08

Re: adding new price values to old one in tetxbox in C#

Posted 13 June 2011 - 10:23 AM

The += is for adding numbers. Textboxes are... strings!

Have a global variable for your decimal... call it "daily total" or what ever. Add to that and after you are done set the textbox to this variable. Repeat!
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: adding new price values to old one in tetxbox in C#

Posted 14 June 2011 - 06:30 AM

Just a couple points here...

You never bother to tell us what the serial port code is all about. Which would seem rather important.

Just having a list of strings is a hard way to go with this. Make a class for "Product"... something like ProductObj with properties for "SKU", "Description", "Price", "TaxRate" (because in most places the tax for food is different than the rate for medicine or for luxury items line cigarettes.

This way you can have a complete object. Most places the SKU is going to be the barcode for the product. But in your case the SKU could be the 3 digit identifier, rather than trying to take it off the description text. This way your code can be dynamic and always use the SKU. If you later change schemes from 3 digit code to 6 digit barcode, then later still to standard UPC code your C# code doesn't break. There is no change needed at all. The way you are forcing the use of the 3 digit code for comparison this application is a nightmare for long-term maintainability.

Just adding a total is never enough. You need to keep a list of products; that's how you product a receipt. So for each new item ordered you should add it to a List<Products> that you keep. You can calculate a running total at any time by adding up the .Price of your line items in your List<Products>, including each time a new item is added to the order. This way you can also remove line items and have your receipt and running total be more dynamic about updating.

Stop working on this. Do the two tutorials below from start to finish (actually build them, don't just skim through reading them). Then come back to your project with some new-found understanding. It will make it a lot easier and better.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1