Click here to Skip to main content
15,881,559 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 99.9K   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 System.Reflection;

using Shopping.Engine;

namespace Shopping
{
	/// <summary>
	/// Summary description for frmFormats.
	/// </summary>
	public class frmUnits : 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.ListView lvUnits;
		private System.Windows.Forms.ColumnHeader Name;
		private System.Windows.Forms.Button btnNew;
		private System.Windows.Forms.Button btnUpdate;
		private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.Button btnDelete;

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

			// Load Units
			LoadUnits();
			
			// Initialize GUI
			lvUnits.SelectedIndexChanged += new EventHandler(lvUnits_SelectedIndexChanged);
			lblMessage.Text = READY;
		}

		public string LastAddedUnit
		{
			get{return mLastAddedUnit;}
		}

		/// <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;
			SaveUnits();
		}


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

			txtItem.Text = lvUnits.Items[lvUnits.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.lvUnits = 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();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			// 
			// lblMessage
			// 
			this.lblMessage.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Italic);
			this.lblMessage.Location = new System.Drawing.Point(3, 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(59, 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(3, 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);
			// 
			// lvUnits
			// 
			this.lvUnits.Columns.Add(this.Name);
			this.lvUnits.FullRowSelect = true;
			this.lvUnits.Location = new System.Drawing.Point(3, 63);
			this.lvUnits.Size = new System.Drawing.Size(235, 176);
			this.lvUnits.View = System.Windows.Forms.View.Details;
			this.lvUnits.GotFocus += new System.EventHandler(this.lvUnits_GotFocus);
			this.lvUnits.SelectedIndexChanged += new System.EventHandler(this.lvUnits_SelectedIndexChanged);
			// 
			// 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(180, 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(3, 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);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuItem1);
			// 
			// menuItem1
			// 
			this.menuItem1.Text = "";
			// 
			// frmUnits
			// 
			this.Controls.Add(this.lblMessage);
			this.Controls.Add(this.btnUpdate);
			this.Controls.Add(this.txtItem);
			this.Controls.Add(this.lvUnits);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnNew);
			this.Menu = this.mainMenu1;
			this.Text = "Unit Manager";

		}
		#endregion

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

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if(lvUnits.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 unit ?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
				{
					lvUnits.Items.RemoveAt(lvUnits.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
			{
				AddUnit();
				txtItem.Focus();
			}	
		}

		/// <summary>
		/// Update le format
		/// </summary>
		private void UpdateUnit()
		{
			if(lvUnits.SelectedIndices.Count == 0) return;
			
			if(txtItem.Text == string.Empty)
				lvUnits.Items.RemoveAt(lvUnits.SelectedIndices[0]);
			else
				lvUnits.Items[lvUnits.SelectedIndices[0]].Text = txtItem.Text;				
		}

		/// <summary>
		/// Add a new Format to the list
		/// </summary>
		private void AddUnit()
		{
			try
			{
				if(txtItem.Text != string.Empty)
				{					
					if(!IsUnitExists(txtItem.Text))
					{
						lvUnits.Items.Add(new ListViewItem(txtItem.Text));

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

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

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

		/// <summary>
		/// Load Formats into listview
		/// </summary>
		/// <param name="file"></param>
		private void LoadUnits()
		{						
			if(System.IO.File.Exists(CST.XMLUNITS))
			{
				ArrayList elems = ShoppingApp.GetUnits();

				lvUnits.Items.Clear();

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

		/// <summary>
		/// Saves formats to permanent memory
		/// </summary>
		private void SaveUnits()
		{			
			try
			{
				//Sauvegarde de la liste
				XmlDocument doc = new XmlDocument();
				XmlElement root = doc.CreateElement("Units");
				doc.AppendChild(root);

				XmlElement elem;

				foreach(ListViewItem itm in lvUnits.Items)
				{
					elem = doc.CreateElement("Item");
					elem.SetAttribute("Name", itm.Text);
					doc.DocumentElement.AppendChild(elem);
				}
												
				doc.Save(CST.XMLUNITS);
			}
			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 IsUnitExists(string name)
		{
			foreach(ListViewItem lvi in lvUnits.Items)
			{
				if(lvi.Text == name) return true;
			}

			return false;
		}

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

		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 lvUnits_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