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 SmartX;
using ITC81DB.DialogForms;
using ITC81DB.Forms;
using ITC81DB_ImageDll;

namespace ITC81DB.Controls
{
    public partial class ControlMainLineGraph : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion
        
        #region Constructor
        public ControlMainLineGraph(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.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLinePassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLinePassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLinePassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLineOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLineOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLineOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLinePassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLinePassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLinePassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLineUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLineUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunLineUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLineOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLineOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLineOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLinePassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLinePassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLinePassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLineUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLineUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunLineUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderUp));
            }
            else
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLinePassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLinePassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLinePassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineUnderUp));
            }
        }
        private void DefaultSetting()
        {
            this.InitializeDraw();
        }

        private void InitializeDraw()
        {
            SmartDraw.CHARTREFRESH charRefreshType = SmartDraw.CHARTREFRESH.LEFTSCROLL;
            SmartDraw.CHARTPENSTYLE[] charPenStyle;
            charPenStyle = new SmartX.SmartDraw.CHARTPENSTYLE[1];
            charPenStyle[0].m_chColor = Color.White;
            charPenStyle[0].m_iPenWidth = 2;


            this.draw.SetChartCfg(0, 198, 420, 198, 5, charRefreshType, 1);
            this.draw.ChartChannelPenStyle = charPenStyle;
            this.draw.ChartDrawStep = 1;
        }
        private void SetDrawData(string stringWeight, ProductItem product)
        {
            int value = 0, gap = 0, startingPoint = 0, weight = 0;

            try
            {
                weight = int.Parse(stringWeight);
                gap = int.Parse(product.OverRange.Replace(".", "")) - int.Parse(product.UnderRange.Replace(".", ""));
                startingPoint = int.Parse(product.UnderRange.Replace(".", "")) - gap;

                value = weight - startingPoint;
                value = (value * 66) / gap;

                if (value > 197)
                    value = 197;
                if (value < 1)
                    value = 1;

                draw.PutData(value);
            }
            catch
            {
            }
        }
        private void SetProduct(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    break;
                case DataStore.WeightInputMode.Deviation:
                    if (item.UnderRangeDeviation == 0)
                        underValue = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                    else
                        underValue = Helper.StringToDecimalPlaces(item.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

            if (this.buttonUnderRange.Text != underValue)
                this.buttonUnderRange.Text = underValue;

            if (this.buttonPassRange.Text != passValue)
                this.buttonPassRange.Text = passValue;

            if (this.buttonOverRange.Text != overValue)
                this.buttonOverRange.Text = overValue;
        }
        private void SetCount(WeightData data)
        {
            string value = "";

            value = data.UnderCount.ToString();
            if (this.labelUnderCount.Text != value)
                this.labelUnderCount.Text = value;

            value = data.PassCount.ToString();
            if (this.labelPassCount.Text != value)
                this.labelPassCount.Text = value;

            value = data.OverCount.ToString();
            if (this.labelOverCount.Text != value)
                this.labelOverCount.Text = value;

            value = data.UnderRatio;
            if (this.labelUnderRatio.Text != value)
                this.labelUnderRatio.Text = value;

            value = data.PassRatio;
            if (this.labelPassRatio.Text != value)
                this.labelPassRatio.Text = value;

            value = data.OverRatio;
            if (this.labelOverRatio.Text != value)
                this.labelOverRatio.Text = value;
        }
        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case DataStore.UserGroup.LogOut:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    break;
                case DataStore.UserGroup.Level1Operator:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    break;
                case DataStore.UserGroup.Level2Engineer:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    break;
                case DataStore.UserGroup.Level3Manager:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    break;
                case DataStore.UserGroup.Level4Developer:
                    this.buttonUnderRange.Enabled = true;
                    this.buttonPassRange.Enabled = true;
                    this.buttonOverRange.Enabled = true;
                    break;
                case DataStore.UserGroup.NotLogin:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    break;
                default:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    break;
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {

        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetProduct(pItem);

            this.SetCount(wData);

            this.draw.PutDataAllClear();
        }
        public void UpdateStartWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            this.SetCount(data);
            this.SetDrawData(data.WeightString, this.ParentForm.ParentForm.CurrentProductItem);
        }

        public void Clear()
        {
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
            this.draw.PutDataAllClear();
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplayUser(status.CurrentUser);

            this.SetProduct(this.ParentForm.ParentForm.CurrentProductItem);
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
        }
        #endregion

        #region Event Handler
        private void buttonUnderRange_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.ParentForm.SystemConfig1.ProductNumber >= 996
                && this.ParentForm.ParentForm.SystemConfig1.ProductNumber <= 1000)
            {
                DialogFormMessage myMsg = new DialogFormMessage(27, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
                return;
            }
            
            string value = "", sValue = "";
            double dValue = 0.0;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonUnderRange.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces,
               false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case DataStore.WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange,
                        this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces) - Math.Abs(myKeyPad.doubleValue);
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange,
                this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces) || dValue < 0)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem.UnderRange = sValue.Replace(".", "");

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case DataStore.WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case DataStore.WeightInputMode.Deviation:
                        if (myKeyPad.doubleValue == 0)
                            value = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                        else
                            value = myKeyPad.StringValue;
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }

                this.buttonUnderRange.Text = value;
                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);

                if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                {
                    string feedbackValue = "";

                    if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange = this.ParentForm.ParentForm.CurrentProductItem.UnderRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem.UnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation.ToString();

                        this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT1UnderRange, feedbackValue);
                    }
                }
                if (this.ParentForm.ParentForm.SystemConfig1.IsAverageTracking == true)
                {
                    if (int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitUnder) > this.ParentForm.ParentForm.CurrentProductItem.UnderRangeInt)
                    {
                        this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitUnder = this.ParentForm.ParentForm.CurrentProductItem.UnderRange;
                        this.ParentForm.ParentForm.SaveAverageTrackingFile(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                    }
                }

                this.ParentForm.ParentForm.Update30000ModbusItem();
                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.UnderRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UnderRange, value);
            }
        }
        private void buttonPassRange_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.ParentForm.SystemConfig1.ProductNumber >= 996
                && this.ParentForm.ParentForm.SystemConfig1.ProductNumber <= 1000)
            {
                DialogFormMessage myMsg = new DialogFormMessage(27, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
                return;
            }
            
            string value = "", underRange = "", overRange = "", viewValue = "";
            int digit = 0, temp = 0, range = 0;
            int oldUnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;
            int oldOverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;

            if (this.ParentForm.ParentForm.CurrentWeightData.Weight < 1)
                viewValue = this.buttonPassRange.Text;
            else
                viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(viewValue, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces,
                     false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

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

            #region UnderRange
            temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldUnderRangeDeviation;
            if (temp < 0)
                value = "0";
            else
                value = temp.ToString();

            this.ParentForm.ParentForm.CurrentProductItem.UnderRange = value;
            #endregion

            #region OverRange
            // V6.2.0
            //if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //    range = 1000000;
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //    range = 100000;
            //else
            //    range = 10000;
            temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldOverRangeDeviation;
            if (temp > 99999)
                value = "99999";
            else
                value = temp.ToString();

            this.ParentForm.ParentForm.CurrentProductItem.OverRange = value;

            #endregion

            this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);
            this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
            this.ParentForm.ParentForm.TransferCurrentProductItem(this.ParentForm.ParentForm.CurrentProductItem);

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    #region WeightMode
                    underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    #endregion
                    break;
                case DataStore.WeightInputMode.Deviation:
                    #region DeviationMode
                    if (this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation == 0)
                        underRange = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                    else
                        underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overRange = string.Format("+{0}", overRange);
                    #endregion
                    break;
                default:
                    break;
            }

            this.ParentForm.ParentForm.Update30000ModbusItem();
            if (this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.IsUsingFeedback() == true)
                this.ParentForm.ParentForm.UpdateFeedbackData(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter2);       

            this.buttonPassRange.Text = myKeyPad.StringValue;
            this.buttonUnderRange.Text = underRange;
            this.buttonOverRange.Text = overRange;
        }
        private void buttonOverRange_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.ParentForm.SystemConfig1.ProductNumber >= 996
                && this.ParentForm.ParentForm.SystemConfig1.ProductNumber <= 1000)
            {
                DialogFormMessage myMsg = new DialogFormMessage(27, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
                return;
            }

            string value = "", sValue = "";
            double dValue = 0.0;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonOverRange.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case DataStore.WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)
                        + myKeyPad.doubleValue;
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces) ||
                dValue > 99999)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
            }
            else
            {
                string temp = "";
                this.ParentForm.ParentForm.CurrentProductItem.OverRange = sValue.Replace(".", "");
               
                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case DataStore.WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case DataStore.WeightInputMode.Deviation:
                        if (myKeyPad.StringValue.Contains('+') == true)
                            temp = myKeyPad.StringValue.Replace("+", "");
                        else
                            temp = myKeyPad.StringValue;
                        value = string.Format("+{0}", temp);
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }
                this.buttonOverRange.Text = value;
                this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);
                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

                if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                {
                    string feedbackValue = "";

                    if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange = this.ParentForm.ParentForm.CurrentProductItem.OverRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem.OverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString();

                        this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT1OverRange, feedbackValue);
                    }
                }
                if (this.ParentForm.ParentForm.SystemConfig1.IsAverageTracking == true)
                {
                    if (int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitOver) < this.ParentForm.ParentForm.CurrentProductItem.OverRangeInt)
                    {
                        this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitOver = this.ParentForm.ParentForm.CurrentProductItem.OverRange;
                        this.ParentForm.ParentForm.SaveAverageTrackingFile(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                    }
                }

                this.ParentForm.ParentForm.Update30000ModbusItem();
                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.OverRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OverRange, value);
            }
        }
        #endregion
    }
}
