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_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H.Controls
{
    public partial class ControlCenterSystemSorterSetting : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        #endregion

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

            this.ParentForm = parent;

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

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

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

            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundSorterSetting));

                this.radioButtonDoubleEntryNone.Text = this.radioButtonExternalInputNone.Text = this.radioButtonEtcNgNone.Text = "None";
                this.buttonTestSorterA.Font = this.buttonTestSorterB.Font = new Font("새굴림", 16, FontStyle.Bold);
                this.buttonTestSorterA.Text = "Sorter A";
                this.buttonTestSorterB.Text = "Sorter B";
                this.radioButtonDoubleEntrySorterA.Text = this.radioButtonExternalInputSorterA.Text = this.radioButtonEtcNgSorterA.Text = "Sorter A";
                this.radioButtonDoubleEntrySorterB.Text = this.radioButtonExternalInputSorterB.Text = this.radioButtonEtcNgSorterB.Text = "Sorter B";

                this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
                this.comboBoxSorterA.Items.Clear();
                this.comboBoxSorterA.Items.Add("None");
                this.comboBoxSorterA.Items.Add("Over");
                this.comboBoxSorterA.Items.Add("Under");
                this.comboBoxSorterA.Items.Add("NG");
                this.comboBoxSorterA.SelectedIndex = 1;
                this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

                this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
                this.comboBoxSorterB.Items.Clear();
                this.comboBoxSorterB.Items.Add("None");
                this.comboBoxSorterB.Items.Add("Over");
                this.comboBoxSorterB.Items.Add("Under");
                this.comboBoxSorterB.Items.Add("NG");
                this.comboBoxSorterB.SelectedIndex = 2;
                this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundSorterSetting));

                this.radioButtonDoubleEntryNone.Text = this.radioButtonExternalInputNone.Text = this.radioButtonEtcNgNone.Text = "不使用";
                this.buttonTestSorterA.Font = this.buttonTestSorterB.Font = new Font("새굴림", 16, FontStyle.Bold);
                this.buttonTestSorterA.Text = "分类机A";
                this.buttonTestSorterB.Text = "分类机B";
                this.radioButtonDoubleEntrySorterA.Text = this.radioButtonExternalInputSorterA.Text = this.radioButtonEtcNgSorterA.Text = "分类机A";
                this.radioButtonDoubleEntrySorterB.Text = this.radioButtonExternalInputSorterB.Text = this.radioButtonEtcNgSorterB.Text = "分类机B";

                this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
                this.comboBoxSorterA.Items.Clear();
                this.comboBoxSorterA.Items.Add("不使用");
                this.comboBoxSorterA.Items.Add("超过");
                this.comboBoxSorterA.Items.Add("低于");
                this.comboBoxSorterA.Items.Add("不良");
                this.comboBoxSorterA.SelectedIndex = 1;
                this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

                this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
                this.comboBoxSorterB.Items.Clear();
                this.comboBoxSorterB.Items.Add("不使用");
                this.comboBoxSorterB.Items.Add("超过");
                this.comboBoxSorterB.Items.Add("低于");
                this.comboBoxSorterB.Items.Add("不良");
                this.comboBoxSorterB.SelectedIndex = 2;
                this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundSorterSetting));

                this.radioButtonDoubleEntryNone.Text = this.radioButtonExternalInputNone.Text = this.radioButtonEtcNgNone.Text = "Нет";
                this.buttonTestSorterA.Font = this.buttonTestSorterB.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.buttonTestSorterA.Text = "СортировщикА";
                this.buttonTestSorterB.Text = "СортировщикB";
                this.radioButtonDoubleEntrySorterA.Text = this.radioButtonExternalInputSorterA.Text = this.radioButtonEtcNgSorterA.Text = "СортировщикА";
                this.radioButtonDoubleEntrySorterB.Text = this.radioButtonExternalInputSorterB.Text = this.radioButtonEtcNgSorterB.Text = "СортировщикB";

                this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
                this.comboBoxSorterA.Items.Clear();
                this.comboBoxSorterA.Items.Add("Нет");
                this.comboBoxSorterA.Items.Add("Много");
                this.comboBoxSorterA.Items.Add("Мало");
                this.comboBoxSorterA.Items.Add("Дефект");
                this.comboBoxSorterA.SelectedIndex = 1;
                this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

                this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
                this.comboBoxSorterB.Items.Clear();
                this.comboBoxSorterB.Items.Add("Нет");
                this.comboBoxSorterB.Items.Add("Много");
                this.comboBoxSorterB.Items.Add("Мало");
                this.comboBoxSorterB.Items.Add("Дефект");
                this.comboBoxSorterB.SelectedIndex = 2;
                this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundSorterSetting));

                this.radioButtonDoubleEntryNone.Text = this.radioButtonExternalInputNone.Text = this.radioButtonEtcNgNone.Text = "Keiner";
                this.buttonTestSorterA.Font = this.buttonTestSorterB.Font = new Font("새굴림", 16, FontStyle.Bold);
                this.buttonTestSorterA.Text = "SortiererA";
                this.buttonTestSorterB.Text = "SortiererB";
                this.radioButtonDoubleEntrySorterA.Text = this.radioButtonExternalInputSorterA.Text = this.radioButtonEtcNgSorterA.Text = "SortiererA";
                this.radioButtonDoubleEntrySorterB.Text = this.radioButtonExternalInputSorterB.Text = this.radioButtonEtcNgSorterB.Text = "SortiererB";

                this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
                this.comboBoxSorterA.Items.Clear();
                this.comboBoxSorterA.Items.Add("Keiner");
                this.comboBoxSorterA.Items.Add("Max.");
                this.comboBoxSorterA.Items.Add("Min.");
                this.comboBoxSorterA.Items.Add("Schlecht");
                this.comboBoxSorterA.SelectedIndex = 1;
                this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

                this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
                this.comboBoxSorterB.Items.Clear();
                this.comboBoxSorterB.Items.Add("Keiner");
                this.comboBoxSorterB.Items.Add("Max.");
                this.comboBoxSorterB.Items.Add("Min.");
                this.comboBoxSorterB.Items.Add("Schlecht");
                this.comboBoxSorterB.SelectedIndex = 2;
                this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundSorterSetting));

                this.radioButtonDoubleEntryNone.Text = this.radioButtonExternalInputNone.Text = this.radioButtonEtcNgNone.Text = "사용안함";
                this.buttonTestSorterA.Font = this.buttonTestSorterB.Font = new Font("새굴림", 16, FontStyle.Bold);
                this.buttonTestSorterA.Text = "선별기A";
                this.buttonTestSorterB.Text = "선별기B";
                this.radioButtonDoubleEntrySorterA.Text = this.radioButtonExternalInputSorterA.Text = this.radioButtonEtcNgSorterA.Text = "선별기A";
                this.radioButtonDoubleEntrySorterB.Text = this.radioButtonExternalInputSorterB.Text = this.radioButtonEtcNgSorterB.Text = "선별기B";

                this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
                this.comboBoxSorterA.Items.Clear();
                this.comboBoxSorterA.Items.Add("사용안함");
                this.comboBoxSorterA.Items.Add("과량");
                this.comboBoxSorterA.Items.Add("경량");
                this.comboBoxSorterA.Items.Add("NG");
                this.comboBoxSorterA.SelectedIndex = 1;
                this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

                this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
                this.comboBoxSorterB.Items.Clear();
                this.comboBoxSorterB.Items.Add("사용안함");
                this.comboBoxSorterB.Items.Add("과량");
                this.comboBoxSorterB.Items.Add("경량");
                this.comboBoxSorterB.Items.Add("NG");
                this.comboBoxSorterB.SelectedIndex = 2;
                this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            }
        }
        private void DefaultSetting()
        {

        }

        private void UpdateControlsDisplay(JudgmentSetItem item,SystemParameter1 parameter)
        { 
            int value = 0;

            if (this.labelProductNo.Text != this.ParentForm.ParentForm.SystemConfig1.ProductNumber.ToString())
                this.labelProductNo.Text = this.ParentForm.ParentForm.SystemConfig1.ProductNumber.ToString();

            this.buttonTestSorterB.ButtonUp();
            this.buttonTestSorterA.ButtonDown();
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.SorterTest, "0000");

            this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
            this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            // 선별기A 모드
            value = item.Sorter1Mode;
            if (this.comboBoxSorterA.SelectedIndex != value)
                this.comboBoxSorterA.SelectedIndex = value;

            // 선별기A 지연시간
            value = item.Sorter1DelayTime;
            if (this.labelSorterADelayTime.Text != value.ToString())
                this.labelSorterADelayTime.Text = value.ToString();

            // 선별기A 동작시간
            value = item.Sorter1RunTime;
            if (this.labelSorterARunTime.Text != value.ToString())
                this.labelSorterARunTime.Text = value.ToString();

            // 선별기B 모드
            value = item.Sorter2Mode;
            if (this.comboBoxSorterB.SelectedIndex != value)
                this.comboBoxSorterB.SelectedIndex = value;

            // 선별기B 지연시간
            value = item.Sorter2DelayTime;
            if (this.labelSorterBDelayTime.Text != value.ToString())
                this.labelSorterBDelayTime.Text = value.ToString();

            // 선별기B 동작시간
            value = item.Sorter2RunTime;
            if (this.labelSorterBRunTime.Text != value.ToString())
                this.labelSorterBRunTime.Text = value.ToString();

            // 이중진입
            value = int.Parse(parameter.SorterDoubleEntry);
            if (value == 1)
                this.radioButtonDoubleEntrySorterA.Checked = true;
            else if (value == 2)
                this.radioButtonDoubleEntrySorterB.Checked = true;
            else
                this.radioButtonDoubleEntryNone.Checked = true;

            // 외부입력
            value = int.Parse(parameter.SorterExternalNgInput);
            if (value == 1)
                this.radioButtonExternalInputSorterA.Checked = true;
            else if (value == 2)
                this.radioButtonExternalInputSorterB.Checked = true;
            else
                this.radioButtonExternalInputNone.Checked = true;

            // 기타NG
            value = int.Parse(parameter.SorterEtcNg);
            if (value == 1)
                this.radioButtonEtcNgSorterA.Checked = true;
            else if (value == 2)
                this.radioButtonEtcNgSorterB.Checked = true;
            else
                this.radioButtonEtcNgNone.Checked = true;

            this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
            this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            this.buttonSave.Visible = false;
        }
        public void UpdateEquipmentStatusDisplay(Define.E_EquipmentStatus status)
        {
            if (status == Define.E_EquipmentStatus.Start)
            {
                //this.pictureBoxStart.Visible = true;
                //this.pictureBoxStop.Visible = false;
            }
            else
            {
                //this.pictureBoxStart.Visible = false;
                //this.pictureBoxStop.Visible = true;
            }
        }

        public void UpdateCurrentProductDisplay(Define.E_EquipmentStatus status, ProductItem pItem)
        {
            string value = "";

            value = pItem.Number.ToString();
            if (this.labelProductNo.Text != value)
                this.labelProductNo.Text = value;

            this.UpdateControlsDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.CurrentSystemParameter1);
        }

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

            this.UpdateControlsDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.CurrentSystemParameter1);
        }
        #endregion

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

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > this.ParentForm.ParentForm.ProductCount)
                {
                    // 입력범위를 확인하세요
                    message = string.Format("1 ~ {0}", this.ParentForm.ParentForm.ProductCount);
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelProductNo.Text = myKeyPad.StringValue;

                    this.labelProductNo.Text = "**";
                    this.ParentForm.ParentForm.TransferProductParameter(myKeyPad.IntValue);
                }
            }
        }

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

            if (this.buttonTestSorterA.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                value = "0000";
            else if (this.buttonTestSorterB.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                value = "0001";

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.SorterTest, value);
        }

        private void labelSorterADelayTime_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterADelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorterARunTime_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterARunTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorterBDelayTime_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterBDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorterBRunTime_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterBRunTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            string before1Mode = "", after1Mode = "";
            string before1Delay = "", after1Delay = "";
            string before1Run = "", after1Run = "";
            string before2Mode = "", after2Mode = "";
            string before2Delay = "", after2Delay = "";
            string before2Run = "", after2Run = "";

            before1Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode.ToString();
            before1Delay = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime.ToString();
            before1Run = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime.ToString();
            before2Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode.ToString();
            before2Delay = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime.ToString();
            before2Run = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime.ToString();

            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode = this.comboBoxSorterA.SelectedIndex;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime = int.Parse(this.labelSorterADelayTime.Text);
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime = int.Parse(this.labelSorterARunTime.Text);
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode = this.comboBoxSorterB.SelectedIndex;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime = int.Parse(this.labelSorterBDelayTime.Text);
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime = int.Parse(this.labelSorterBRunTime.Text);
            this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

            after1Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode.ToString();
            after1Delay = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime.ToString();
            after1Run = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime.ToString();
            after2Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode.ToString();
            after2Delay = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime.ToString();
            after2Run = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime.ToString();

            if (before1Mode != after1Mode)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorAMode, "", before1Mode, after1Mode);
            if (before1Delay != after1Delay)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorADelay, "", before1Delay, after1Delay);
            if (before1Run != after1Run)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorARun, "", before1Run, after1Run);
            if (before2Mode != after2Mode)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorBMode, "", before2Mode, after2Mode);
            if (before2Delay != after2Delay)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorBDelay, "", before2Delay, after2Delay);
            if (before2Run != after2Run)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorBRun, "", before2Run, after2Run);

            StringBuilder sb = new StringBuilder();
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime.ToString()));
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UserDefined1, sb.ToString());

            this.buttonSave.Visible = false;
        }

        private void radioButtonDoubleEntry_Click(object sender, EventArgs e)
        {
            string before = "", after = "";
            string value = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter1.SorterDoubleEntry;

            if (this.radioButtonDoubleEntryNone.Checked == true)
                value = "0000";
            else if (this.radioButtonDoubleEntrySorterA.Checked == true)
                value = "0001";
            else if (this.radioButtonDoubleEntrySorterB.Checked == true)
                value = "0002";
            else
                value = "0000";

            after = this.ParentForm.ParentForm.CurrentSystemParameter1.SorterDoubleEntry = value;
            this.ParentForm.ParentForm.SaveSystemParameter1File(this.ParentForm.ParentForm.CurrentSystemParameter1);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DoubleEnter, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorDoubleEntry, "", before, after);
        }
        private void radioButtonExternalInput_Click(object sender, EventArgs e)
        {
            string before = "", after = "";
            string value = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter1.SorterExternalNgInput;

            if (this.radioButtonExternalInputNone.Checked == true)
                value = "0000";
            else if (this.radioButtonExternalInputSorterA.Checked == true)
                value = "0001";
            else if (this.radioButtonExternalInputSorterB.Checked == true)
                value = "0002";
            else
                value = "0000";

            after = this.ParentForm.ParentForm.CurrentSystemParameter1.SorterExternalNgInput = value;
            this.ParentForm.ParentForm.SaveSystemParameter1File(this.ParentForm.ParentForm.CurrentSystemParameter1);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.ExternalInput, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorExternalInput, "", before, after);
        }
        private void radioButtonEtcNg_Click(object sender, EventArgs e)
        {
            string before = "", after = "";
            string value = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter1.SorterEtcNg;

            if (this.radioButtonEtcNgNone.Checked == true)
                value = "0000";
            else if (this.radioButtonEtcNgSorterA.Checked == true)
                value = "0001";
            else if (this.radioButtonEtcNgSorterB.Checked == true)
                value = "0002";
            else
                value = "0000";

            after = this.ParentForm.ParentForm.CurrentSystemParameter1.SorterEtcNg = value;
            this.ParentForm.ParentForm.SaveSystemParameter1File(this.ParentForm.ParentForm.CurrentSystemParameter1);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.ExternalNG, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.SorExternalNG, "", before, after);
        }

        private void comboBoxSorterA_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxSorterA.Items.Count == 0 || this.comboBoxSorterB.Items.Count == 0)
                return;

            if (this.buttonSave.Visible != true)
                this.buttonSave.Visible = true;

            this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            if (this.comboBoxSorterA.SelectedIndex == 1)
                this.comboBoxSorterB.SelectedIndex = 2;
            else if (this.comboBoxSorterA.SelectedIndex == 2)
                this.comboBoxSorterB.SelectedIndex = 1;
            else if (this.comboBoxSorterA.SelectedIndex == 3)
                this.comboBoxSorterB.SelectedIndex = 0;

            this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
        }
        private void comboBoxSorterB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxSorterA.Items.Count == 0 || this.comboBoxSorterB.Items.Count == 0)
                return;

            if (this.buttonSave.Visible != true)
                this.buttonSave.Visible = true;

            this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

            if (this.comboBoxSorterB.SelectedIndex == 1)
                this.comboBoxSorterA.SelectedIndex = 2;
            else if (this.comboBoxSorterB.SelectedIndex == 2)
                this.comboBoxSorterA.SelectedIndex = 1;
            else if (this.comboBoxSorterB.SelectedIndex == 3)
                this.comboBoxSorterA.SelectedIndex = 0;

            this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
        }
        #endregion
    }
}
