using System;
using System.Linq;
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_ImageDll;
using ITC81DB_2.Forms;
using SmartX;

namespace ITC81DB_2.Controls
{
    public partial class ControlMainFeedback : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainFeedback(FormMainDisplay parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay 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.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "Weight";
                this.labelTitleDeviation1.Text = this.labelTitleDeviation2.Text = "Dev.";

                this.labelTitleOver1.Text = this.labelTitleOver2.Text = "Over";
                this.labelTitlePass1.Text = this.labelTitlePass2.Text = "Pass";
                this.labelTitleUnder1.Text = this.labelTitleUnder2.Text = "Under";

                this.labelTitleDelayCount1.Text = this.labelTitleDelayCount2.Text = "Ignore";
                this.labelTitleCurrentDelayCount1.Text = this.labelTitleCurrentDelayCount2.Text = "Current";
                this.labelTitleSampleCount1.Text = this.labelTitleSampleCount2.Text = "Sample";
                this.labelTitleCurrentSampleCount1.Text = this.labelTitleCurrentSampleCount2.Text = "Current";

                this.labelTitleFeedback1.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleFeedback2.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleFeedback1.Text = this.labelTitleFeedback2.Text = "Feedback";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "重量";
                this.labelTitleDeviation1.Text = this.labelTitleDeviation2.Text = "误差";

                this.labelTitleOver1.Text = this.labelTitleOver2.Text = "上限";
                this.labelTitlePass1.Text = this.labelTitlePass2.Text = "标准";
                this.labelTitleUnder1.Text = this.labelTitleUnder2.Text = "下限";

                this.labelTitleDelayCount1.Text = this.labelTitleDelayCount2.Text = "延迟计数";
                this.labelTitleCurrentDelayCount1.Text = this.labelTitleCurrentDelayCount2.Text = "当前值";
                this.labelTitleSampleCount1.Text = this.labelTitleSampleCount2.Text = "取样计数";
                this.labelTitleCurrentSampleCount1.Text = this.labelTitleCurrentSampleCount2.Text = "当前值";

                this.labelTitleFeedback1.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleFeedback2.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleFeedback1.Text = this.labelTitleFeedback2.Text = "反馈";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "Peso";
                this.labelTitleDeviation1.Text = this.labelTitleDeviation2.Text = "Desv.";

                this.labelTitleOver1.Text = this.labelTitleOver2.Text = "Superior";
                this.labelTitlePass1.Text = this.labelTitlePass2.Text = "Referencia";
                this.labelTitleUnder1.Text = this.labelTitleUnder2.Text = "Inferior";

                this.labelTitleDelayCount1.Text = this.labelTitleDelayCount2.Text = "Ignorar";
                this.labelTitleCurrentDelayCount1.Text = this.labelTitleCurrentDelayCount2.Text = "Actual";
                this.labelTitleSampleCount1.Text = this.labelTitleSampleCount2.Text = "Muestra";
                this.labelTitleCurrentSampleCount1.Text = this.labelTitleCurrentSampleCount2.Text = "Actual";

                this.labelTitleFeedback1.Font = new Font("새굴림", 8, FontStyle.Bold);
                this.labelTitleFeedback2.Font = new Font("새굴림", 8, FontStyle.Bold);
                this.labelTitleFeedback1.Text = this.labelTitleFeedback2.Text = "Retroalimentación";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.German)
            {

            }
            else
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "중량";
                this.labelTitleDeviation1.Text = this.labelTitleDeviation2.Text = "편차";

                this.labelTitleOver1.Text = this.labelTitleOver2.Text = "상한";
                this.labelTitlePass1.Text = this.labelTitlePass2.Text = "기준";
                this.labelTitleUnder1.Text = this.labelTitleUnder2.Text = "하한";

                this.labelTitleDelayCount1.Text = this.labelTitleDelayCount2.Text = "무시개수";
                this.labelTitleCurrentDelayCount1.Text = this.labelTitleCurrentDelayCount2.Text = "현재값";
                this.labelTitleSampleCount1.Text = this.labelTitleSampleCount2.Text = "샘플개수";
                this.labelTitleCurrentSampleCount1.Text = this.labelTitleCurrentSampleCount2.Text = "현재값";

                this.labelTitleFeedback1.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleFeedback2.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleFeedback1.Text = this.labelTitleFeedback2.Text = "피드백값";
            }
        }
        private void DefaultSetting()
        {
        }

        public bool IsUsingFeedback()
        {
            try
            {
                int value = int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter9507.OptionBoard);
                if (value == 1 && int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1SamplingCount) != 0)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }
        public string CaculateDeviation1(int value)
        {
            string sValue = "";

            sValue = (value - this.ParentForm.ParentForm.CurrentProductItem1.PassRangeInt).ToString();

            return sValue;
        }
        public string CaculateDeviation2(int value)
        {
            string sValue = "";

            sValue = (value - this.ParentForm.ParentForm.CurrentProductItem2.PassRangeInt).ToString();

            return sValue;
        }

        public void ItemChangeEnable(bool value)
        {
            if (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1IsWeightSetting == "1")
            {
                if (value == true)
                {
                    this.labelUpperLimit1.ForeColor = Color.White;
                    this.labelLowerLimit1.ForeColor = Color.White;
                    this.labelUpperLimitDev1.ForeColor = Color.White;
                    this.labelLowerLimitDev1.ForeColor = Color.White;

                    this.labelUpperLimit2.ForeColor = Color.White;
                    this.labelLowerLimit2.ForeColor = Color.White;
                    this.labelUpperLimitDev2.ForeColor = Color.White;
                    this.labelLowerLimitDev2.ForeColor = Color.White;
                }
                else
                {
                    this.labelUpperLimit1.ForeColor = Color.DarkGray;
                    this.labelLowerLimit1.ForeColor = Color.DarkGray;
                    this.labelUpperLimitDev1.ForeColor = Color.DarkGray;
                    this.labelLowerLimitDev1.ForeColor = Color.DarkGray;

                    this.labelUpperLimit2.ForeColor = Color.DarkGray;
                    this.labelLowerLimit2.ForeColor = Color.DarkGray;
                    this.labelUpperLimitDev2.ForeColor = Color.DarkGray;
                    this.labelLowerLimitDev2.ForeColor = Color.DarkGray;
                }

                this.labelUpperLimit1.Enabled = value;
                this.labelLowerLimit1.Enabled = value;
                this.labelUpperLimitDev1.Enabled = value;
                this.labelLowerLimitDev1.Enabled = value;

                this.labelUpperLimit2.Enabled = value;
                this.labelLowerLimit2.Enabled = value;
                this.labelUpperLimitDev2.Enabled = value;
                this.labelLowerLimitDev2.Enabled = value;
            }

            if (value == true)
            {
                this.labelSampleCount1.ForeColor = Color.White;
                this.labelDelayCount1.ForeColor = Color.White;
                this.labelSampleCount2.ForeColor = Color.White;
                this.labelDelayCount2.ForeColor = Color.White;
            }
            else
            {
                this.labelSampleCount1.ForeColor = Color.DarkGray;
                this.labelDelayCount1.ForeColor = Color.DarkGray;
                this.labelSampleCount2.ForeColor = Color.DarkGray;
                this.labelDelayCount2.ForeColor = Color.DarkGray;
            }

            this.labelSampleCount1.Enabled = value;
            this.labelDelayCount1.Enabled = value;

            this.labelSampleCount2.Enabled = value;
            this.labelDelayCount2.Enabled = value;
        }
        public void UpdateDisplayCurrentValue1(FeedbackItem parameter)
        {
            string sValue = "";

            sValue = parameter.SampleCount.Trim();
            if (this.labelCurrentSampleCount1.Text != sValue)
                this.labelCurrentSampleCount1.Text = sValue;

            sValue = parameter.DelayCount.Trim();
            if (this.labelCurrentDelayCount1.Text != sValue)
                this.labelCurrentDelayCount1.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(parameter.FeedbackWeight.Trim(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            sValue = parameter.Sign.Trim() + sValue;
            if (this.labelCurrentFeedback1.Text != sValue)
                this.labelCurrentFeedback1.Text = sValue;
        }
        public void UpdateDisplayCurrentValue2(FeedbackItem parameter)
        {
            string sValue = "";

            sValue = parameter.SampleCount.Trim();
            if (this.labelCurrentSampleCount2.Text != sValue)
                this.labelCurrentSampleCount2.Text = sValue;

            sValue = parameter.DelayCount.Trim();
            if (this.labelCurrentDelayCount2.Text != sValue)
                this.labelCurrentDelayCount2.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(parameter.FeedbackWeight.Trim(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            sValue = parameter.Sign.Trim() + sValue;
            if (this.labelCurrentFeedback2.Text != sValue)
                this.labelCurrentFeedback2.Text = sValue;
        }
        private void UpdateDisplay(SystemParameter9508 parameter)
        {
            string sValue = "";

            // 샘플 개수
            sValue = parameter.OPT1SamplingCount;
            if (this.labelSampleCount1.Text != sValue)
                this.labelSampleCount1.Text = this.labelSampleCount2.Text = sValue;

            // 지연 개수
            sValue = parameter.OPT1DelayCount;
            if (this.labelDelayCount1.Text != sValue)
                this.labelDelayCount1.Text = this.labelDelayCount2.Text = sValue;

            #region 1열
            // 기준
            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelStandard1.Text != sValue)
                this.labelStandard1.Text = this.labelStandard1_1.Text = sValue;

            // 피드백 상한
            sValue = Helper.StringToDecimalPlaces(parameter.OPT1OverRange1, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUpperLimit1.Text != sValue)
                this.labelUpperLimit1.Text = sValue;

            // 피드백 하한
            sValue = Helper.StringToDecimalPlaces(parameter.OPT1UnderRange1, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelLowerLimit1.Text != sValue)
                this.labelLowerLimit1.Text = sValue;

            // 상한편차
            sValue = '+' + Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem1.OverRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUpperLimitDev1.Text != sValue)
                this.labelUpperLimitDev1.Text = sValue;

            // 하한편차
            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem1.UnderRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelLowerLimitDev1.Text != sValue)
                this.labelLowerLimitDev1.Text = sValue;
            #endregion

            #region 2열
            // 기준
            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelStandard2.Text != sValue)
                this.labelStandard2.Text = this.labelStandard2_1.Text = sValue;

            // 피드백 상한
            sValue = Helper.StringToDecimalPlaces(parameter.OPT1OverRange2, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUpperLimit2.Text != sValue)
                this.labelUpperLimit2.Text = sValue;

            // 피드백 하한
            sValue = Helper.StringToDecimalPlaces(parameter.OPT1UnderRange2, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelLowerLimit2.Text != sValue)
                this.labelLowerLimit2.Text = sValue;

            // 상한편차
            sValue = '+' + Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem2.OverRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUpperLimitDev2.Text != sValue)
                this.labelUpperLimitDev2.Text = sValue;

            // 하한편차
            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem2.UnderRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelLowerLimitDev2.Text != sValue)
                this.labelLowerLimitDev2.Text = sValue;
            #endregion

            // 사용/미사용
            sValue = parameter.OPT1IsWeightSetting;
            if (int.Parse(sValue) == 0)
            {
                #region 1열
                this.labelUpperLimit1.ForeColor = Color.DarkGray;
                this.labelLowerLimit1.ForeColor = Color.DarkGray;
                this.labelUpperLimitDev1.ForeColor = Color.DarkGray;
                this.labelLowerLimitDev1.ForeColor = Color.DarkGray;
                this.labelUpperLimit1.Enabled = false;
                this.labelLowerLimit1.Enabled = false;
                this.labelUpperLimitDev1.Enabled = false;
                this.labelLowerLimitDev1.Enabled = false; 
                #endregion

                #region 2열
                this.labelUpperLimit2.ForeColor = Color.DarkGray;
                this.labelLowerLimit2.ForeColor = Color.DarkGray;
                this.labelUpperLimitDev2.ForeColor = Color.DarkGray;
                this.labelLowerLimitDev2.ForeColor = Color.DarkGray;
                this.labelUpperLimit2.Enabled = false;
                this.labelLowerLimit2.Enabled = false;
                this.labelUpperLimitDev2.Enabled = false;
                this.labelLowerLimitDev2.Enabled = false;
                #endregion
            }
            else
            {
                #region 1열
                this.labelUpperLimit1.ForeColor = Color.White;
                this.labelLowerLimit1.ForeColor = Color.White;
                this.labelUpperLimitDev1.ForeColor = Color.White;
                this.labelLowerLimitDev1.ForeColor = Color.White;
                this.labelUpperLimit1.Enabled = true;
                this.labelLowerLimit1.Enabled = true;
                this.labelUpperLimitDev1.Enabled = true;
                this.labelLowerLimitDev1.Enabled = true;
                #endregion

                #region 2열
                this.labelUpperLimit2.ForeColor = Color.White;
                this.labelLowerLimit2.ForeColor = Color.White;
                this.labelUpperLimitDev2.ForeColor = Color.White;
                this.labelLowerLimitDev2.ForeColor = Color.White;
                this.labelUpperLimit2.Enabled = true;
                this.labelLowerLimit2.Enabled = true;
                this.labelUpperLimitDev2.Enabled = true;
                this.labelLowerLimitDev2.Enabled = true;
                #endregion
            }
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplay(this.ParentForm.ParentForm.CurrentSystemParameter9508);
            this.UpdateDisplayCurrentValue1(this.ParentForm.ParentForm.CurrentFeedbackItem1);
            this.UpdateDisplayCurrentValue2(this.ParentForm.ParentForm.CurrentFeedbackItem2);
        }
        #endregion

        #region Event Handler
        private void labelSampleCount_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSampleCount1.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    value = Helper.StringZeroFillDigits4(this.labelSampleCount1.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1SamplingCount = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.TransferDataStream
                        (CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6201_OPT1SamplingCount, value);
                }
            }
        }
        private void labelDelayCount_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDelayCount1.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    value = Helper.StringZeroFillDigits4(this.labelDelayCount1.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1DelayCount = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.TransferDataStream
                        (CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6202_OPT1DelayCount, value);
                }
            }
        }

        private void labelUpperLimit1_Click(object sender, EventArgs e)
        {
            string value = "";
            
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelUpperLimit1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    this.ParentForm.ParentForm.CurrentFeedbackItem1.OverRangeDeviation =
                       this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.CaculateDeviation1(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1Int);
                    this.labelUpperLimitDev1.Text = '+' + Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem1.OverRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6205_OPT1OverRange1, value);
                }
            }
        }
        private void labelLowerLimit1_Click(object sender, EventArgs e)
        {
            string value = "";
            
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelLowerLimit1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    this.ParentForm.ParentForm.CurrentFeedbackItem1.UnderRangeDeviation =
                        this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.CaculateDeviation1
                        (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1Int);
                    this.labelLowerLimitDev1.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem1.UnderRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6206_OPT1UnderRange1, value);
                }
            }
        }
        private void labelUpperLimitDev1_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            
            double dValue = 0.0;
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelUpperLimitDev1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                if (dValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                    || dValue >= 10000)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    string temp = "";
                    this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1 = sValue.Replace(".", "");

                    if (myKeyPad.StringValue.Contains('+') == true)
                        temp = myKeyPad.StringValue.Replace("+", "");
                    else
                        temp = myKeyPad.StringValue;
                    value = string.Format("+{0}", temp);

                    this.labelUpperLimitDev1.Text = value;
                    this.labelUpperLimit1.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6205_OPT1OverRange1, value);
                }
            }
        }
        private void labelLowerLimitDev1_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            
            double dValue = 0.0;
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelLowerLimitDev1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                    - Math.Abs(myKeyPad.doubleValue);
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1 = sValue.Replace(".", "");

                    if (myKeyPad.doubleValue == 0)
                        value = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                    else
                        value = myKeyPad.StringValue;

                    this.labelLowerLimitDev1.Text = value;
                    this.labelLowerLimit1.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6206_OPT1UnderRange1, value);
                }
            }
        }

        private void labelUpperLimit2_Click(object sender, EventArgs e)
        {
            string value = "";

            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelUpperLimit2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    this.ParentForm.ParentForm.CurrentFeedbackItem2.OverRangeDeviation =
                       this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.CaculateDeviation2(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2Int);
                    this.labelUpperLimitDev2.Text = '+' + Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem2.OverRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6216_OPT1OverRange2, value);
                }
            }
        }
        private void labelLowerLimit2_Click(object sender, EventArgs e)
        {
            string value = "";

            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelLowerLimit2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    this.ParentForm.ParentForm.CurrentFeedbackItem2.UnderRangeDeviation =
                        this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.CaculateDeviation2
                        (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2Int);
                    this.labelLowerLimitDev2.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem2.UnderRangeDeviation, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6217_OPT1UnderRange2, value);
                }
            }
        }
        private void labelUpperLimitDev2_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";

            double dValue = 0.0;
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelUpperLimitDev2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                if (dValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                    || dValue >= 10000)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    string temp = "";
                    this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2 = sValue.Replace(".", "");

                    if (myKeyPad.StringValue.Contains('+') == true)
                        temp = myKeyPad.StringValue.Replace("+", "");
                    else
                        temp = myKeyPad.StringValue;
                    value = string.Format("+{0}", temp);

                    this.labelUpperLimitDev2.Text = value;
                    this.labelUpperLimit2.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6216_OPT1OverRange2, value);
                }
            }
        }
        private void labelLowerLimitDev2_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";

            double dValue = 0.0;
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //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.labelLowerLimitDev2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                    - Math.Abs(myKeyPad.doubleValue);
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2 = sValue.Replace(".", "");

                    if (myKeyPad.doubleValue == 0)
                        value = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                    else
                        value = myKeyPad.StringValue;

                    this.labelLowerLimitDev2.Text = value;
                    this.labelLowerLimit2.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6217_OPT1UnderRange2, value);
                }
            }
        }
        #endregion
    }
}
