using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Northwind
{
	/// <summary>
	/// Summary description for frmCustomerList.
	/// </summary>
	public class frmCustomerList : System.Windows.Forms.Form
	{
		public string SelectedCustomerID
		{
			get
			{
				if(null!=lbChooseCustomer.SelectedItem)
				{
					return ((DataRowView)lbChooseCustomer.SelectedItem)
						.Row["CustomerID"].ToString();
				}
				return null;
			}
		}

		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.ListBox lbChooseCustomer;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmCustomerList()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			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()
		{
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.lbChooseCustomer = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// btnOK
			// 
			this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			this.btnOK.Enabled = false;
			this.btnOK.Location = new System.Drawing.Point(56, 280);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(168, 280);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// lbChooseCustomer
			// 
			this.lbChooseCustomer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lbChooseCustomer.Location = new System.Drawing.Point(8, 8);
			this.lbChooseCustomer.Name = "lbChooseCustomer";
			this.lbChooseCustomer.Size = new System.Drawing.Size(272, 264);
			this.lbChooseCustomer.TabIndex = 2;
			this.lbChooseCustomer.DoubleClick += new System.EventHandler(this.lbChooseCustomer_DoubleClick);
			this.lbChooseCustomer.SelectedIndexChanged += new System.EventHandler(this.lbChooseCustomer_SelectedIndexChanged);
			// 
			// frmCustomerList
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(288, 310);
			this.Controls.Add(this.lbChooseCustomer);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Name = "frmCustomerList";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Customer List";
			this.Load += new System.EventHandler(this.frmCustomerList_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
			this.DialogResult=DialogResult.Cancel;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
			this.DialogResult=DialogResult.OK;
		}

		private void frmCustomerList_Load(object sender, System.EventArgs e)
		{
			try
			{
				this.Cursor = Cursors.WaitCursor;
				//TODO:  Load up the customer list
				using(NorthwindDataAccess nwda = 
						  new NorthwindDataAccess())
				{
					lbChooseCustomer.DataSource = 
						nwda.RetrieveCustomerList().Tables[0];
					lbChooseCustomer.DisplayMember = 
						"CompanyName";
				}
			}
			catch(Exception ex)
			{
				MainClass.MainUIThreadExceptionHandler(ex);
			}
			finally
			{
				this.Cursor=Cursors.Default;
			}
		}

		private void lbChooseCustomer_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			btnOK.Enabled = (-1!=lbChooseCustomer.SelectedIndex);
		}

		private void lbChooseCustomer_DoubleClick(object sender, System.EventArgs e)
		{
			if(null!=lbChooseCustomer.SelectedItem)
				btnOK_Click(sender,e);
		}
	}
}
