Click here to Skip to main content
15,887,214 members
Articles / Mobile Apps

Shopping - A .NET shopping application for Pocket PC

Rate me:
Please Sign up or sign in to vote.
4.14/5 (22 votes)
28 May 2004CPOL9 min read 100K   1.5K   60  
Shopping is an application written in C# for the Pocket PC. It is a program which can be used to assist you in your daily shopping needs.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

using Shopping.Engine;

namespace Shopping
{
	/// <summary>
	/// Summary description for frmCategories.
	/// </summary>
	public class frmCategories : System.Windows.Forms.Form
	{
		private const string UPDATE = "Update";
		private const string NEW = "New";
		private const string ADD = "Add";
		private const string READY = "Ready";				

		private System.Windows.Forms.Label lblMessage;
		private System.Windows.Forms.TextBox txtItem;
		private System.Windows.Forms.ColumnHeader Name;
		private System.Windows.Forms.Button btnUpdate;
		private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ListView lvCategories;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnNew;

		private string mLastAddedCategory = string.Empty;
	
		public frmCategories()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Load Categories
			LoadCategories();
			
			// Initialize GUI
			lvCategories.SelectedIndexChanged += new EventHandler(lvCategories_SelectedIndexChanged);
			lblMessage.Text = READY;
		}

		public string LastAddedCategory
		{
			get{return mLastAddedCategory;}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}


		/// <summary>
		/// Save elements when window close
		/// </summary>
		/// <param name="e"></param>
		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing (e);

			inputPanel1.Enabled = false;
			SaveCategories();
		}


		private void lvCategories_SelectedIndexChanged(object sender, EventArgs e)
		{
			if(lvCategories.SelectedIndices.Count == 0) return;

			txtItem.Text = lvCategories.Items[lvCategories.SelectedIndices[0]].Text;	
			txtItem.Enabled = true;							
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lblMessage = new System.Windows.Forms.Label();
			this.btnUpdate = new System.Windows.Forms.Button();
			this.txtItem = new System.Windows.Forms.TextBox();
			this.lvCategories = new System.Windows.Forms.ListView();
			this.Name = new System.Windows.Forms.ColumnHeader();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnNew = new System.Windows.Forms.Button();
			this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			// 
			// lblMessage
			// 
			this.lblMessage.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Italic);
			this.lblMessage.Location = new System.Drawing.Point(2, 247);
			this.lblMessage.Size = new System.Drawing.Size(176, 16);
			this.lblMessage.Text = "Ready";
			// 
			// btnUpdate
			// 
			this.btnUpdate.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.btnUpdate.Location = new System.Drawing.Point(58, 8);
			this.btnUpdate.Size = new System.Drawing.Size(78, 24);
			this.btnUpdate.Text = "Update";
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			// 
			// txtItem
			// 
			this.txtItem.Enabled = false;
			this.txtItem.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.txtItem.Location = new System.Drawing.Point(2, 35);
			this.txtItem.Size = new System.Drawing.Size(235, 20);
			this.txtItem.Text = "";
			this.txtItem.LostFocus += new System.EventHandler(this.txtItem_LostFocus);
			this.txtItem.GotFocus += new System.EventHandler(this.txtItem_GotFocus);
			// 
			// lvCategories
			// 
			this.lvCategories.Columns.Add(this.Name);
			this.lvCategories.FullRowSelect = true;
			this.lvCategories.Location = new System.Drawing.Point(2, 63);
			this.lvCategories.Size = new System.Drawing.Size(235, 176);
			this.lvCategories.View = System.Windows.Forms.View.Details;
			this.lvCategories.GotFocus += new System.EventHandler(this.lvCategories_GotFocus);
			// 
			// Name
			// 
			this.Name.Text = "Name";
			this.Name.Width = 215;
			// 
			// btnDelete
			// 
			this.btnDelete.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.btnDelete.Location = new System.Drawing.Point(179, 8);
			this.btnDelete.Size = new System.Drawing.Size(58, 24);
			this.btnDelete.Text = "Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnNew
			// 
			this.btnNew.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.btnNew.Location = new System.Drawing.Point(2, 8);
			this.btnNew.Size = new System.Drawing.Size(48, 24);
			this.btnNew.Text = "New";
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			// 
			// inputPanel1
			// 
			this.inputPanel1.EnabledChanged += new System.EventHandler(this.inputPanel1_EnabledChanged);
			// 
			// frmCategories
			// 
			this.Controls.Add(this.lblMessage);
			this.Controls.Add(this.btnUpdate);
			this.Controls.Add(this.txtItem);
			this.Controls.Add(this.lvCategories);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnNew);
			this.Menu = this.mainMenu1;
			this.Text = "Category Manager";

		}
		#endregion

		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			UpdateCategories();
			txtItem.Enabled = false;		
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if(lvCategories.SelectedIndices.Count == 0)
			{				
				MessageBox.Show("You must select an item to delete.", "Deleting ...", 
					MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);			
			}
			else
			{
				if(MessageBox.Show("Remove selected category ?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
				{
					lvCategories.Items.RemoveAt(lvCategories.SelectedIndices[0]);
					txtItem.Text = string.Empty;
				}
			}
		}

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			if(btnNew.Text == NEW)
			{
				txtItem.Enabled = true;
				txtItem.Text = string.Empty;
				btnNew.Text = ADD;
				txtItem.Focus();
			}
			else
			{
				AddCategory();
				txtItem.Focus();
			}	
		}

		/// <summary>
		/// Update categories
		/// </summary>
		private void UpdateCategories()
		{
			if(lvCategories.SelectedIndices.Count == 0) return;

			if(txtItem.Text == string.Empty)
				lvCategories.Items.RemoveAt(lvCategories.SelectedIndices[0]);
			else
				lvCategories.Items[lvCategories.SelectedIndices[0]].Text = txtItem.Text;
		}

		/// <summary>
		/// Add a new Category to the list
		/// </summary>
		private void AddCategory()
		{
			try
			{
				if(txtItem.Text != string.Empty)
				{					
					if(!IsCategoryExists(txtItem.Text))
					{
						lvCategories.Items.Add(new ListViewItem(txtItem.Text));

						//Select the added item 
						lvCategories.Items[lvCategories.Items.Count -1].Selected = true;
						lvCategories.EnsureVisible(lvCategories.Items.Count -1);

						lblMessage.ForeColor = Color.Black;
						lblMessage.Text = "Category Added";
					}
					else
					{
						lblMessage.ForeColor = Color.Red;
						lblMessage.Text = "Category already exists.";
					}
				}

				mLastAddedCategory = txtItem.Text;
			}
			finally
			{
				txtItem.Text = string.Empty;				
			}
		}

		/// <summary>
		/// Load Categories into listview
		/// </summary>
		/// <param name="file"></param>
		private void LoadCategories()
		{													
			ArrayList elems = ShoppingApp.GetCategories();						
			
			lvCategories.Items.Clear();			

			foreach(string elem in elems)
			{
				lvCategories.Items.Add(new ListViewItem(elem));
			}					
		}

		/// <summary>
		/// Saves Categories to permanent memory
		/// </summary>
		private void SaveCategories()
		{			
			try
			{
				//Saving list
				XmlDocument doc = new XmlDocument();
				XmlElement root = doc.CreateElement("Categories");
				doc.AppendChild(root);

				XmlElement elem;

				foreach(ListViewItem itm in lvCategories.Items)
				{
					elem = doc.CreateElement("Item");
					elem.SetAttribute("Name", itm.Text);
					doc.DocumentElement.AppendChild(elem);
				}
												
				doc.Save(CST.XMLCATEGORIES);
			}
			catch
			{				
				MessageBox.Show("An error occured during save operation.", "Saving ...", 
					MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);				
			}
		}

		/// <summary>
		/// Determine if the specified category already exists
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		private bool IsCategoryExists(string name)
		{
			foreach(ListViewItem lvi in lvCategories.Items)
			{
				if(lvi.Text == name) return true;
			}

			return false;
		}

		private void txtItem_GotFocus(object sender, System.EventArgs e)
		{
			inputPanel1.Enabled = true;
		}

		private void txtItem_LostFocus(object sender, System.EventArgs e)		
		{
			try
			{
				inputPanel1.Enabled = false;
			}
			catch
			{				
			}
		}

		private void inputPanel1_EnabledChanged(object sender, System.EventArgs e)
		{
			if (inputPanel1.Enabled == false)
			{				
				lvCategories.Height = 176;
			}
			else
			{			
				lvCategories.Height = 120;
			}	
		}

		private void lvCategories_GotFocus(object sender, System.EventArgs e)
		{
			btnNew.Text = "New";
		}		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Dany McCarthy Consultant Inc.
Canada Canada
I am located in Quebec City, Canada. I have a Bachelor degree in Computer Science and another one in economic science.

I currently work as a software architect as a self-employee for my Company Dany McCarthy Consultant Inc. I am specialized in backend development, mainly SOA and development infrastructure in .Net.

I have worked as a consultant for several big governmental agencies and private companies in the last decade. I have been involved in very large projects.

Comments and Discussions