using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using ITC81DB_2.DialogForms;
using ITC81DB_2.Forms;
using ITC81DB_2_ImageDll;

namespace ITC81DB_2.Controls
{
    public partial class ControlCenterBasicProduct : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private int SelectedProductNo;
        private ProductItem SelectedProductItem1;
        private ProductItem SelectedProductItem2;
        #endregion

        #region Constructor
        public ControlCenterBasicProduct(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundProduct));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundProduct));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.spnBackgroundProduct));
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundProduct));
            }
        }
        private void DefaultSetting()
        {
            this.SelectedProductNo = 1;
            this.SelectedProductItem1 = new ProductItem();
            this.SelectedProductItem2 = new ProductItem();

            if (this.listBoxProductList.Items.Count == 0)
            {
                this.listBoxProductList.SelectedIndexChanged -= new EventHandler(this.listBoxProductList_SelectedIndexChanged);
                this.AddProductList();
                this.listBoxProductList.SelectedIndexChanged += new EventHandler(this.listBoxProductList_SelectedIndexChanged);
            }
        }

        private void AddProductList()
        {
            Collection<string> productNames = new Collection<string>();

            this.ParentForm.ParentForm.LoadProductName1(ref productNames);

            this.listBoxProductList.Items.Clear();

            for (int i = 0; i < productNames.Count; i++)
                this.listBoxProductList.Items.Add(productNames[i]);
        }
        private void DisplayControls1(ProductItem item)
        {
            int iValue = 0;
            string sValue = "";

            if (this.SelectedProductItem1 == null && item == null)
                return;

            sValue = item.Number.ToString();
            if (this.buttonProductNo.Text != sValue)
                this.buttonProductNo.Text = sValue;

            sValue = item.Name;
            if (this.textBoxProductName.Text != sValue)
                this.textBoxProductName.Text = sValue;

            sValue = item.LotNo;
            if (this.textBoxLotNo.Text != sValue)
                this.textBoxLotNo.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelOverRange1.Text != sValue)
                this.labelOverRange1.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelPassRange1.Text != sValue)
                this.labelPassRange1.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUnderRange1.Text != sValue)
                this.labelUnderRange1.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelTareRange1.Text != sValue)
                this.labelTareRange1.Text = sValue;

            iValue = int.Parse(item.OverRange) - int.Parse(item.PassRange);
            sValue = Helper.StringToDecimalPlaces(iValue.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            sValue = string.Format("+{0}", sValue);
            if (this.labelDeviationOver1.Text != sValue)
                this.labelDeviationOver1.Text = sValue;

            iValue = int.Parse(item.PassRange) - int.Parse(item.UnderRange);
            sValue = string.Format("-{0}", Helper.StringToDecimalPlaces(iValue.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
            if (this.labelDeviationUnder1.Text != sValue)
                this.labelDeviationUnder1.Text = sValue;
        }
        private void DisplayControls2(ProductItem item)
        {
            int iValue = 0;
            string sValue = "";

            if (this.SelectedProductItem1 == null && item == null)
                return;

            sValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelOverRange2.Text != sValue)
                this.labelOverRange2.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelPassRange2.Text != sValue)
                this.labelPassRange2.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUnderRange2.Text != sValue)
                this.labelUnderRange2.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelTareRange2.Text != sValue)
                this.labelTareRange2.Text = sValue;

            iValue = int.Parse(item.OverRange) - int.Parse(item.PassRange);
            sValue = Helper.StringToDecimalPlaces(iValue.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            sValue = string.Format("+{0}", sValue);
            if (this.labelDeviationOver2.Text != sValue)
                this.labelDeviationOver2.Text = sValue;

            iValue = int.Parse(item.PassRange) - int.Parse(item.UnderRange);
            sValue = string.Format("-{0}", Helper.StringToDecimalPlaces(iValue.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
            if (this.labelDeviationUnder2.Text != sValue)
                this.labelDeviationUnder2.Text = sValue;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.BasicProduct;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.buttonProductNo.Text = this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString();
            this.SelectedProductNo = this.ParentForm.ParentForm.SystemConfig.ProductNumber;

            this.listBoxProductList.SelectedIndex = this.SelectedProductNo - 1;

            this.ParentForm.ParentForm.LoadProductFile1(ref this.SelectedProductItem1, this.SelectedProductNo - 1);
            this.ParentForm.ParentForm.LoadProductFile2(ref this.SelectedProductItem2, this.SelectedProductNo - 1);
            this.DisplayControls1(this.SelectedProductItem1);
            this.DisplayControls2(this.SelectedProductItem2);

            this.smartKeyboard.Hide();
            this.buttonNameEdit.Enabled = true;
            this.buttonLotEdit.Enabled = true;
            this.textBoxProductName.Enabled = true;
            this.textBoxLotNo.Enabled = true;
            this.listBoxProductList.Enabled = true;
        }
        #endregion

        #region Event Handler
        private void buttonProductNo_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonProductNo.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(280, 90);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 1000)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.buttonProductNo.Text = myKeyPad.StringValue;
                    this.SelectedProductNo = myKeyPad.IntValue;

                    this.listBoxProductList.SelectedIndexChanged -= new EventHandler(this.listBoxProductList_SelectedIndexChanged);
                    this.listBoxProductList.SelectedIndex = this.SelectedProductNo - 1;
                    this.listBoxProductList.SelectedIndexChanged += new EventHandler(this.listBoxProductList_SelectedIndexChanged);

                    this.ParentForm.ParentForm.LoadProductFile1(ref this.SelectedProductItem1, this.SelectedProductNo - 1);
                    this.ParentForm.ParentForm.LoadProductFile2(ref this.SelectedProductItem2, this.SelectedProductNo - 1);

                    this.DisplayControls1(this.SelectedProductItem1);
                    this.DisplayControls2(this.SelectedProductItem2);
                }
            }
        }
        private void buttonNameEdit_Click(object sender, EventArgs e)
        {
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Location = new Point(8, 160);
            this.smartKeyboard.Size = new Size(695, 300);
            this.smartKeyboard.Hide();

            this.smartKeyboard.TargetInputObject = this.textBoxProductName;
            this.smartKeyboard.Show();

            this.textBoxProductName.Select(this.textBoxProductName.Text.Length, 0);

            this.buttonNameEdit.Enabled = false;
            this.buttonLotEdit.Enabled = false;
            this.textBoxLotNo.Enabled = false;
            this.listBoxProductList.Enabled = false;
        }
        private void buttonLotEdit_Click(object sender, EventArgs e)
        {
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Location = new Point(8, 160);
            this.smartKeyboard.Size = new Size(695, 300);
            this.smartKeyboard.Hide();

            this.smartKeyboard.TargetInputObject = this.textBoxLotNo;
            this.smartKeyboard.Show();

            this.textBoxLotNo.Select(this.textBoxLotNo.Text.Length, 0);

            this.buttonNameEdit.Enabled = false;
            this.buttonLotEdit.Enabled = false;
            this.textBoxProductName.Enabled = false;
            this.listBoxProductList.Enabled = false;
        }

        private void textBoxProductName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.SelectedProductItem1.Name = this.textBoxProductName.Text;

                this.listBoxProductList.SelectedIndexChanged -= new EventHandler(this.listBoxProductList_SelectedIndexChanged);
                this.listBoxProductList.Items.Insert(this.SelectedProductItem1.Number - 1, this.textBoxProductName.Text);
                this.listBoxProductList.Items.RemoveAt(this.SelectedProductItem1.Number);
                this.listBoxProductList.SelectedIndex = this.SelectedProductItem1.Number - 1;
                this.listBoxProductList.SelectedIndexChanged += new EventHandler(this.listBoxProductList_SelectedIndexChanged);

                e.Handled = true;
                this.smartKeyboard.Hide();

                this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxLotNo.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;
                this.smartKeyboard.Hide();
                this.ParentForm.ParentForm.LoadProductFile1(ref this.SelectedProductItem1, this.SelectedProductNo - 1);
                this.DisplayControls1(this.SelectedProductItem1);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxLotNo.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
            else if (e.KeyChar == '<' || e.KeyChar == '>' || e.KeyChar == '|' || e.KeyChar == '"' || e.KeyChar == '?'
                || e.KeyChar == '*' || e.KeyChar == ':' || e.KeyChar == '/' || e.KeyChar == '\\')
            {
                e.Handled = true;
            }
        }
        private void textBoxLotNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.SelectedProductItem1.LotNo = this.textBoxLotNo.Text;

                e.Handled = true;
                this.smartKeyboard.Hide();

                this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxProductName.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;
                this.smartKeyboard.Hide();
                this.ParentForm.ParentForm.LoadProductFile1(ref this.SelectedProductItem1, this.SelectedProductNo - 1);
                this.DisplayControls1(this.SelectedProductItem1);

                this.buttonNameEdit.Enabled = true;
                this.buttonLotEdit.Enabled = true;
                this.textBoxProductName.Enabled = true;
                this.listBoxProductList.Enabled = true;
            }
            else if (e.KeyChar == '<' || e.KeyChar == '>' || e.KeyChar == '|' || e.KeyChar == '"' || e.KeyChar == '?'
                || e.KeyChar == '*' || e.KeyChar == ':' || e.KeyChar == '/' || e.KeyChar == '\\')
            {
                e.Handled = true;
            }
        }

        private void labelOverRange1_Click(object sender, EventArgs e)
        {
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelOverRange1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < Helper.StringToWeight(this.SelectedProductItem1.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelOverRange1.Text = myKeyPad.StringValue;
                    this.SelectedProductItem1.OverRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

                    this.DisplayControls1(this.SelectedProductItem1);
                }
            }
        }
        private void labelPassRange1_Click(object sender, EventArgs e)
        {
            int digit = 0, temp = 0, range = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPassRange1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);


            if (myKeyPad.ShowDialog() != DialogResult.OK)
                return;

            this.SelectedProductItem1.PassRange = myKeyPad.StringValue.Replace(".", "");

            temp = this.SelectedProductItem1.PassRangeInt + this.SelectedProductItem1.UnderRangeDeviation;
            if (temp < 0)
                this.SelectedProductItem1.UnderRange = "0";
            else
                this.SelectedProductItem1.UnderRange = temp.ToString();

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                range = 1000000;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                range = 100000;
            else
                range = 10000;
            temp = this.SelectedProductItem1.PassRangeInt + this.SelectedProductItem1.OverRangeDeviation;
            if (temp >= range)
                this.SelectedProductItem1.OverRange = this.SelectedProductItem1.PassRange;
            else
                this.SelectedProductItem1.OverRange = temp.ToString();

            this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

            this.DisplayControls1(this.SelectedProductItem1);
        }
        private void labelUnderRange1_Click(object sender, EventArgs e)
        {
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelUnderRange1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > Helper.StringToWeight(this.SelectedProductItem1.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelUnderRange1.Text = myKeyPad.StringValue;
                    this.SelectedProductItem1.UnderRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

                    this.DisplayControls1(this.SelectedProductItem1);
                }
            }
        }
        private void labelDeviationOver1_Click(object sender, EventArgs e)
        {
            int digit = 0;
            string sValue = "";
            double dValue = 0.0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDeviationOver1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.SelectedProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                       + myKeyPad.doubleValue;
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                if (dValue < Helper.StringToWeight(this.SelectedProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDeviationOver1.Text = myKeyPad.StringValue;

                    this.SelectedProductItem1.OverRange = sValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

                    this.DisplayControls1(this.SelectedProductItem1);
                }
            }
        }
        private void labelDeviationUnder1_Click(object sender, EventArgs e)
        {
            int digit = 0;
            string sValue = "";
            double dValue = 0.0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDeviationUnder1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.SelectedProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                       - Math.Abs(myKeyPad.doubleValue);
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                if (dValue > Helper.StringToWeight(this.SelectedProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDeviationUnder1.Text = myKeyPad.StringValue;

                    this.SelectedProductItem1.UnderRange = sValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);

                    this.DisplayControls1(this.SelectedProductItem1);
                }
            }
        }
        private void labelTareRange1_Click(object sender, EventArgs e)
        {
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelTareRange1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(100, 90);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 99999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelTareRange1.Text = myKeyPad.StringValue;
                    this.SelectedProductItem1.TareRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile1(this.SelectedProductItem1, this.SelectedProductNo - 1);
                }
            }
        }
        private void labelOverRange2_Click(object sender, EventArgs e)
        {
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelOverRange2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < Helper.StringToWeight(this.SelectedProductItem2.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelOverRange2.Text = myKeyPad.StringValue;
                    this.SelectedProductItem2.OverRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile2(this.SelectedProductItem2, this.SelectedProductNo - 1);

                    this.DisplayControls2(this.SelectedProductItem2);
                }
            }
        }
        private void labelPassRange2_Click(object sender, EventArgs e)
        {
            int digit = 0, temp = 0, range = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPassRange2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);


            if (myKeyPad.ShowDialog() != DialogResult.OK)
                return;

            this.SelectedProductItem2.PassRange = myKeyPad.StringValue.Replace(".", "");

            temp = this.SelectedProductItem2.PassRangeInt + this.SelectedProductItem2.UnderRangeDeviation;
            if (temp < 0)
                this.SelectedProductItem2.UnderRange = "0";
            else
                this.SelectedProductItem2.UnderRange = temp.ToString();

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                range = 1000000;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                range = 100000;
            else
                range = 10000;
            temp = this.SelectedProductItem2.PassRangeInt + this.SelectedProductItem2.OverRangeDeviation;
            if (temp >= range)
                this.SelectedProductItem2.OverRange = this.SelectedProductItem2.PassRange;
            else
                this.SelectedProductItem2.OverRange = temp.ToString();

            this.ParentForm.ParentForm.SaveProductFile2(this.SelectedProductItem2, this.SelectedProductNo - 1);

            this.DisplayControls2(this.SelectedProductItem2);
        }
        private void labelUnderRange2_Click(object sender, EventArgs e)
        {
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelUnderRange2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > Helper.StringToWeight(this.SelectedProductItem2.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelUnderRange2.Text = myKeyPad.StringValue;
                    this.SelectedProductItem2.UnderRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile2(this.SelectedProductItem2, this.SelectedProductNo - 1);

                    this.DisplayControls2(this.SelectedProductItem2);
                }
            }
        }
        private void labelDeviationOver2_Click(object sender, EventArgs e)
        {
            int digit = 0;
            string sValue = "";
            double dValue = 0.0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDeviationOver2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.SelectedProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                       + myKeyPad.doubleValue;
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                if (dValue < Helper.StringToWeight(this.SelectedProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDeviationOver2.Text = myKeyPad.StringValue;

                    this.SelectedProductItem2.OverRange = sValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveProductFile2(this.SelectedProductItem2, this.SelectedProductNo - 1);

                    this.DisplayControls2(this.SelectedProductItem2);
                }
            }
        }
        private void labelDeviationUnder2_Click(object sender, EventArgs e)
        {
            int digit = 0;
            string sValue = "";
            double dValue = 0.0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDeviationUnder2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.SelectedProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                       - Math.Abs(myKeyPad.doubleValue);
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                if (dValue > Helper.StringToWeight(this.SelectedProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDeviationUnder2.Text = myKeyPad.StringValue;

                    this.SelectedProductItem2.UnderRange = sValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveProductFile2(this.SelectedProductItem2, this.SelectedProductNo - 1);

                    this.DisplayControls2(this.SelectedProductItem2);
                }
            }
        }
        private void labelTareRange2_Click(object sender, EventArgs e)
        {
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelTareRange2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(100, 90);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 99999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelTareRange2.Text = myKeyPad.StringValue;
                    this.SelectedProductItem2.TareRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.SaveProductFile2(this.SelectedProductItem2, this.SelectedProductNo - 1);
                }
            }
        }
        
        private void listBoxProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBoxProductList.SelectedItem == null)
                return;

            this.SelectedProductNo = this.listBoxProductList.SelectedIndex + 1;
            this.buttonProductNo.Text = this.SelectedProductNo.ToString();

            this.ParentForm.ParentForm.LoadProductFile1(ref this.SelectedProductItem1, this.SelectedProductNo - 1);
            this.ParentForm.ParentForm.LoadProductFile2(ref this.SelectedProductItem2, this.SelectedProductNo - 1);

            this.DisplayControls1(this.SelectedProductItem1);
            this.DisplayControls2(this.SelectedProductItem2);
        }

        private void smartKeyboard_OnXKeyClick(object sender, EventArgs e)
        {
            this.smartKeyboard.Hide();
            this.ParentForm.ParentForm.LoadProductFile1(ref this.SelectedProductItem1, this.SelectedProductNo - 1);
            this.DisplayControls1(this.SelectedProductItem1);
            this.buttonNameEdit.Enabled = true;
            this.buttonLotEdit.Enabled = true;
            this.textBoxProductName.Enabled = true;
            this.textBoxLotNo.Enabled = true;
            this.listBoxProductList.Enabled = true;
        }
        #endregion
    }
}
