using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using C1.Data;

namespace DataLibrary2
{
    public class DataClass2 : DataLibrary1.DataClass1
    {
        private C1.Data.C1SchemaRef c1SchemaRef1;
        private C1.Data.C1TableLogic c1TableLogic1;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DataClass2(System.ComponentModel.IContainer container)
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            container.Add(this);
            InitializeComponent();
        }

        public DataClass2()
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            InitializeComponent();

            // This line is necessary to connect c1SchemaRef1 to schemaDef
            schemaDef.Add(c1SchemaRef1);
        }

        /// <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 Component 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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataClass2));
            this.c1SchemaRef1 = new C1.Data.C1SchemaRef();
            this.c1TableLogic1 = new C1.Data.C1TableLogic();
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef)).BeginInit();
            // 
            // schemaDef
            // 
            this.schemaDef.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("schemaDef.DummyImage")));
            this.schemaDef.SerializedSchemaXML = this.schemaDef.GetSchemaFromResources(resources, "schemaDef", this);
            this.schemaDef.SerializedDesignTimeOptionsXML = this.schemaDef.GetDesignTimeOptionsFromResources(resources, "schemaDef", this);
            // 
            // c1SchemaRef1
            // 
            this.c1SchemaRef1.SchemaDef = "schemaDef";
            // 
            // c1TableLogic1
            // 
            this.c1TableLogic1.SchemaComponent = this.c1SchemaRef1;
            this.c1TableLogic1.SchemaPersist = null;
            this.c1TableLogic1.Table = "Order Details";
            this.c1TableLogic1.BeforeFieldChange += new C1.Data.FieldChangeEventHandler(this.c1TableLogic1_BeforeFieldChange);
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef)).EndInit();

        }
        #endregion

        private void c1TableLogic1_BeforeFieldChange(object sender, C1.Data.FieldChangeEventArgs e)
        {
            if (e.Field.Name == "Quantity")
            {
                if ((short)e.NewValue < 1)
                    e.NewValue = (short)1;
            }
            else if (e.Field.Name == "Discount")
            {
                if ((float)e.NewValue < 0 || (float)e.NewValue > 1)
                    throw new ApplicationException("Discount must be between 0 and 1");
            }
        }

        protected override void c1TableLogic1BeforeFieldChange(object sender, C1.Data.FieldChangeEventArgs e)
        {
            if (e.Field.Name == "CustomerID")
            {
                if (e.NewValue.ToString().Length != 5)
                    throw new ApplicationException("CustomerID must be a 5-character word");
            }
            base.c1TableLogic1BeforeFieldChange(sender, e);
        }
    }
}
