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 ControlMainBarGraph : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion
        
        #region Constructor
        public ControlMainBarGraph(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.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunScreen));

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainStopScreen));

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainStopScreen));

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunScreen));

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunScreen));

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareUp));
            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunScreen));

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunTareUp));
            }
        }
        private void DefaultSetting()
        {

        }

        public void RescaleProgressBar(ProductItem item)
        {
            int value = 0;

            value = item.ProgressBarMaximum;
            if (this.progressBar.Maximum != value)
                this.progressBar.Maximum = value;

            value = item.ProgressBarMinimum;
            if (this.progressBar.Minimum != value)
                this.progressBar.Minimum = value;

            this.progressBar.Value = this.progressBar.Minimum;
        }
        private void SetProduct(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "", tareValue = "";

            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);
                    tareValue = Helper.StringToDecimalPlaces(item.TareRange, 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);
                    tareValue = Helper.StringToDecimalPlaces(item.TareRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    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;

            if (this.buttonTareRange.Text != tareValue)
                this.buttonTareRange.Text = tareValue;
        }
        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.ExNGCount.ToString();
            if (this.labelExNGCount.Text != value)
                this.labelExNGCount.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount.Text != value)
                this.labelTotalCount.Text = value;
        }
        private void SetProgressBarValue(SmartProgressBar progressBar, double weight, DataStore.JudgmentStatus judStatus, int decimalPlaces)
        {
            int value = 0;

            if (decimalPlaces == 0)
                value = int.Parse(string.Format("{0}", weight));
            else if (decimalPlaces == 1)
                value = int.Parse(string.Format("{0}", weight * 10));
            else if (decimalPlaces == 2)
                value = int.Parse(string.Format("{0}", weight * 100));
            else if (decimalPlaces == 3)
                value = int.Parse(string.Format("{0}", weight * 1000));

            if (progressBar.Value != value)
                progressBar.Value = value;

            if (judStatus == DataStore.JudgmentStatus.Over)
            {
                if (progressBar.BarColor1 != Color.Yellow)
                    progressBar.BarColor1 = Color.Yellow;
            }
            else if (judStatus == DataStore.JudgmentStatus.Under || judStatus == DataStore.JudgmentStatus.Double
                || judStatus == DataStore.JudgmentStatus.ExNg || judStatus == DataStore.JudgmentStatus.Metal
                || judStatus == DataStore.JudgmentStatus.ExNg1 || judStatus == DataStore.JudgmentStatus.ExNg2
                || judStatus == DataStore.JudgmentStatus.LengthError)
            {
                if (progressBar.BarColor1 != Color.Red)
                    progressBar.BarColor1 = Color.Red;
            }
            else
            {
                if (progressBar.BarColor1 != Color.Lime)
                    progressBar.BarColor1 = Color.Lime;
            }
        }
        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case DataStore.UserGroup.LogOut:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    this.buttonTareRange.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;
                    this.buttonTareRange.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;
                    this.buttonTareRange.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;
                    this.buttonTareRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    break;
                case DataStore.UserGroup.Level4Developer:
                    this.buttonUnderRange.Enabled = true;
                    this.buttonPassRange.Enabled = true;
                    this.buttonOverRange.Enabled = true;
                    this.buttonTareRange.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;
                    this.buttonTareRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    break;
                default:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    this.buttonTareRange.Enabled = false;
                    break;
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            this.labelSpeed.Text = "0";
            this.progressBar.Value = this.progressBar.Minimum;
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetProduct(pItem);
            this.SetCount(wData);

            this.RescaleProgressBar(pItem);
        }
        public void UpdateStartWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";
            
            if (data.ProductionSpeed <= 500)
            {
                value = data.ProductionSpeed.ToString();
                if (this.labelSpeed.Text != value)
                    this.labelSpeed.Text = value;
            }

            this.SetProgressBarValue(this.progressBar, data.Weight, data.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

            this.SetCount(data);
        }

        public void Clear()
        {
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);

            this.progressBar.Value = this.progressBar.Minimum;
            this.labelSpeed.Text = "0";
        }
        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.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.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;
            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
            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);
            }
        }
        private void buttonTareRange_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 = "";

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

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

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

                    // NET Icon 표시
                    this.ParentForm.MainWeightBigScreen.SetIconNET(this.ParentForm.ParentForm.CurrentProductItem);
                    this.ParentForm.MainWeightSmallScreen.SetIconNET(this.ParentForm.ParentForm.CurrentProductItem);

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