using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LensConnect_Controller
{
    public partial class Form4 : Form
    {
        ManualResetEventSlim evPresetMove = new ManualResetEventSlim();

        public Form4()
        {
            InitializeComponent();
        }
        private void Form4_Load(object sender, EventArgs e)
        {
            PresetStop_btn.Enabled = false;
            if (Form1.Instance.withZoom != true)
            {
                ZoomPos1Val.Enabled = false;
                ZoomPos2Val.Enabled = false;
                ZoomPos3Val.Enabled = false;
                ZoomPos4Val.Enabled = false;
            }
            if (Form1.Instance.withFocus != true)
            {
                FocusPos1Val.Enabled = false;
                FocusPos2Val.Enabled = false;
                FocusPos3Val.Enabled = false;
                FocusPos4Val.Enabled = false;
            }
            if (Form1.Instance.withIris != true)
            {
                IrisPos1Val.Enabled = false;
                IrisPos2Val.Enabled = false;
                IrisPos3Val.Enabled = false;
                IrisPos4Val.Enabled = false;
            }
            if (Form1.Instance.withOptFil != true)
            {
                OptPos1ComboBox.Enabled = false;
                OptPos2ComboBox.Enabled = false;
                OptPos3ComboBox.Enabled = false;
                OptPos4ComboBox.Enabled = false;
            }
            else
            {
                OptPos1ComboBox.Items.Clear();
                OptPos2ComboBox.Items.Clear();
                OptPos3ComboBox.Items.Clear();
                OptPos4ComboBox.Items.Clear();
                int val = LensCtrl.Instance.optFilMaxAddr;
                for (int i = 0; i <= val; i++)
                {
                    OptPos1ComboBox.Items.Add("Opt" + i);
                    OptPos2ComboBox.Items.Add("Opt" + i);
                    OptPos3ComboBox.Items.Add("Opt" + i);
                    OptPos4ComboBox.Items.Add("Opt" + i);
                }
                OptPos1ComboBox.SelectedIndex = 0;
                OptPos2ComboBox.SelectedIndex = 0;
                OptPos3ComboBox.SelectedIndex = 0;
                OptPos4ComboBox.SelectedIndex = 0;
            }
        }

        private void PresetStart_btn_Click(object sender, EventArgs e)
        {
            int CheckCount = 0;
            if (Pos1ChkBox.Checked == true)
            {
                if (Form1.Instance.withZoom == true)
                {
                    if (Form1.SUCCESS != ZoomAddrChk(ZoomPos1Val.Text))
                        return;
                }
                if (Form1.Instance.withFocus == true)
                {
                    if (Form1.SUCCESS != FocusAddrChk(FocusPos1Val.Text))
                        return;
                }
                if (Form1.Instance.withIris == true)
                {
                    if (Form1.SUCCESS != IrisAddrChk(IrisPos1Val.Text))
                        return;
                }
                CheckCount++;
            }
            if (Pos2ChkBox.Checked == true)
            {
                if (Form1.Instance.withZoom == true)
                {
                    if (Form1.SUCCESS != ZoomAddrChk(ZoomPos2Val.Text))
                        return;
                }
                if (Form1.Instance.withFocus == true)
                {
                    if (Form1.SUCCESS != FocusAddrChk(FocusPos2Val.Text))
                        return;
                }
                if (Form1.Instance.withIris == true)
                {
                    if (Form1.SUCCESS != IrisAddrChk(IrisPos2Val.Text))
                        return;
                }
                CheckCount++;
           }
            if (Pos3ChkBox.Checked == true)
            {
                if (Form1.Instance.withZoom == true)
                {
                    if (Form1.SUCCESS != ZoomAddrChk(ZoomPos3Val.Text))
                        return;
                }
                if (Form1.Instance.withFocus == true)
                {
                    if (Form1.SUCCESS != FocusAddrChk(FocusPos3Val.Text))
                        return;
                }
                if (Form1.Instance.withIris == true)
                {
                    if (Form1.SUCCESS != IrisAddrChk(IrisPos3Val.Text))
                        return;
                }
                CheckCount++;
            }
            if (Pos4ChkBox.Checked == true)
            {
                if (Form1.Instance.withZoom == true)
                {
                    if (Form1.SUCCESS != ZoomAddrChk(ZoomPos4Val.Text))
                        return;
                }
                if (Form1.Instance.withFocus == true)
                {
                    if (Form1.SUCCESS != FocusAddrChk(FocusPos4Val.Text))
                        return;
                }
                if (Form1.Instance.withIris == true)
                {
                    if (Form1.SUCCESS != IrisAddrChk(IrisPos4Val.Text))
                        return;
                }
                CheckCount++;
            }
            if (2 > CheckCount)
            {
                MessageBox.Show("Please check at least two checkboxes.");
                return;
            }
            Form1.Instance.BtnProhibit();
            evPresetMove.Set();
            PresetMove();
        }
        private int ZoomAddrChk(string ZoomAddrStr)
        {
            if (ZoomAddrStr == "")
            {
                MessageBox.Show("Zoom address position is empty.");
                return Form1.NG;
            }
            else
            {
                ushort ZoomAddrData = (ushort)Convert.ToInt32(ZoomAddrStr);
                if ((ZoomAddrData > LensCtrl.Instance.zoomMaxAddr) | (LensCtrl.Instance.zoomMinAddr > ZoomAddrData))
                {
                    MessageBox.Show("Zoom is out of address.");
                    return Form1.NG;
                }
                return Form1.SUCCESS;
            }
        }
        private int FocusAddrChk(string FocusAddrStr)
        {
            if (FocusAddrStr == "")
            {
                MessageBox.Show("Focus address position is empty.");
                return Form1.NG;
            }
            else
            {
                ushort FocusAddrData = (ushort)Convert.ToInt32(FocusAddrStr);
                if ((FocusAddrData > LensCtrl.Instance.focusMaxAddr) | (LensCtrl.Instance.focusMinAddr > FocusAddrData))
                {
                    MessageBox.Show("Focus is out of address.");
                    return Form1.NG;
                }
                return Form1.SUCCESS;
            }
        }
        private int IrisAddrChk(string IrisAddrStr)
        {
            if (IrisAddrStr == "")
            {
                MessageBox.Show("Iris address position is empty.");
                return Form1.NG;
            }
            else
            {
                ushort IrisAddrData = (ushort)Convert.ToInt32(IrisAddrStr);
                if ((IrisAddrData > LensCtrl.Instance.irisMaxAddr) | (LensCtrl.Instance.irisMinAddr > IrisAddrData))
                {
                    MessageBox.Show("Iris is out of address.");
                    return Form1.NG;
                }
                return Form1.SUCCESS;
            }
        }
 
        private void PresetStop_btn_Click(object sender, EventArgs e)
        {
            evPresetMove.Reset();
            PresetStatusTxt.Text = "";
        }

        private void Pos1Current_btn_Click(object sender, EventArgs e)
        {
            if (Form1.Instance.withZoom == true)
                ZoomPos1Val.Text = LensCtrl.Instance.zoomCurrentAddr.ToString();

            if (Form1.Instance.withFocus == true)
                FocusPos1Val.Text = LensCtrl.Instance.focusCurrentAddr.ToString();

            if (Form1.Instance.withIris == true)
                IrisPos1Val.Text = LensCtrl.Instance.irisCurrentAddr.ToString();

            if (Form1.Instance.withOptFil == true)
                OptPos1ComboBox.SelectedIndex = LensCtrl.Instance.optCurrentAddr;

            if (WaitTime1.Text == "")
                WaitTime1.Text = "1000";
        }
        private void Pos2Current_btn_Click(object sender, EventArgs e)
        {
            if (Form1.Instance.withZoom == true)
                    ZoomPos2Val.Text = LensCtrl.Instance.zoomCurrentAddr.ToString();

            if (Form1.Instance.withFocus == true)
                FocusPos2Val.Text = LensCtrl.Instance.focusCurrentAddr.ToString();

            if (Form1.Instance.withIris == true)
                IrisPos2Val.Text = LensCtrl.Instance.irisCurrentAddr.ToString();

            if (Form1.Instance.withOptFil == true)
                OptPos2ComboBox.SelectedIndex = LensCtrl.Instance.optCurrentAddr;

            if (WaitTime2.Text == "")
                WaitTime2.Text = "1000";
        }
        private void Pos3Current_btn_Click(object sender, EventArgs e)
        {
            if (Form1.Instance.withZoom == true)
                ZoomPos3Val.Text = LensCtrl.Instance.zoomCurrentAddr.ToString();

            if (Form1.Instance.withFocus == true)
                FocusPos3Val.Text = LensCtrl.Instance.focusCurrentAddr.ToString();

            if (Form1.Instance.withIris == true)
                IrisPos3Val.Text = LensCtrl.Instance.irisCurrentAddr.ToString();

            if (Form1.Instance.withOptFil == true)
                OptPos3ComboBox.SelectedIndex = LensCtrl.Instance.optCurrentAddr;

            if (WaitTime3.Text == "")
                WaitTime3.Text = "1000";
        }
        private void Pos4Current_btn_Click(object sender, EventArgs e)
        {
            if (Form1.Instance.withZoom == true)
                ZoomPos4Val.Text = LensCtrl.Instance.zoomCurrentAddr.ToString();

            if (Form1.Instance.withFocus == true)
                FocusPos4Val.Text = LensCtrl.Instance.focusCurrentAddr.ToString();

            if (Form1.Instance.withIris == true)
                IrisPos4Val.Text = LensCtrl.Instance.irisCurrentAddr.ToString();

            if (Form1.Instance.withOptFil == true)
                OptPos4ComboBox.SelectedIndex = LensCtrl.Instance.optCurrentAddr;

            if (WaitTime4.Text == "")
                WaitTime4.Text = "1000";
        }

        private void Form4_btn(bool flag)
        {
            PresetStart_btn.Enabled = flag;
            PresetStop_btn.Enabled = !flag;

            PresetFormClose_btn.Enabled = flag;
            Export_btn.Enabled = flag;
            Import_btn.Enabled = flag;
            Pos1Current_btn.Enabled = flag;
            Pos2Current_btn.Enabled = flag;
            Pos3Current_btn.Enabled = flag;
            Pos4Current_btn.Enabled = flag;

        }

        public void PresetMove()
        {
            ushort addrData;
            Form4_btn(false);
            int posCount = 0;
            do
            {
                switch (posCount)
                {
                    case 0:
                        if (Pos1ChkBox.Checked == true)
                        {
                            groupBox1.BackColor = Color.LightGray;
                            if (Form1.Instance.withZoom == true)
                            {
                                ZoomPos1Val.BackColor = Color.LightSkyBlue;
                                addrData = (ushort)Convert.ToInt32(ZoomPos1Val.Text);
                                if (Form1.NG == ZoomPresetMove(addrData))
                                    return;
                                ZoomPos1Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withFocus == true)
                            {
                                FocusPos1Val.BackColor = Color.PaleGreen;
                                addrData = (ushort)Convert.ToInt32(FocusPos1Val.Text);
                                if (Form1.NG == FocusPresetMove(addrData))
                                    return;
                                FocusPos1Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withIris == true)
                            {
                                IrisPos1Val.BackColor = Color.Pink;
                                addrData = (ushort)Convert.ToInt32(IrisPos1Val.Text);
                                if (Form1.NG == IrisPresetMove(addrData))
                                    return;
                                IrisPos1Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withOptFil == true)
                            {
                                OptPos1ComboBox.BackColor = Color.Yellow;
                                addrData = (ushort)Convert.ToInt32(OptPos1ComboBox.SelectedIndex);
                                if (Form1.NG == OptFilterPresetMove(addrData))
                                    return;
                                OptPos1ComboBox.BackColor = Color.Empty;
                            }
                            WaitTime1.BackColor = Color.Violet;
                            PresetStatusTxt.Text = "Waiting.";
                            Application.DoEvents();
                            Thread.Sleep(Convert.ToInt32(WaitTime1.Text));
                            WaitTime1.BackColor = Color.Empty;
                            groupBox1.BackColor = Color.Empty;
                        }
                        posCount++;
                        break;

                    case 1:
                        if (Pos2ChkBox.Checked == true)
                        {
                            groupBox2.BackColor = Color.LightGray;
                            if (Form1.Instance.withZoom == true)
                            {
                                ZoomPos2Val.BackColor = Color.LightSkyBlue;
                                addrData = (ushort)Convert.ToInt32(ZoomPos2Val.Text);
                                if (Form1.NG == ZoomPresetMove(addrData))
                                    return;
                                ZoomPos2Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withFocus == true)
                            {
                                FocusPos2Val.BackColor = Color.PaleGreen;
                                addrData = (ushort)Convert.ToInt32(FocusPos2Val.Text);
                                if (Form1.NG == FocusPresetMove(addrData))
                                    return;
                                FocusPos2Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withIris == true)
                            {
                                IrisPos2Val.BackColor = Color.Pink;
                                addrData = (ushort)Convert.ToInt32(IrisPos2Val.Text);
                                if (Form1.NG == IrisPresetMove(addrData))
                                    return;
                                IrisPos2Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withOptFil == true)
                            {
                                OptPos2ComboBox.BackColor = Color.Yellow;
                                addrData = (ushort)Convert.ToInt32(OptPos2ComboBox.SelectedIndex);
                                if (Form1.NG == OptFilterPresetMove(addrData))
                                    return;
                                OptPos2ComboBox.BackColor = Color.Empty;
                            }
                            WaitTime2.BackColor = Color.Violet;
                            PresetStatusTxt.Text = "Waiting.";
                            Application.DoEvents();
                            Thread.Sleep(Convert.ToInt32(WaitTime2.Text));
                            WaitTime2.BackColor = Color.Empty;
                            groupBox2.BackColor = Color.Empty;
                        }
                        posCount++;
                        break;

                    case 2:
                        if (Pos3ChkBox.Checked == true)
                        {
                            groupBox3.BackColor = Color.LightGray;
                            if (Form1.Instance.withZoom == true)
                            {
                                ZoomPos3Val.BackColor = Color.LightSkyBlue;
                                addrData = (ushort)Convert.ToInt32(ZoomPos3Val.Text);
                                if (Form1.NG == ZoomPresetMove(addrData))
                                    return;
                                ZoomPos3Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withFocus == true)
                            {
                                FocusPos3Val.BackColor = Color.PaleGreen;
                                addrData = (ushort)Convert.ToInt32(FocusPos3Val.Text);
                                if (Form1.NG == FocusPresetMove(addrData))
                                    return;
                                FocusPos3Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withIris == true)
                            {
                                IrisPos3Val.BackColor = Color.Pink;
                                addrData = (ushort)Convert.ToInt32(IrisPos3Val.Text);
                                if (Form1.NG == IrisPresetMove(addrData))
                                    return;
                                IrisPos3Val.BackColor = Color.Empty;
                            }
                            if (Form1.Instance.withOptFil == true)
                            {
                                OptPos3ComboBox.BackColor = Color.Yellow;
                                addrData = (ushort)Convert.ToInt32(OptPos3ComboBox.SelectedIndex);
                                if (Form1.NG == OptFilterPresetMove(addrData))
                                    return;
                                OptPos3ComboBox.BackColor = Color.Empty;
                            }
                            WaitTime3.BackColor = Color.Violet;
                            PresetStatusTxt.Text = "Waiting.";
                            Application.DoEvents();
                            Thread.Sleep(Convert.ToInt32(WaitTime3.Text));
                            WaitTime3.BackColor = Color.Empty;
                            groupBox3.BackColor = Color.Empty;
                        }
                        posCount++;
                        break;

                    case 3:
                        if (Pos4ChkBox.Checked == true)
                        {
                            groupBox4.BackColor = Color.LightGray;
                            if (Form1.Instance.withZoom == true)
                            {
                                ZoomPos4Val.BackColor = Color.LightSkyBlue;
                                addrData = (ushort)Convert.ToInt32(ZoomPos4Val.Text);
                                if (Form1.NG == ZoomPresetMove(addrData))
                                    return;
                                ZoomPos4Val.BackColor = Color.Empty;
                            }

                            if (Form1.Instance.withFocus == true)
                            {
                                FocusPos4Val.BackColor = Color.PaleGreen;
                                addrData = (ushort)Convert.ToInt32(FocusPos4Val.Text);
                                if (Form1.NG == FocusPresetMove(addrData))
                                    return;
                                FocusPos4Val.BackColor = Color.Empty;
                            }

                            if (Form1.Instance.withIris == true)
                            {
                                IrisPos4Val.BackColor = Color.Pink;
                                addrData = (ushort)Convert.ToInt32(IrisPos4Val.Text);
                                if (Form1.NG == IrisPresetMove(addrData))
                                    return;
                                IrisPos4Val.BackColor = Color.Empty;
                            }

                            if (Form1.Instance.withOptFil == true)
                            {
                                OptPos4ComboBox.BackColor = Color.Yellow;
                                addrData = (ushort)Convert.ToInt32(OptPos4ComboBox.SelectedIndex);
                                if (Form1.NG == OptFilterPresetMove(addrData))
                                    return;
                                OptPos4ComboBox.BackColor = Color.Empty;
                            }
                            WaitTime4.BackColor = Color.Violet;
                            PresetStatusTxt.Text = "Waiting.";
                            Application.DoEvents();
                            Thread.Sleep(Convert.ToInt32(WaitTime4.Text));
                            WaitTime4.BackColor = Color.Empty;
                            groupBox4.BackColor = Color.Empty;
                        }
                        posCount = 0;
                        break;

                }
                Application.DoEvents();
            } while (evPresetMove.IsSet);

            Form1.Instance.ZoomValueUpdate();
            Form1.Instance.FocusValueUpdate();
            Form1.Instance.IrisValueUpdate();
            Form1.Instance.BtnPermition(Form1.SUCCESS);
            Form4_btn(true);
        }
        private int ZoomPresetMove(ushort addrData)
        {
            if (LensCtrl.Instance.zoomCurrentAddr != addrData)
            {
                PresetStatusTxt.Text = "Zoom is in Operation.";
                int retval = LensCtrl.Instance.ZoomMove(addrData);
                if ((retval != Form1.SUCCESS) & (retval < ConfigVal.LOWHI_ERROR))
                {
                    MessageBox.Show("Zoom is error." + LensCtrl.Instance.ErrorTxt(retval));
                    PresetStatusTxt.Text = "";
                    return Form1.NG;
                }
            }
            return Form1.SUCCESS;
        }
        private int FocusPresetMove(ushort addrData)
        {
            if (LensCtrl.Instance.focusCurrentAddr != addrData)
            {
                PresetStatusTxt.Text = "Focus is in Operation.";
                int retval = LensCtrl.Instance.FocusMove(addrData);
                if ((retval != Form1.SUCCESS) & (retval < ConfigVal.LOWHI_ERROR))
                {
                    MessageBox.Show("Focus is error." + LensCtrl.Instance.ErrorTxt(retval));
                    PresetStatusTxt.Text = "";
                    return Form1.NG;
                }
            }
            return Form1.SUCCESS;
        }
        private int IrisPresetMove(ushort addrData)
        {
            if (LensCtrl.Instance.irisCurrentAddr != addrData)
            {
                PresetStatusTxt.Text = "Iris is in Operation.";
                int retval = LensCtrl.Instance.IrisMove(addrData);
                if ((retval != Form1.SUCCESS) & (retval < ConfigVal.LOWHI_ERROR))
                {
                    MessageBox.Show("Iris is error." + LensCtrl.Instance.ErrorTxt(retval));
                    PresetStatusTxt.Text = "";
                    return Form1.NG;
                }
            }
            return Form1.SUCCESS;
        }
        private int OptFilterPresetMove(ushort addrData)
        {
            if (LensCtrl.Instance.optCurrentAddr != addrData)
            {
                PresetStatusTxt.Text = "Optical filter is in Operation.";
                int retval = LensCtrl.Instance.OptFilterMove(addrData);
                if ((retval != Form1.SUCCESS) & (retval < ConfigVal.LOWHI_ERROR))
                {
                    MessageBox.Show("Optical filter is error." + LensCtrl.Instance.ErrorTxt(retval));
                    PresetStatusTxt.Text = "";
                    return Form1.NG;
                }
            }
            return Form1.SUCCESS;
        }
        private void Export_btn_Click(object sender, EventArgs e)
        {
            string presetPosition;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = "Preset.txt";
            sfd.InitialDirectory = "";
            sfd.Filter = "TEXT File(*.txt)|*.txt|All files(*.*)|*.*";
            sfd.FilterIndex = 1;
            sfd.Title = "Save the preset file.";
            sfd.RestoreDirectory = true;
            sfd.CheckPathExists = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                presetPosition = sfd.FileName;

                using (StreamWriter sw = new StreamWriter(presetPosition))
                {
                    sw.WriteLine(Pos1ChkBox.Checked);
                    sw.WriteLine(ZoomPos1Val.Text);
                    sw.WriteLine(FocusPos1Val.Text);
                    sw.WriteLine(IrisPos1Val.Text);
                    sw.WriteLine(OptPos1ComboBox.SelectedIndex);
                    sw.WriteLine(WaitTime1.Text);

                    sw.WriteLine(Pos2ChkBox.Checked);
                    sw.WriteLine(ZoomPos2Val.Text);
                    sw.WriteLine(FocusPos2Val.Text);
                    sw.WriteLine(IrisPos2Val.Text);
                    sw.WriteLine(OptPos2ComboBox.SelectedIndex);
                    sw.WriteLine(WaitTime2.Text);

                    sw.WriteLine(Pos3ChkBox.Checked);
                    sw.WriteLine(ZoomPos3Val.Text);
                    sw.WriteLine(FocusPos3Val.Text);
                    sw.WriteLine(IrisPos3Val.Text);
                    sw.WriteLine(OptPos3ComboBox.SelectedIndex);
                    sw.WriteLine(WaitTime3.Text);

                    sw.WriteLine(Pos4ChkBox.Checked);
                    sw.WriteLine(ZoomPos4Val.Text);
                    sw.WriteLine(FocusPos4Val.Text);
                    sw.WriteLine(IrisPos4Val.Text);
                    sw.WriteLine(OptPos4ComboBox.SelectedIndex);
                    sw.WriteLine(WaitTime4.Text);
                }
            }
        }
        private void Import_btn_Click(object sender, EventArgs e)
        {
            string presetPosition;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = "Preset.txt";
            ofd.InitialDirectory = "";
            ofd.Filter = "TEXT File(*.txt)|*.txt|All files(*.*)|*.*";
            ofd.FilterIndex = 1;
            ofd.Title = "Please select the preset file.";
            ofd.RestoreDirectory = true;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                presetPosition = ofd.FileName;
                using (StreamReader sr = new StreamReader(presetPosition))
                {
                    if(sr.ReadLine() == "True")
                    {
                        Pos1ChkBox.Checked = true;
                    }
                    else
                    {
                        Pos1ChkBox.Checked = false;
                    }
                    ZoomPos1Val.Text = sr.ReadLine();
                    FocusPos1Val.Text = sr.ReadLine();
                    IrisPos1Val.Text = sr.ReadLine();
                    if (Form1.Instance.withOptFil == true)
                    {
                        OptPos1ComboBox.SelectedIndex = int.Parse(sr.ReadLine());
                    }
                    else
                    {
                        int.Parse(sr.ReadLine());
                    }
                    WaitTime1.Text = sr.ReadLine();

                    if (sr.ReadLine() == "True")
                    {
                        Pos2ChkBox.Checked = true;
                    }
                    else
                    {
                        Pos2ChkBox.Checked = false;
                    }
                    ZoomPos2Val.Text = sr.ReadLine();
                    FocusPos2Val.Text = sr.ReadLine();
                    IrisPos2Val.Text = sr.ReadLine();
                    if (Form1.Instance.withOptFil == true)
                    {
                        OptPos2ComboBox.SelectedIndex = int.Parse(sr.ReadLine());
                    }
                    else
                    {
                        int.Parse(sr.ReadLine());
                    }
                    WaitTime2.Text = sr.ReadLine();

                    if (sr.ReadLine() == "True")
                    {
                        Pos3ChkBox.Checked = true;
                    }
                    else
                    {
                        Pos3ChkBox.Checked = false;
                    }
                    ZoomPos3Val.Text = sr.ReadLine();
                    FocusPos3Val.Text = sr.ReadLine();
                    IrisPos3Val.Text = sr.ReadLine();
                    if (Form1.Instance.withOptFil == true)
                    {
                        OptPos3ComboBox.SelectedIndex = int.Parse(sr.ReadLine());
                    }
                    else
                    {
                        int.Parse(sr.ReadLine());
                    }
                    WaitTime3.Text = sr.ReadLine();

                    if (sr.ReadLine() == "True")
                    {
                        Pos4ChkBox.Checked = true;
                    }
                    else
                    {
                        Pos4ChkBox.Checked = false;
                    }
                    ZoomPos4Val.Text = sr.ReadLine();
                    FocusPos4Val.Text = sr.ReadLine();
                    IrisPos4Val.Text = sr.ReadLine();
                    if (Form1.Instance.withOptFil == true)
                    {
                        OptPos4ComboBox.SelectedIndex = int.Parse(sr.ReadLine());
                    }
                    else
                    {
                        int.Parse(sr.ReadLine());
                    }
                    WaitTime4.Text = sr.ReadLine();

                }
            }
        }

        private void PresetFormClose_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Form4_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result  = MessageBox.Show("Setting data will be cleared when closed.\r\nAre you sure that you would like to close the Preset window?", "Message", MessageBoxButtons.YesNo);
            if (result  == System.Windows.Forms.DialogResult.Yes)
                Form1.Instance.PresetOpen_btn.Enabled = true;
            else if (result  == System.Windows.Forms.DialogResult.No)
                e.Cancel = true;
        }
    }
}
