Click here to Skip to main content
15,894,012 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 100.1K   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 McSoft.UILib;
using Shopping.Engine;

namespace Shopping
{
	/// <summary>
	/// Summary description for frmEdit.
	/// </summary>
	public class frmEdit : System.Windows.Forms.Form
	{
		private const int TABCONTROL_ORIGINAL_HEIGHT = 270;

		private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
		private System.Windows.Forms.MainMenu mainMenu1;	

		private McSoft.UILib.AbcCategories mCategories;
		private Line HeaderLine;

		private McSoft.UILib.HyperlinkButton mLnkProducts;
		private System.Windows.Forms.ImageList imgToolbar;
		private McSoft.UILib.HyperlinkButton mLnkUnits;
		
		private ArrayList mAllProducts = new ArrayList(0);
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.Label lblQty;
		private System.Windows.Forms.NumericUpDown numQty;
		private System.Windows.Forms.ListBox lstProducts;
		private System.Windows.Forms.TextBox txtNote;
		private System.Windows.Forms.TabPage tabEditor;
		private System.Windows.Forms.TabPage tabNotes;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton btnOK;
		private System.Windows.Forms.ToolBarButton btnCancel;
		private System.Windows.Forms.ComboBox cboUnits;
		private CartItem mNewCartItem = null;
	
		public frmEdit(CartItem itm)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();		

			//Creates and Initialize AbcCategories
			mCategories = new AbcCategories();
			mCategories.Parent = tabEditor;
			mCategories.Bounds = new Rectangle(0, 0, 246, 16);
			mCategories.CategoryChanged += new CategoryChangedEventHandler(mCategories_CategoryChanged);

			HeaderLine = new Line();
			HeaderLine.ForeColor = Color.Gray;
			HeaderLine.Bounds = new Rectangle(0, 17, 246, 1);
			HeaderLine.Parent = tabEditor;

			//Creates and Initialize Products links
			mLnkProducts = new HyperlinkButton();
			mLnkProducts.Parent = tabEditor;
			mLnkProducts.Bounds = new Rectangle(4, 26, 48, 16);
			mLnkProducts.Text = "Products";
			mLnkProducts.ForeColor = Color.MidnightBlue;
			mLnkProducts.Click += new EventHandler(mLnkProducts_Click);

			//Creates and Initialize Units links
			mLnkUnits = new HyperlinkButton();
			mLnkUnits.Parent = tabEditor;
			mLnkUnits.Bounds = new Rectangle(4, 149, 48, 16);
			mLnkUnits.Text = "Units";
			mLnkUnits.ForeColor = Color.MidnightBlue;
			mLnkUnits.Click += new EventHandler(mLnkUnits_Click);
			
			LoadProducts();
			FilterProducts("A");  //Initially we do not apply any filter
			LoadUnits();
			if(itm != null) LoadCurrentData(itm);
		}

		/// <summary>
		/// Cart created by editor
		/// </summary>
		public CartItem NewCartItem
		{
			get{return mNewCartItem;}
		}

		/// <summary>
		/// Load a CartItem into GUI.
		/// </summary>
		/// <param name="itm"></param>
		private void LoadCurrentData(CartItem itm)
		{
			int idx = 0;
			foreach(string currentItm in lstProducts.Items)
			{
				if(currentItm == itm.Name)
				{
					lstProducts.SelectedIndex = idx;
					lstProducts.TopIndex = idx;
				}
				idx ++;
			}

			numQty.Text = itm.Quantity.ToString();
			numQty.Value = itm.Quantity;
			cboUnits.Text = itm.Format;
			txtNote.Text = itm.Note;
		}

		/// <summary>
		/// Load available formats into combobox
		/// </summary>
		private void LoadUnits()
		{
			cboUnits.Items.Clear();									

			ArrayList items = ShoppingApp.GetUnits();
			
			foreach(string elem in items)
			{
				cboUnits.Items.Add(elem);
			}
	
			if( !cboUnits.Items.Contains(CST.NO_DEFAULT) )
				cboUnits.Items.Add(CST.NO_DEFAULT);
		}

		/// <summary>
		/// Load available products into combo
		/// </summary>
		private void LoadProducts()
		{	
			if(System.IO.File.Exists(CST.XMLPRODUCTS))
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(CST.XMLPRODUCTS);

				mAllProducts = new ArrayList(doc.DocumentElement.ChildNodes.Count);
				
				foreach(XmlElement elem in doc.DocumentElement)
				{
					Product product = new Product(elem.GetAttribute("Name"), 
						elem.HasAttribute("DefaultFormat") ? elem.GetAttribute("DefaultFormat") : string.Empty);
					mAllProducts.Add(product);												
				}		
				
				IComparer myComparer = new ProductsComparer();
				mAllProducts.Sort(myComparer);	
			}		
		}
		

		/// <summary>
		/// Filter the list 
		/// </summary>		
		private void FilterProducts(string filter)
		{
			if(filter == "A")
			{
				if(lstProducts.Items.Count != mAllProducts.Count)
				{
					lstProducts.Items.Clear();

					foreach(Product product in mAllProducts)
					{
						lstProducts.Items.Add(product.Name);
					}
				}
			}
			else
			{
				lstProducts.Items.Clear();	
				foreach(Product product in mAllProducts)
				{		
					if(product.Name.Substring(0,1).ToLower().CompareTo(filter.ToLower()) >= 0)
						lstProducts.Items.Add(product.Name);				
				}
			}			
		}


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

		#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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmEdit));
			this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.imgToolbar = new System.Windows.Forms.ImageList();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabEditor = new System.Windows.Forms.TabPage();
			this.cboUnits = new System.Windows.Forms.ComboBox();
			this.lblQty = new System.Windows.Forms.Label();
			this.numQty = new System.Windows.Forms.NumericUpDown();
			this.lstProducts = new System.Windows.Forms.ListBox();
			this.tabNotes = new System.Windows.Forms.TabPage();
			this.txtNote = new System.Windows.Forms.TextBox();
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.btnOK = new System.Windows.Forms.ToolBarButton();
			this.btnCancel = new System.Windows.Forms.ToolBarButton();
			// 
			// inputPanel1
			// 
			this.inputPanel1.EnabledChanged += new System.EventHandler(this.inputPanel1_EnabledChanged);
			// 
			// imgToolbar
			// 
			this.imgToolbar.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
			this.imgToolbar.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
			this.imgToolbar.ImageSize = new System.Drawing.Size(16, 16);
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabEditor);
			this.tabControl1.Controls.Add(this.tabNotes);
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(240, 270);
			this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
			// 
			// tabEditor
			// 
			this.tabEditor.Controls.Add(this.cboUnits);
			this.tabEditor.Controls.Add(this.lblQty);
			this.tabEditor.Controls.Add(this.numQty);
			this.tabEditor.Controls.Add(this.lstProducts);
			this.tabEditor.Location = new System.Drawing.Point(4, 4);
			this.tabEditor.Size = new System.Drawing.Size(232, 244);
			this.tabEditor.Text = "Editor";
			// 
			// cboUnits
			// 
			this.cboUnits.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.cboUnits.Location = new System.Drawing.Point(58, 149);
			this.cboUnits.Size = new System.Drawing.Size(174, 21);
			// 
			// lblQty
			// 
			this.lblQty.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.lblQty.ForeColor = System.Drawing.Color.MidnightBlue;
			this.lblQty.Location = new System.Drawing.Point(2, 126);
			this.lblQty.Size = new System.Drawing.Size(48, 16);
			this.lblQty.Text = "Quantity";
			// 
			// numQty
			// 
			this.numQty.Location = new System.Drawing.Point(58, 124);
			this.numQty.Maximum = new System.Decimal(new int[] {
																   9999,
																   0,
																   0,
																   0});
			this.numQty.Minimum = new System.Decimal(new int[] {
																   1,
																   0,
																   0,
																   0});
			this.numQty.Size = new System.Drawing.Size(174, 20);
			this.numQty.Value = new System.Decimal(new int[] {
																 1,
																 0,
																 0,
																 0});
			this.numQty.LostFocus += new System.EventHandler(this.numQty_LostFocus);
			this.numQty.GotFocus += new System.EventHandler(this.numQty_GotFocus);
			// 
			// lstProducts
			// 
			this.lstProducts.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.lstProducts.Location = new System.Drawing.Point(59, 27);
			this.lstProducts.Size = new System.Drawing.Size(173, 93);
			this.lstProducts.SelectedIndexChanged += new System.EventHandler(this.lstProducts_SelectedIndexChanged);
			// 
			// tabNotes
			// 
			this.tabNotes.Controls.Add(this.txtNote);
			this.tabNotes.Location = new System.Drawing.Point(4, 4);
			this.tabNotes.Size = new System.Drawing.Size(232, 244);
			this.tabNotes.Text = "Notes";
			// 
			// txtNote
			// 
			this.txtNote.AcceptsReturn = true;
			this.txtNote.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
			this.txtNote.Multiline = true;
			this.txtNote.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtNote.Size = new System.Drawing.Size(236, 240);
			this.txtNote.Text = "";
			this.txtNote.LostFocus += new System.EventHandler(this.txtNote_LostFocus);
			this.txtNote.GotFocus += new System.EventHandler(this.txtNote_GotFocus);
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.Add(this.btnOK);
			this.toolBar1.Buttons.Add(this.btnCancel);
			this.toolBar1.ImageList = this.imgToolbar;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// btnOK
			// 
			this.btnOK.ImageIndex = 0;
			// 
			// btnCancel
			// 
			this.btnCancel.ImageIndex = 1;
			// 
			// frmEdit
			// 
			this.ClientSize = new System.Drawing.Size(240, 294);
			this.ControlBox = false;
			this.Controls.Add(this.tabControl1);
			this.Controls.Add(this.toolBar1);
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.MinimizeBox = false;
			this.Text = "Item Editor";

		}
		#endregion

		private void mLnkProducts_Click(object sender, EventArgs e)
		{
			frmProducts editor = new frmProducts();
			editor.ShowDialog();

			LoadProducts();
			LoadUnits();
			FilterProducts(mCategories.SelectedCategory);	
		
			// Select the last added product in the list
			lstProducts.SelectedItem = editor.LastAddedItem;
		}

		private void mLnkUnits_Click(object sender, EventArgs e)
		{
			frmUnits editor = new frmUnits();
			editor.ShowDialog();

			LoadUnits();
			if(editor.LastAddedUnit != string.Empty)
				cboUnits.SelectedItem = editor.LastAddedUnit;
		}

		private void btnOK_Click()
		{
			mNewCartItem = new CartItem(lstProducts.Text, Convert.ToInt32(numQty.Value), cboUnits.Text, txtNote.Text,false);
			this.Close();
		}

		private void btnCancel_Click()
		{
			mNewCartItem = null;
			this.Close();
		}		

		private void mCategories_CategoryChanged(object sender, EventArgs e)
		{
			FilterProducts( ((ToggleCategoryButton)sender).SelectedCategory );
		}

		private void inputPanel1_EnabledChanged(object sender, System.EventArgs e)
		{		
			if (inputPanel1.Enabled == false)
			{				
				tabControl1.Height = TABCONTROL_ORIGINAL_HEIGHT;
			}
			else
			{			
				tabControl1.Height = inputPanel1.VisibleDesktop.Height;
			}
			txtNote.Height = tabNotes.Height;
		}

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

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

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

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

		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(tabControl1.SelectedIndex == 0)
			{
				lstProducts.Focus();
			}
			else
			{
				txtNote.Focus();
			}
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch(toolBar1.Buttons.IndexOf(e.Button))
			{
				case 0:
					btnOK_Click();
					break;
				case 1:
					btnCancel_Click();
					break;				
			}
		}

		private void lstProducts_SelectedIndexChanged(object sender, System.EventArgs e)
		{	
			string productName = (string)lstProducts.SelectedItem;
			
			foreach(Product product in mAllProducts)
			{
				if(product.Name == productName)
				{
					cboUnits.SelectedItem = product.DefaultFormat == string.Empty ? CST.NO_DEFAULT : product.DefaultFormat ;
					break;
				}
			}						
		}
	}
}

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