/* --------------------------------------------------------------------------------------------- U S I N G T O T A L S & C O U N T S Ron Kessler 6/29/06 C# 2005 Version --------------------------------------------------------------------------------------------- Notes: This program introduces you to VB counts & Totals. The program keeps a running total of the amount they spend and the total number of items they bought. The program also shows you how to use module-level or class-level variables Updated 6/29/06 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Using_Totals_and_Counting { public partial class frmCounts : Form { public frmCounts() { InitializeComponent(); } //---class or module level variables go here after the InitializeComponent(); stuff decimal grandTotal = 0; //hold the running total int numItems = 0; //hold the number of items bought private void btnAdd_Click(object sender, EventArgs e) { //---take item and cost & put them in textboxs string itemSelected = lstItems.Text.Substring(0,15); string price = lstItems.Text.Substring(16); itemSelected = itemSelected.Trim(); price = price.Trim(); // optional way: decimal price =Convert.ToDecimal( lstItems.Text.Substring(16).Trim()); //---now assign values to the textboxes txtItem.Text = itemSelected.Trim(); txtCost.Text = price.Trim(); //---add this cost to the running total grandTotal += Convert.ToDecimal(price); numItems++; //same as numItems = numItems + 1 or numItems += 1 } private void btnCheckOut_Click(object sender, EventArgs e) { lblTotalSpent.Text = "You spent " + grandTotal + " today. Thanks!"; lblNumItems.Text = "You purchased " + numItems + " items."; } private void btnQuit_Click(object sender, EventArgs e) { this.Close(); } } }