Click here to Skip to main content
15,895,084 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.Windows.Forms;

namespace McSoft.UILib
{
	public delegate void DoubleClickEventHandler(object sender, EventArgs e);

	/// <summary>
	/// This is our base class for our List based Controls
	/// </summary>
	public abstract class OwnerDrawnListView : McSoftControl
	{		
		private int itemHeight         = -1;
		private int selectedIndex      = -1;
		private int previousClick = SystemInformation.DoubleClickTime + 1;

		VScrollBar vs;
		ArrayList items;

		public event EventHandler SelectedIndexChanged;
		public event DoubleClickEventHandler OnDoubleClick;

		public OwnerDrawnListView()
		{
			this.vs = new VScrollBar();
			this.vs.Parent      = this;
			this.vs.Visible     = false;
			this.vs.SmallChange = 1;
			this.vs.ValueChanged += new EventHandler(this.ScrollValueChanged);

			this.items = new ArrayList();
		}

		public ArrayList Items
		{
			get
			{
				return this.items;
			}
		}


		protected virtual void MouseDownEventCheck()
		{
		}


		protected VScrollBar VScrollBar
		{
			get
			{
				return this.vs;
			}
		}

		// Raise the SelectedIndexChanged event
		protected virtual void OnSelectedIndexChanged(EventArgs e)
		{
			if(this.SelectedIndexChanged != null)
				this.SelectedIndexChanged(this, e);
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			this.SelectedIndex = this.vs.Value + (e.Y / this.ItemHeight);

			MouseDownEventCheck();

			// Invalidate the control so we can draw the item as selected.
			this.Refresh();
		}

		protected override void OnClick(EventArgs e)
		{
			int now = System.Environment.TickCount;
			if(now - previousClick <= SystemInformation.DoubleClickTime)			
				if(OnDoubleClick != null) OnDoubleClick(this.selectedIndex, e);
			previousClick = now;

			base.OnClick (e);
		}


		// Get or set index of selected item.
		public int SelectedIndex
		{
			get
			{
				return this.selectedIndex;
			}

			set
			{
				this.selectedIndex = value;

				if (this.SelectedIndexChanged != null)
					this.SelectedIndexChanged(this, EventArgs.Empty);

			}
		}

		protected void ScrollValueChanged(object o, EventArgs e)
		{
			this.Refresh();
		}

		protected virtual int ItemHeight
		{
			get
			{
				return this.itemHeight;
			}

			set
			{
				this.itemHeight = value;
			}
		}

		// If the requested index is before the first visible index then set the
		// first item to be the requested index. If it is after the last visible
		// index, then set the last visible index to be the requested index.
		public void EnsureVisible(int index)
		{
			if(index < this.vs.Value)
			{
				this.vs.Value = index;
				this.Refresh();
			}
			else if(index >= this.vs.Value + this.DrawCount)
			{
				this.vs.Value = index - this.DrawCount + 1;
				this.Refresh();
			}
		}		

		// Selected item moves when you use the keyboard up/down keys.
		protected override void OnKeyDown(KeyEventArgs e)
		{
			switch(e.KeyCode)
			{
				case Keys.Down:					
					if(this.SelectedIndex < this.vs.Maximum && this.selectedIndex < this.Items.Count - 1)
					{
						EnsureVisible(++this.SelectedIndex);
						this.Refresh();						
					}					
					break;
				case Keys.Up:
					if(this.SelectedIndex > this.vs.Minimum)
					{
						EnsureVisible(--this.SelectedIndex);
						this.Refresh();
					}
					break;
				case Keys.PageDown:
					this.SelectedIndex = Math.Min(this.vs.Maximum, this.SelectedIndex + this.DrawCount);
					EnsureVisible(this.SelectedIndex);
					this.Refresh();
					break;
				case Keys.PageUp:
					this.SelectedIndex = Math.Max(this.vs.Minimum, this.SelectedIndex - this.DrawCount);
					EnsureVisible(this.SelectedIndex);
					this.Refresh();
					break;
				case Keys.Home:
					this.SelectedIndex = 0;
					EnsureVisible(this.SelectedIndex);
					this.Refresh();
					break;
				case Keys.End:
					this.SelectedIndex = this.items.Count - 1;
					EnsureVisible(this.SelectedIndex);
					this.Refresh();
					break;
			}

			base.OnKeyDown(e);
		}

		// Calculate how many items we can draw given the height of the control.
		protected int DrawCount
		{
			get
			{
				if(this.vs.Value + this.vs.LargeChange > this.vs.Maximum)
					return this.vs.Maximum - this.vs.Value + 1;
				else
					return this.vs.LargeChange;
			}
		}
	}
}

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