using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace GcDevicePc
{
    public partial class system_set_Form : Form
    {
        public algorithm.algorithm algorithm = new algorithm.algorithm();
        //////algorithm.algorithm algorithm =new algorithm.algorithm();
        public system_set_Form()
        {
            InitializeComponent();
        }
        //////滤波算法
        private void ditong_wave_filter_rB_Click(object sender, EventArgs e)
        {
            algorithm.ditong_wave_filter_true = ditong_wave_filter_rB.Checked;
        }
        private void fly_wave_filter_rB_Click(object sender, EventArgs e)
        {
            algorithm.fly_wave_filter_true = fly_wave_filter_rB.Checked;
        }
        private void ditong_wave_filter_bt_Click(object sender, EventArgs e)
        {
            try
            {
                algorithm.di_tong_wight_value = Convert.ToInt16(ditong_wave_filter_tB.Text);               
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的整数！");
            }
        }
        private void fly_wave_filter_bt_Click(object sender, EventArgs e)
        {
            try
            {
                algorithm.low_wave_filter = Convert.ToInt16(fly_low_wave_filter_tB.Text);
                algorithm.hight_wave_filter = Convert.ToInt16(fly_hight_wave_filter_tB.Text);              
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的整数！");
            }
        }
        //////高低峰限制
        private void min_m_peak_cb_Click(object sender, EventArgs e)
        {
            algorithm.min_m_peak_cb = min_m_peak_cb.Checked;
        }
        private void min_m_peak_leap_cb_Click(object sender, EventArgs e)
        {
            algorithm.min_m_peak_leap_cb = min_m_peak_leap_cb.Checked;
        }
        private void min_area_peak_cb_Click(object sender, EventArgs e)
        {
            algorithm.min_area_peak_cb = min_area_peak_cb.Checked;
        }
        private void min_m_peak_bt_Click(object sender, EventArgs e)
        {
            try
            {
                float value = Convert.ToSingle(min_m_peak_tb.Text);
                algorithm.min_m_peak_value = value;               
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        private void min_m_peak_leap_bt_Click(object sender, EventArgs e)
        {
            try
            {
                float value = Convert.ToSingle(min_m_peak_leap_tb.Text);
                algorithm.min_m_peak_leap_value = value;               
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        private void min_area_peak_bt_Click(object sender, EventArgs e)
        {
            try
            {
                float value = Convert.ToSingle(min_area_peak_tb.Text);
                algorithm.min_m_peak_area_value = value;               
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        //负峰翻转
        private void bottom_overturn_peak_cB_Click(object sender, EventArgs e)
        {
            algorithm.bottom_overturn_peak_T = bottom_overturn_peak_cB.Checked;
        }
        private void bottom_overturn_peak_bt_Click(object sender, EventArgs e)
        {
            try
            {
                int value = Convert.ToInt16(bottom_overturn_peak_start_place_tb.Text);
                algorithm.bottom_overturn_peak_start_place = value;
                int value1 = Convert.ToInt16(bottom_overturn_peak_end_place_tb.Text);
                algorithm.bottom_overturn_peak_end_place = value1;
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        } 
        //////自动判谷
        private void auto_max_min_valley_cb_Click(object sender, EventArgs e)
        {
            algorithm.auto_max_min_valley_cb = auto_max_min_valley_cb.Checked;

            if (auto_max_min_valley_cb.Checked)
            {
                panel3.Enabled = false;
            }
            else 
            {
                panel3.Enabled = true;
            }
        }        
        private void auto_max_min_valley_bt_Click(object sender, EventArgs e)
        {
            try
            {
                int value1 = Convert.ToInt32(auto_max_min_valley_start_place_tb.Text);
                int value2 = Convert.ToInt32(auto_max_min_valley_end_place_tb.Text);
                float value3 = Convert.ToSingle(auto_max_min_valley_small_minus_tb.Text);
                float value4 = Convert.ToSingle(auto_max_min_valley_small_add_tb.Text);

                algorithm.auto_max_min_valley_start_place = value1;
                algorithm.auto_max_min_valley_end_place = value2;
                algorithm.auto_max_min_valley_small_minus = value3;
                algorithm.auto_max_min_valley_small_add = value4;
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        //////判谷条件
        private void max_valley_cb_Click(object sender, EventArgs e)
        {
            algorithm.max_valley_cb = max_valley_cb.Checked;
        }
        private void min_valley_cb_Click(object sender, EventArgs e)
        {
            algorithm.min_valley_cb = min_valley_cb.Checked;
        }
        private void max_valley_bt_Click(object sender, EventArgs e)
        {
            try
            {
                float value = Convert.ToSingle(max_valley_tb.Text);
                algorithm.max_valley_value = value;              
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        private void min_valley_bt_Click(object sender, EventArgs e)
        {
            try
            {
                float value = Convert.ToSingle(min_valley_tb.Text);
                algorithm.min_valley_value = value;               
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        //////谷分离
        private void overlap_m_peak_divide_cb_Click(object sender, EventArgs e)
        {
            algorithm.overlap_m_peak_divide_cb = overlap_m_peak_divide_cb.Checked;
        }
        private void overlap_divide_kind1_cb_Click(object sender, EventArgs e)
        {
            algorithm.overlap_divide_kind1_cb = overlap_divide_kind1_cb.Checked;
        }
        private void overlap_divide_kind2_cb_Click(object sender, EventArgs e)
        {
            algorithm.overlap_divide_kind2_cb = overlap_divide_kind2_cb.Checked;
        }
        private void overlap_divide_kind3_cb_Click(object sender, EventArgs e)
        {
            algorithm.overlap_divide_kind3_cb = overlap_divide_kind3_cb.Checked;
        }
        private void overlap_divide_kind1_bt_Click(object sender, EventArgs e)
        {
            try
            {
                float value = Convert.ToSingle(overlap_divide_kind1_tb.Text);
                algorithm.overlap_divide_valley_value = value;
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }

        void load_setting() 
        {
            base_value_work_kind_cB.Text = algorithm.base_value_work_kind;
            //////滤波算法
            ditong_wave_filter_rB.Checked = algorithm.ditong_wave_filter_true;
            fly_wave_filter_rB.Checked = algorithm.fly_wave_filter_true;
            ditong_wave_filter_tB.Text = algorithm.di_tong_wight_value.ToString();
            fly_low_wave_filter_tB.Text = algorithm.low_wave_filter.ToString();
            fly_hight_wave_filter_tB.Text = algorithm.hight_wave_filter.ToString();
            //////判谷条件
            min_m_peak_cb.Checked = algorithm.min_m_peak_cb;
            min_m_peak_leap_cb.Checked = algorithm.min_m_peak_leap_cb;
            min_area_peak_cb.Checked = algorithm.min_area_peak_cb;
            min_m_peak_tb.Text = algorithm.min_m_peak_value.ToString();
            min_m_peak_leap_tb.Text = algorithm.min_m_peak_leap_value.ToString();
            min_area_peak_tb.Text = algorithm.min_m_peak_area_value.ToString();
            //////负峰翻转设置
            bottom_overturn_peak_cB.Checked = algorithm.bottom_overturn_peak_T;
            bottom_overturn_peak_start_place_tb.Text = algorithm.bottom_overturn_peak_start_place.ToString();
            bottom_overturn_peak_end_place_tb.Text = algorithm.bottom_overturn_peak_end_place.ToString();
            //////自动判谷
            auto_max_min_valley_cb.Checked = algorithm.auto_max_min_valley_cb;
            if (auto_max_min_valley_cb.Checked)
            {
                panel3.Enabled = false;
            }
            else
            {
                panel3.Enabled = true;
            }
            auto_max_min_valley_start_place_tb.Text = algorithm.auto_max_min_valley_start_place.ToString();
            auto_max_min_valley_end_place_tb.Text = algorithm.auto_max_min_valley_end_place.ToString();
            auto_max_min_valley_small_minus_tb.Text = algorithm.auto_max_min_valley_small_minus.ToString();
            auto_max_min_valley_small_add_tb.Text = algorithm.auto_max_min_valley_small_add.ToString();
            //////判谷条件
            max_valley_cb.Checked = algorithm.max_valley_cb;
            min_valley_cb.Checked = algorithm.min_valley_cb;
            max_valley_tb.Text = algorithm.max_valley_value.ToString();
            min_valley_tb.Text = algorithm.min_valley_value.ToString();
            //////谷分离
            overlap_m_peak_divide_cb.Checked = algorithm.overlap_m_peak_divide_cb;
            overlap_divide_kind1_cb.Checked = algorithm.overlap_divide_kind1_cb;
            overlap_divide_kind2_cb.Checked = algorithm.overlap_divide_kind2_cb;
            overlap_divide_kind3_cb.Checked = algorithm.overlap_divide_kind3_cb;
            overlap_divide_kind1_tb.Text = algorithm.overlap_divide_valley_value.ToString();
        }
        private void system_set_Form_Load(object sender, EventArgs e)
        {          
            ////// Zoom into the X axis
            //////chart1.ChartAreas[0].AxisX.ScaleView.Zoom(0, 2048);
            ////// Enable range selection and zooming end user interface
            chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = double.NaN;
            chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 20;
            ////// Zoom into the Y axis
            //////chart1.ChartAreas[0].AxisY.ScaleView.Zoom(0, 60000);
            ////// Enable range selection and zooming end user interface
            chart1.ChartAreas[0].CursorY.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
            chart1.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = double.NaN;
            chart1.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = 20;
            //////将滚动内嵌到坐标轴中
            chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = false;
            chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = false;

            ////配置文件读取
           string[] setting_list;
           setting_list = File.ReadAllLines("sys_setting.txt");
           if (SYS_setting.Columns.Count <= 0)
           {
               SYS_setting.Columns.Add("num");
               SYS_setting.Columns.Add("name");
               SYS_setting.Columns.Add("Datatime");
           }
           if (setting_list.Length > 1)
           {
               for (int i = 1; i < setting_list.Length; i++)
               {
                   string[] VVV = setting_list[i].Split(';');
                   SYS_setting.Rows.Add(VVV);
               }
               setting_dGridView.DataSource = SYS_setting;
               
               if (algorithm_W.load_infor != "无")
               {
                   current_setting_lb.Text = "当前配置:" + algorithm_W.load_infor;
                   setting_name_tBox.Text = algorithm_W.load_infor;
                   algorithm.load_formatter_infor(algorithm_W.load_infor);
                   load_setting();
               }
               else 
               {
                   setting_name_tBox.Text = setting_list[0];
                   current_setting_lb.Text = "当前配置:" + setting_list[0];                  
                   algorithm.load_formatter_infor(setting_list[0]);
                   load_setting();
               }
           }
           ////matter_place_gv.AutoGenerateColumns = false;
           ////result_dataGridView.DataSource = data_result;

           port_A_thread = new Thread(calculate_thread_method);
           port_A_thread.Start();
        }
        DataTable data_result = null;
        DataTable data_line = null;
        private void timer1_Tick(object sender, EventArgs e)
        {
            #region  刷新界面
            //lock (this)
            //{
                //try
                //{
                //    if (!algorithm.work_OK) { return; }
                //    algorithm.data_calculator();
                //    data_line = algorithm.chart_show;
                //    data_result = algorithm.calculate_tab;

                //    //////chart_show.Columns.Add("count");
                //    //////chart_show.Columns.Add("minute");
                //    //////chart_show.Columns.Add("value");
                //    //////chart_show.Columns.Add("value1");
                //    //////chart_show.Columns.Add("base_value");
                //    //////chart_show.Columns.Add("is_peak");
                //    //////chart_show.Columns.Add("is_peak1");

                //    chart1.Series[0].XValueMember = "minute";
                //    chart1.Series[0].YValueMembers = "value1";
                //    chart1.Series[1].XValueMember = "minute";
                //    chart1.Series[1].YValueMembers = "base_value";
                //    chart1.DataSource = data_line;
                //    chart1.DataBind();

                //    result_dataGridView.DataSource = data_result;
                //    matter_place_gv.DataSource = data_result;
                //    //////chart1.Series[0].Points.Clear();
                //    //////chart1.Series[1].Points.Clear();                            
                //    for (int k = 0; k < data_line.Rows.Count; k++)
                //    {
                //        ////chart1.Series[0].Points.AddXY(diandao_out_time0[RowIndex].ToString("0.0000"), diandao_value_1[RowIndex].ToString("0.0000"));
                //        ////chart1.Series[1].Points.AddXY(diandao_out_time0[RowIndex].ToString("0.0000"), diandao_out_base[RowIndex].ToString("0.0000"));
                //        //// = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
                //        //////chart_data.Rows[RowIndex][0] = diandao_out_time0[RowIndex].ToString("0.0000");
                //        //////chart_data.Rows[RowIndex][1] = diandao_value_1[RowIndex].ToString("0.00");
                //        //////chart_data.Rows[RowIndex][2] = diandao_out_base[RowIndex].ToString("0.00");

                //        #region 标峰和谷
                //        if (data_line.Rows[k][6].ToString() == "9" && top_show_cB.Checked)
                //        {
                //            chart1.Series[0].Points[k].MarkerBorderWidth = 4;
                //            chart1.Series[0].Points[k].MarkerColor = Color.Red;
                //            chart1.Series[0].Points[k].MarkerSize = 4;
                //            ////chart1.Series[0].Points[place - 2].
                //            ////chart1.Series[0].Points[place - 2].BorderDashStyle = ChartDashStyle.Dash;
                //            chart1.Series[0].Points[k].IsValueShownAsLabel = true;
                //        }
                //        else if (data_line.Rows[k][6].ToString() == "2" && bottom_show_cB.Checked)
                //        {
                //            chart1.Series[0].Points[k].MarkerBorderWidth = 4;
                //            chart1.Series[0].Points[k].MarkerColor = Color.Blue;
                //            chart1.Series[0].Points[k].MarkerSize = 4;
                //            ////chart1.Series[0].Points[place - 2].
                //            ////chart1.Series[0].Points[place - 2].BorderDashStyle = ChartDashStyle.Dash;
                //            chart1.Series[0].Points[k].IsValueShownAsLabel = true;
                //        }
                //        else
                //        {
                //            ////chart1.Series[0].Points[RowIndex].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                //            chart1.Series[0].Points[k].MarkerBorderWidth = 1;
                //            chart1.Series[0].Points[k].MarkerColor = Color.Gray;
                //            chart1.Series[0].Points[k].MarkerSize = 1;
                //            chart1.Series[0].Points[k].IsValueShownAsLabel = false;
                //        }
                //        #endregion
                //    }
                //    if (chroma_show_cB.Checked)
                //    {
                //        int index0 = -1;
                //        int index1 = -1;
                //        int index2 = -1;
                //        ////int index3 = -1;

                //        for (int k = 0; k < data_result.Rows.Count; k++)
                //        {
                //            index0 = Convert.ToInt16(data_result.Rows[k][12]);
                //            index1 = Convert.ToInt16(data_result.Rows[k][10]);
                //            index2 = Convert.ToInt16(data_result.Rows[k][11]);
                //            if (index0 >= 0 && index0 < data_line.Rows.Count && index1 < data_line.Rows.Count && index2 < data_line.Rows.Count)
                //            {
                //                ////////                 0     1      2        3       4      5      6      7        8           9            10        11          12      13          14       15       16       17                              
                //                ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；
                //                chart1.Series[0].Points[index0].Label = "名称:" + data_result.Rows[k][1].ToString()
                //                    + Environment.NewLine + "出峰时间:" + data_line.Rows[index0][1].ToString() + "Min"
                //                    + Environment.NewLine + "保持时间:" + (Convert.ToDouble(data_line.Rows[index1][1]) + " - " + Convert.ToDouble(data_line.Rows[index2][1])).ToString() + "Min"
                //                    + Environment.NewLine + "浓度:" + data_result.Rows[k][7].ToString()
                //                    + Environment.NewLine + "归一值:" + data_result.Rows[k][18].ToString();
                //            }
                //        }
                //    }                   
                //}
                //catch (Exception)
                //{

                //}
            //}
            #endregion
        }

        bool run_pause = false;
        bool new_data = true;
        int refresh = 0;
        Thread port_A_thread;

        void calculate_thread_method()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(500);
                    if (!run_pause)
                    {
                        algorithm.data_calculator();
                        data_line = algorithm.chart_show;
                        data_result = algorithm.calculate_tab;

                        try
                        {
                            if(this.InvokeRequired)
                            {
                                webTextCallback test = new webTextCallback(data_show_delegate_Text);
                                this.Invoke(test, new object[] { "port_A", null });                     
                            }                         
                        }
                        catch
                        {

                        }
                    }
                }
                catch (Exception)
                {

                }
            }
        }

        delegate void webTextCallback(string text, double[] value);
        private void data_show_delegate_Text(string text, double[] value)
        {
            lock (this)
            {
                try
                {
                if (text == "port_A" && algorithm.work_OK)
                {

                    //////chart_show.Columns.Add("count");
                    //////chart_show.Columns.Add("minute");
                    //////chart_show.Columns.Add("value");
                    //////chart_show.Columns.Add("value1");
                    //////chart_show.Columns.Add("base_value");
                    //////chart_show.Columns.Add("is_peak");
                    //////chart_show.Columns.Add("is_peak1");
                    ////chart1.Series[0].Points.DataBind(data_line.AsEnumerable(), "minute", "value1,base_value", "");
                    ////chart1.Series[0].XValueType = ChartValueType.Double; //设置X轴类型为时间
                    ////chart1.Series[1].Points.DataBind(data_line.AsEnumerable(), "minute", "value1", "");
                    ////chart1.Series[1].XValueType = ChartValueType.Double; //设置X轴类型为时间
                    ////chart1.Series[2].Points.DataBind(data_line.AsEnumerable(), "minute", "base_value", "");
                    ////chart1.Series[2].XValueType = ChartValueType.Double; //设置X轴类型为时间
                    chart1.Series[1].XValueMember = "minute";
                    chart1.Series[1].YValueMembers = "value1";
                    chart1.Series[2].XValueMember = "minute";
                    chart1.Series[2].YValueMembers = "base_value";
                    chart1.Series[0].XValueMember = "minute";
                    chart1.Series[0].YValueMembers = "value1,base_value";
                   
                    chart1.DataSource = data_line;
                    chart1.DataBind();

                    result_dataGridView.DataSource = data_result;
                    ////chart1.Series[0].Points.Clear();
                    ////chart1.Series[1].Points.Clear();                            
                    for (int k = 0; k < data_line.Rows.Count; k++)
                    {
                        ////chart1.Series[0].Points.AddXY(diandao_out_time0[RowIndex].ToString("0.0000"), diandao_value_1[RowIndex].ToString("0.0000"));
                        ////chart1.Series[1].Points.AddXY(diandao_out_time0[RowIndex].ToString("0.0000"), diandao_out_base[RowIndex].ToString("0.0000"));
                        //// = new System.Windows.Forms.DataVisualization.Charting.DataPoint();
                        //////chart_data.Rows[RowIndex][0] = diandao_out_time0[RowIndex].ToString("0.0000");
                        //////chart_data.Rows[RowIndex][1] = diandao_value_1[RowIndex].ToString("0.00");
                        //////chart_data.Rows[RowIndex][2] = diandao_out_base[RowIndex].ToString("0.00");

                        #region 标峰和谷
                        if (data_line.Rows[k][6].ToString() == "9" && top_show_cB.Checked)
                        {
                            chart1.Series[0].Points[k].MarkerBorderWidth = 4;
                            chart1.Series[0].Points[k].MarkerColor = Color.Red;
                            chart1.Series[0].Points[k].MarkerSize = 4;
                            ////chart1.Series[0].Points[place - 2].
                            ////chart1.Series[0].Points[place - 2].BorderDashStyle = ChartDashStyle.Dash;
                            chart1.Series[0].Points[k].IsValueShownAsLabel = true;
                        }
                        else if (data_line.Rows[k][6].ToString() == "2" && bottom_show_cB.Checked)
                        {
                            chart1.Series[0].Points[k].MarkerBorderWidth = 4;
                            chart1.Series[0].Points[k].MarkerColor = Color.Blue;
                            chart1.Series[0].Points[k].MarkerSize = 4;
                            ////chart1.Series[0].Points[place - 2].
                            ////chart1.Series[0].Points[place - 2].BorderDashStyle = ChartDashStyle.Dash;
                            chart1.Series[0].Points[k].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            //chart1.Series[0].Points[RowIndex].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                            chart1.Series[0].Points[k].MarkerBorderWidth = 1;
                            chart1.Series[0].Points[k].MarkerColor = Color.Gray;
                            chart1.Series[0].Points[k].MarkerSize = 1;
                            chart1.Series[0].Points[k].IsValueShownAsLabel = false;
                        }
                        #endregion
                    }
                    int index0 = -1;
                    int index1 = -1;
                    int index2 = -1;
                    Color area_color;
                    ////if (algorithm.calculate_tab.Columns.Count == 22)
                    ////{
                    ////    algorithm.calculate_tab.Columns.Remove("颜色");
                    ////    algorithm.calculate_tab.Columns.Add("颜色");
                    ////}

                    for (int k = 0; k < data_result.Rows.Count; k++)
                    {
                        index0 = Convert.ToInt16(data_result.Rows[k][12]);
                        index1 = Convert.ToInt16(data_result.Rows[k][10]);
                        index2 = Convert.ToInt16(data_result.Rows[k][11]);
                        if (!data_result.Rows[k][21].Equals(DBNull.Value))
                        {
                            area_color = Color.FromArgb(Convert.ToInt32(data_result.Rows[k][21]));
                            result_dataGridView.Rows[k].Cells[9].Style.BackColor = area_color;
                            ////area_color = 
                            for (int i = index1; i < index2 && i < chart1.Series[0].Points.Count; i++)
                            {
                                chart1.Series[0].Points[i].Color = area_color;
                            }
                        }
                    }
                    if (chroma_show_cB.Checked)
                    {
                        index0 = -1;
                        index1 = -1;
                        index2 = -1;
                        ////int index3 = -1;

                        for (int k = 0; k < data_result.Rows.Count; k++)
                        {
                            index0 = Convert.ToInt16(data_result.Rows[k][12]);
                            index1 = Convert.ToInt16(data_result.Rows[k][10]);
                            index2 = Convert.ToInt16(data_result.Rows[k][11]);
                            if (index0 >= 0 && index0 < data_line.Rows.Count && index1 < data_line.Rows.Count && index2 < data_line.Rows.Count)
                            {
                                ////////                 0     1      2        3       4      5      6      7        8           9            10        11          12      13          14       15       16       17                              
                                ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；
                                chart1.Series[0].Points[index0].Label = "名称:" + data_result.Rows[k][1].ToString()
                                    + Environment.NewLine + "出峰时间:" + data_line.Rows[index0][1].ToString() + "Min"
                                    + Environment.NewLine + "保持时间:" + (Convert.ToDouble(data_line.Rows[index1][1]) + " - " + Convert.ToDouble(data_line.Rows[index2][1])).ToString() + "Min"
                                    + Environment.NewLine + "浓度:" + data_result.Rows[k][7].ToString()
                                    + Environment.NewLine + "归一值:" + data_result.Rows[k][18].ToString();
                            }
                            ////for (int i = index1; i < index2; i++)
                            ////{
                            ////    chart1.Series[0].Points[i].Color = Color.Red;
                            ////}

                        }
                    }

                }
                }
                catch (Exception)
                {

                }
            }
        }

        ////组分配置文件管理
        DataTable SYS_setting = new DataTable();
        private void add_setting_bt_Click(object sender, EventArgs e)
        {
            if (SYS_setting.Columns.Count <= 0)
            {
                SYS_setting.Columns.Add("num");
                SYS_setting.Columns.Add("name");
                SYS_setting.Columns.Add("Datatime");
            }

            bool find = false;
            for (int i = 0; i < SYS_setting.Rows.Count; i++)
            {
                if (SYS_setting.Rows[i][1].ToString() == setting_name_tBox.Text.ToString())
                {
                    find = true;
                    break;
                }
            }
            if (find) 
            {
                algorithm.write_formatter_infor(setting_name_tBox.Text.ToString());
            }
            else
            {
                DateTime TTT = DateTime.Now;
                string name = setting_name_tBox.Text.ToString() + "," + TTT.ToString() + Environment.NewLine;
                SYS_setting.Rows.Add(SYS_setting.Rows.Count, setting_name_tBox.Text.ToString(), TTT.ToString());
                algorithm.write_formatter_infor(setting_name_tBox.Text.ToString());
                string AAA = "";
                AAA += algorithm.load_infor + Environment.NewLine;
                for (int i = 0; i < SYS_setting.Rows.Count; i++)
                {
                    AAA += SYS_setting.Rows[i][0].ToString() + ";" + SYS_setting.Rows[i][1].ToString() + ";" + SYS_setting.Rows[i][2].ToString() + Environment.NewLine;
                }
                File.WriteAllText("sys_setting.txt", AAA);
            }
        }
        private void tabControl1_Selected(object sender, TabControlEventArgs e) ////选择当前设置类别
        {
            if (e.TabPage.Text == "组分设置")
            {
                refresh_component();
            }
            else if (e.TabPage.Name == "判峰处理")
            {


            }
            else if (e.TabPage.Name == "存档")
            {

            }
            else if (e.TabPage.Name == "")
            {

            }
        }

        private void setting_up_move_bt_Click(object sender, EventArgs e)
        {

        }
        private void setting_down_move_bt_Click(object sender, EventArgs e)
        {

        }
        private void setting_delete_bt_Click(object sender, EventArgs e)
        {
            if (SYS_setting.Columns.Count <= 0)
            {
                SYS_setting.Columns.Add("num");
                SYS_setting.Columns.Add("name");
                SYS_setting.Columns.Add("Datatime");
            }

            bool find = false; int index = -1;
            for (int i = 0; i < SYS_setting.Rows.Count; i++)
            {
                if (SYS_setting.Rows[i][1].ToString() == setting_name_tBox.Text.ToString())
                {
                    find = true;
                    index = i;
                    break;
                }
            }
            if (find)
            {
                File.Delete(setting_name_tBox.Text.ToString());
                SYS_setting.Rows.RemoveAt(index);
            }
            else
            {               
                string AAA = "";
                AAA += algorithm.load_infor + Environment.NewLine;
                for (int i = 0; i < SYS_setting.Rows.Count; i++)
                {
                    AAA += SYS_setting.Rows[i][0].ToString() + ";" + SYS_setting.Rows[i][1].ToString() + ";" + SYS_setting.Rows[i][2].ToString() + Environment.NewLine;
                }
                File.WriteAllText("sys_setting.txt", AAA);
            }
        }
        private void setting_save_bt_Click(object sender, EventArgs e)
        {

        }

        private void setting_dGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                setting_name_tBox.Text = setting_dGridView.SelectedRows[0].Cells[1].Value.ToString();
            }
            catch (Exception) 
            {
            
            }
        }
        private void setting_dGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string AAA = setting_dGridView.SelectedRows[0].Cells[1].Value.ToString();
                algorithm.load_formatter_infor(AAA);
                load_setting();
                current_setting_lb.Text = "当前配置:" + algorithm.load_infor;
                AAA = "";
                AAA += algorithm.load_infor + Environment.NewLine;
                for (int i = 0; i < SYS_setting.Rows.Count; i++)
                {
                    AAA += SYS_setting.Rows[i][0].ToString() + ";" + SYS_setting.Rows[i][1].ToString() + ";" + SYS_setting.Rows[i][2].ToString() + Environment.NewLine;
                }
                File.WriteAllText("sys_setting.txt", AAA);
            }
            catch (Exception)
            {

            }
        }

        int matter_place_in_kind = -1;
        private void chart1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    ////work_space_contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
                else
                {
                    //////lock (this)
                    //////{
                    ////refresh = 0;
                    double value_X = chart1.ChartAreas[0].CursorX.Position;
                    double value_Y = chart1.ChartAreas[0].CursorY.Position;
                    if (value_X < 0 || value_Y < 0)
                    {
                        return;                    
                    }
                    XYdata_value.Text = "X:" + value_X + "分    Y:" + value_Y;
                   
                    //////X_value_lb.Text = "X:" + chart1.Series[0].Points[index0].XValue.ToString();
                    //////Y_value_lb.Text = "Y:" + chart1.Series[0].Points[index0].YValues[0].ToString();
                    //////if (index0 >= 1 && index0 <= chart_data.Rows.Count)
                    //////{
                    //////    //////Y_value_lb.Text = "Y:" + chart1.ChartAreas[0].CursorY.Position.ToString();
                    ////Y_value_lb.Text = "Y:" + data_line.Rows[index0][1].ToString();
                    ////X_value_lb.Text = "X:" + data_line.Rows[index0][0].ToString();

                    if (matter_place_in_kind == 1)
                    {
             ////           XYdata_value.Text = "时间:" + chart1.ChartAreas[0].CursorX.Position + "分"
             ////+ Environment.NewLine + "信号值:" + chart1.ChartAreas[0].CursorY.Position;                       
                        component_g_G_dGView.Rows[component_enter_row].Cells[1].Value = value_X;
                        component_g_G_dGView.RefreshEdit();
                    }
                    else if (matter_place_in_kind == 2)
                    {                       
                        component_g_G_dGView.Rows[component_enter_row].Cells[2].Value = value_X;
                        component_g_G_dGView.RefreshEdit();
                    }
                    //}
                    //}
                }
            }
            catch (Exception)
            {

            }
        }
        private void matter_place_st_bx_Enter(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                matter_place_in_kind = 1;
            }
        }
        private void matter_place_ed_bx_Enter(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                matter_place_in_kind = 2;
            }
        }

        private void clear_bt_Click(object sender, EventArgs e)
        {
            algorithm.datavalue_clear();
        }

        int matter_place = -1;
        int seriport_enter_column = -1;
        void refresh_component()
        {
            if (algorithm.calculate_tab==null)
            {
                return;
            }
            try
            {
                if (component_g_G_dGView.Rows.Count != algorithm.calculate_tab.Rows.Count)
                {
                    component_g_G_dGView.Rows.Clear();
                    for (int i = 0; i < algorithm.calculate_tab.Rows.Count; i++)
                    {
                        ////////                 0     1      2        3       4      5      6      7        8           9            10                                       
                        ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；
                        ////////  11          12      13          14       15       16       17       18       19        20      
                        ////////对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；归一值；归一系数；归一方式
                        string name = algorithm.calculate_tab.Rows[i][1].ToString();
                        double start_T = Convert.ToDouble(algorithm.calculate_tab.Rows[i][2]);
                        double end_T = Convert.ToDouble(algorithm.calculate_tab.Rows[i][3]);
                        string peak_divide = algorithm.calculate_tab.Rows[i][9].ToString();
                        double peak_divide_V = 0;
                        string peak_cutting = algorithm.calculate_tab.Rows[i][8].ToString();
                        string chroma_kind = "";
                        string normalize = algorithm.calculate_tab.Rows[i][20].ToString();
                        string normalize_factor = algorithm.calculate_tab.Rows[i][19].ToString();
                        Color peak_color = Color.FromArgb(Convert.ToInt32(algorithm.calculate_tab.Rows[i][21]));
                        string chroma_adjust_kind = algorithm.calculate_tab.Rows[i][1].ToString();
                        ////非线性校准
                        if (algorithm.calculate_tab.Rows[i][14].ToString() == "1")
                        {
                            chroma_adjust_kind = "二次曲线";
                        }
                        else if (algorithm.calculate_tab.Rows[i][14].ToString() == "2")
                        {
                            chroma_adjust_kind = "分段查表";
                        }
                        if (algorithm.calculate_tab.Rows[i][15].ToString() == "1")
                        {
                            chroma_kind = "峰高";
                        }
                        else if (algorithm.calculate_tab.Rows[i][16].ToString() == "1")
                        {
                            chroma_kind = "峰半宽";
                        }
                        else if (algorithm.calculate_tab.Rows[i][17].ToString() == "1")
                        {
                            chroma_kind = "峰面积";
                        }
                        component_g_G_dGView.Rows.Add(name, start_T, end_T, peak_divide, peak_divide_V, peak_cutting, chroma_kind, normalize, normalize_factor, "确定", peak_color, chroma_adjust_kind);
                    }
                }
                else
                {
                    for (int i = 0; i < algorithm.calculate_tab.Rows.Count; i++)
                    {
                        ////////                 0     1      2        3       4      5      6      7        8           9            10                                       
                        ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；
                        ////////  11          12      13          14       15       16       17       18       19        20      
                        ////////对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；归一值；归一系数；归一方式
                        string name = algorithm.calculate_tab.Rows[i][1].ToString();
                        double start_T = Convert.ToDouble(algorithm.calculate_tab.Rows[i][2]);
                        double end_T = Convert.ToDouble(algorithm.calculate_tab.Rows[i][3]);
                        string peak_divide = algorithm.calculate_tab.Rows[i][9].ToString();
                        double peak_divide_V = 0;
                        string peak_cutting = algorithm.calculate_tab.Rows[i][8].ToString();
                        string chroma_kind = "";
                        string normalize = algorithm.calculate_tab.Rows[i][20].ToString();
                        string normalize_factor = algorithm.calculate_tab.Rows[i][19].ToString();
                        Color peak_color = Color.FromArgb(Convert.ToInt32(algorithm.calculate_tab.Rows[i][21]));
                        string chroma_adjust_kind = algorithm.calculate_tab.Rows[i][1].ToString();
                        ////非线性校准
                        if (algorithm.calculate_tab.Rows[i][14].ToString() == "1")
                        {
                            chroma_adjust_kind = "二次曲线";
                        }
                        else if (algorithm.calculate_tab.Rows[i][14].ToString() == "2")
                        {
                            chroma_adjust_kind = "分段查表";
                        }
                        if (algorithm.calculate_tab.Rows[i][15].ToString() == "1")
                        {
                            chroma_kind = "峰高";
                        }
                        else if (algorithm.calculate_tab.Rows[i][16].ToString() == "1")
                        {
                            chroma_kind = "峰半宽";
                        }
                        else if (algorithm.calculate_tab.Rows[i][17].ToString() == "1")
                        {
                            chroma_kind = "峰面积";
                        }
                        component_g_G_dGView.Rows[i].Cells[0].Value = name;
                        component_g_G_dGView.Rows[i].Cells[1].Value = start_T;
                        component_g_G_dGView.Rows[i].Cells[2].Value = end_T;
                        component_g_G_dGView.Rows[i].Cells[3].Value = peak_divide;
                        component_g_G_dGView.Rows[i].Cells[4].Value = peak_divide_V;
                        component_g_G_dGView.Rows[i].Cells[5].Value = peak_cutting;
                        component_g_G_dGView.Rows[i].Cells[6].Value = chroma_kind;
                        component_g_G_dGView.Rows[i].Cells[7].Value = normalize;
                        component_g_G_dGView.Rows[i].Cells[8].Value = normalize_factor;
                        component_g_G_dGView.Rows[i].Cells[9].Value = "确定";
                        component_g_G_dGView.Rows[i].Cells[10].Value = peak_color;
                        component_g_G_dGView.Rows[i].Cells[11].Value = chroma_adjust_kind;

                        //////component_g_G_dGView.Rows.Add(name, start_T, end_T, peak_divide, peak_divide_V, peak_cutting, chroma_kind, normalize, normalize_factor, "确定", peak_color, chroma_adjust_kind);
                    }
                }
            }
            catch (Exception) 
            {
            
            }
        }
        private void clear_chroma_bt_Click(object sender, EventArgs e)
        {
            algorithm.clear_component_group();
            refresh_component();
        }
        private void auto_add_chroma_bt_Click(object sender, EventArgs e)
        {
            algorithm.auto_add_component_group();
            refresh_component();
        }
        private void add_one_row_bt_Click(object sender, EventArgs e)
        {
            //////algorithm.calculate_tab
            try
            {
                #region 初始判断
                if (algorithm.calculate_tab == null)
                {
                    ////////                 0     1      2        3       4      5      6      7        8           9            10        11          12      13          14       15       16       17       18       19        20                         
                    ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；归一值；归一系数；归一方式
                    algorithm.calculate_tab = new DataTable();
                    algorithm.calculate_tab.Columns.Add("编号");
                    algorithm.calculate_tab.Columns.Add("名称");
                    algorithm.calculate_tab.Columns.Add("起点时间");
                    algorithm.calculate_tab.Columns.Add("结束时间");
                    algorithm.calculate_tab.Columns.Add("峰高");
                    algorithm.calculate_tab.Columns.Add("峰半宽");
                    algorithm.calculate_tab.Columns.Add("峰面积");
                    algorithm.calculate_tab.Columns.Add("浓度");
                    algorithm.calculate_tab.Columns.Add("切峰方式");
                    algorithm.calculate_tab.Columns.Add("叠峰分离方式");
                    algorithm.calculate_tab.Columns.Add("对应开始点");
                    algorithm.calculate_tab.Columns.Add("对应结束点");
                    algorithm.calculate_tab.Columns.Add("峰高点");
                    algorithm.calculate_tab.Columns.Add("峰绝对高");
                    algorithm.calculate_tab.Columns.Add("校准方式");
                    algorithm.calculate_tab.Columns.Add("峰高开");
                    algorithm.calculate_tab.Columns.Add("峰半宽开");
                    algorithm.calculate_tab.Columns.Add("峰面积开");
                    algorithm.calculate_tab.Columns.Add("归一值");
                    algorithm.calculate_tab.Columns.Add("归一系数");
                    algorithm.calculate_tab.Columns.Add("归一方式");
                    algorithm.calculate_tab.Columns.Add("颜色");
                }
                //////algorithm.calculate_tab.Columns.Add("颜色");
                if (algorithm.matter_2_line_value == null)
                {
                    algorithm.matter_2_line_value = new DataTable();
                    algorithm.matter_2_line_value.Columns.Add("coefficient_2x");
                    algorithm.matter_2_line_value.Columns.Add("coefficient_1x");
                    algorithm.matter_2_line_value.Columns.Add("coefficient_0x");
                }

                if (algorithm.airs_non_line_group_quotiety == null)
                {
                    algorithm.airs_non_line_group_quotiety = new DataSet();
                }
                #endregion
                string name = "新组份";
                if (name.Trim() == "")
                {
                    MessageBox.Show("请输入名称！");
                    return;
                }
                for (int t = 0; t < data_result.Rows.Count; t++)
                {
                    if (data_result.Rows[t][1].ToString().Trim() == name)
                    {
                        name +="1";
                        t = 0;
                    }
                }
                double st = 0;
                double ed = 0;
                //////添加组分
                algorithm.calculate_tab.Rows.Add(
                    algorithm.calculate_tab.Rows.Count + 1,  //编号
                    name, //名称
                    st, //起点间
                    ed,//结束时间
                    0, 0, 0, 0,  //峰高，峰半宽，峰面积，浓度
                    "斜切",//切峰方式
                    "前谷点",//叠峰分离方式
                    -1,//对应开始点
                    -1,//对应结束点
                    -1,//峰高点
                    0,//峰绝对高
                    "1",//校准方式  1二次曲线 2分段查表
                    "1",//峰高开
                    "0",//峰半宽开
                    "0",//峰面积开
                    "0.000%",//归一值
                     1, //归一系数
                    "归一", //归一方式                   
                    Color.Blue.ToArgb() //峰的颜色值
                    );
                algorithm.matter_2_line_value.Rows.Add(0, 1, 0);  //二次校准系数
                DataTable new_datatable = new DataTable(name);
                new_datatable.Columns.Add("m_peak");
                new_datatable.Columns.Add("m_area");
                new_datatable.Columns.Add("m_half_width");
                new_datatable.Columns.Add("matter_standard_value");
                new_datatable.Columns.Add("adjust_point");
                new_datatable.Rows.Add(1, 1, 1, 1, 0);  //分段查表 数据表
                algorithm.matter_search_group_value.Add(new_datatable);
                ////////algorithm.matter_adjust_kind_value.Rows.Add(1, 0, 1, 0, 0);
                DataTable new_datatable_1 = new DataTable(name);
                new_datatable_1.Columns.Add("m_peak");
                new_datatable_1.Columns.Add("m_peak_area");
                new_datatable_1.Columns.Add("area_half_width");
                new_datatable_1.Columns.Add("m_peak_factor");
                new_datatable_1.Columns.Add("m_peak_area_factor");
                new_datatable_1.Columns.Add("area_half_width_factor");
                new_datatable_1.Rows.Add("-1000", "-1000", "-1000", "1", "1", "1");
                new_datatable_1.Rows.Add("1000", "1000", "1000", "1", "1", "1");
                algorithm.airs_non_line_group_quotiety.Tables.Add(new_datatable_1); //分段查表 系数表    

                refresh_component();
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        private void component_g_G_dGView_CellClick(object sender, DataGridViewCellEventArgs e) ////系统设置
        { 
            int RowIndex = e.RowIndex;
            matter_place = RowIndex;
            if (RowIndex < 0)
            {
                component_g_G_dGView.ClearSelection();
                return;
            }
            int ColumnIndex = e.ColumnIndex;
            if(ColumnIndex == 9) //确定
            {
                ////try
                ////{
                #region 初始判断
                if (algorithm.calculate_tab == null)
                {
                    ////////                 0     1      2        3       4      5      6      7        8           9            10        11          12      13          14       15       16       17                              
                    ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；
                    algorithm.calculate_tab = new DataTable();
                    algorithm.calculate_tab.Columns.Add("编号");
                    algorithm.calculate_tab.Columns.Add("名称");
                    algorithm.calculate_tab.Columns.Add("起点时间");
                    algorithm.calculate_tab.Columns.Add("结束时间");
                    algorithm.calculate_tab.Columns.Add("峰高");
                    algorithm.calculate_tab.Columns.Add("峰半宽");
                    algorithm.calculate_tab.Columns.Add("峰面积");
                    algorithm.calculate_tab.Columns.Add("浓度");
                    algorithm.calculate_tab.Columns.Add("切峰方式");
                    algorithm.calculate_tab.Columns.Add("叠峰分离方式");
                    algorithm.calculate_tab.Columns.Add("对应开始点");
                    algorithm.calculate_tab.Columns.Add("对应结束点");
                    algorithm.calculate_tab.Columns.Add("峰高点");
                    algorithm.calculate_tab.Columns.Add("峰绝对高");
                    algorithm.calculate_tab.Columns.Add("校准方式");
                    algorithm.calculate_tab.Columns.Add("峰高开");
                    algorithm.calculate_tab.Columns.Add("峰半宽开");
                    algorithm.calculate_tab.Columns.Add("峰面积开");
                    algorithm.calculate_tab.Columns.Add("归一值");
                    algorithm.calculate_tab.Columns.Add("归一系数");
                    algorithm.calculate_tab.Columns.Add("归一方式");
                    algorithm.calculate_tab.Columns.Add("颜色");
                }

                if (algorithm.matter_2_line_value == null)
                {
                    algorithm.matter_2_line_value = new DataTable();
                    algorithm.matter_2_line_value.Columns.Add("coefficient_2x");
                    algorithm.matter_2_line_value.Columns.Add("coefficient_1x");
                    algorithm.matter_2_line_value.Columns.Add("coefficient_0x");
                }

                if (algorithm.airs_non_line_group_quotiety == null)
                {
                    algorithm.airs_non_line_group_quotiety = new DataSet();
                }
                #endregion
                string name = component_g_G_dGView.Rows[RowIndex].Cells[0].Value.ToString().Trim();
                if (name.Trim() == "")
                {
                    MessageBox.Show("请输入名称！");
                    return;
                }
                
                double st = Convert.ToDouble(component_g_G_dGView.Rows[RowIndex].Cells[1].Value);
                double ed = Convert.ToDouble(component_g_G_dGView.Rows[RowIndex].Cells[2].Value);

                //////添加组分
                algorithm.calculate_tab.Rows[RowIndex][1] = name;
                algorithm.calculate_tab.Rows[RowIndex][2] = st;
                algorithm.calculate_tab.Rows[RowIndex][3] = ed;

                if (component_g_G_dGView.Rows[RowIndex].Cells[6].Value.ToString() == "峰高")
                {
                    algorithm.calculate_tab.Rows[RowIndex][15] = "1";
                    algorithm.calculate_tab.Rows[RowIndex][16] = "0";
                    algorithm.calculate_tab.Rows[RowIndex][17] = "0";
                }
                else if (component_g_G_dGView.Rows[RowIndex].Cells[6].Value.ToString() == "峰面积")
                {
                    algorithm.calculate_tab.Rows[RowIndex][15] = "0";
                    algorithm.calculate_tab.Rows[RowIndex][16] = "0";
                    algorithm.calculate_tab.Rows[RowIndex][17] = "1";
                }
                else if (component_g_G_dGView.Rows[RowIndex].Cells[6].Value.ToString() == "峰半宽")
                {
                    algorithm.calculate_tab.Rows[RowIndex][15] = "0";
                    algorithm.calculate_tab.Rows[RowIndex][16] = "1";
                    algorithm.calculate_tab.Rows[RowIndex][17] = "0";
                }

                algorithm.calculate_tab.Rows[RowIndex][9] = component_g_G_dGView.Rows[RowIndex].Cells[3].Value.ToString();
                ////algorithm.calculate_tab.Rows[i][9] = component_g_G_dGView.Rows[i].Cells[4].Value;
                algorithm.calculate_tab.Rows[RowIndex][8] = component_g_G_dGView.Rows[RowIndex].Cells[5].Value.ToString();
                algorithm.calculate_tab.Rows[RowIndex][20] = component_g_G_dGView.Rows[RowIndex].Cells[7].Value.ToString();
                if (component_g_G_dGView.Rows[RowIndex].Cells[7].Value.ToString() == "归一")
                {
                    component_g_G_dGView.Rows[RowIndex].Cells[8].Value = 1;
                }  
                algorithm.calculate_tab.Rows[RowIndex][19] = component_g_G_dGView.Rows[RowIndex].Cells[8].Value.ToString();
                //for (int i = 0; i < algorithm.calculate_tab.Rows.Count; i++)
                //{
                //    algorithm.calculate_tab.Rows[i][20] = component_g_G_dGView.Rows[RowIndex].Cells[7].Value;
                //    component_g_G_dGView.Rows[i].Cells[7].Value = component_g_G_dGView.Rows[RowIndex].Cells[7].Value;
                //}

                component_g_G_dGView.Refresh();
                ////component_g_G_dGView.Rows[i].Cells[9].Value = "确定";
                ////component_g_G_dGView.Rows[i].Cells[9].Value = peak_color;
                ////component_g_G_dGView.Rows[i].Cells[9].Value = chroma_adjust_kind;                
                ////}
                ////catch (Exception)
                ////{
                ////    MessageBox.Show("请输入有效数值！");
                ////}
            }
            else if (ColumnIndex == 10) //颜色修改
            {
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    Color CCC = colorDialog1.Color;
                    algorithm.calculate_tab.Rows[RowIndex][21] = CCC.ToArgb();
                    component_g_G_dGView.Rows[RowIndex].Cells[ColumnIndex].Style.BackColor = CCC;
                }
            }
            else if (ColumnIndex == 11) //校正方式
            {                
                ////非线性校准
                if (algorithm.calculate_tab.Rows[RowIndex][14].ToString() == "1") ////二次曲线
                {
                    coefficient_2x.Text = algorithm.matter_2_line_value.Rows[RowIndex][0].ToString();
                    coefficient_1x.Text = algorithm.matter_2_line_value.Rows[RowIndex][1].ToString();
                    coefficient_0x.Text = algorithm.matter_2_line_value.Rows[RowIndex][2].ToString(); 
                    set_2_line_rbt.Checked = true;
                    adjust_unline_tab.SelectTab(1);
                }
                else if (algorithm.calculate_tab.Rows[RowIndex][14].ToString() == "2")  ////分段查表
                {
                    search_unline_rbt.Checked = true;
                    adjust_unline_tab.SelectTab(0);

                    DataView dv = algorithm.matter_search_group_value[RowIndex].DefaultView;
                    dv.Sort = "m_peak asc";
                    algorithm.matter_search_group_value[RowIndex] = dv.ToTable();
                    fenduan_line_adjust.DataSource = algorithm.matter_search_group_value[RowIndex];
                }

                #region 校准展开
                int X = this.Width;
                groupBox2.Location = new Point(X, groupBox2.Location.Y);
                groupBox2.Show();
                while (groupBox2.Location.X > this.Width - groupBox2.Width)
                {
                    ////groupBox2.Visible = false; 
                    X -= 40;
                    groupBox2.Location = new Point(X, groupBox2.Location.Y);
                    tabPage5.Refresh();
                    Thread.Sleep(50);
                    ////groupBox2.Visible = true;                   
                    ////this.Refresh();
                }
                #endregion
            }
        }
       
        private void line_adjust_close_bt_Click(object sender, EventArgs e)
        {
            ////groupBox2.Show();
            int X = groupBox2.Location.X;
            groupBox2.Location = new Point(this.Width - groupBox2.Width, groupBox2.Location.Y);
            groupBox2.Show();
            while (groupBox2.Location.X < this.Width)
            {
                //////436
                X += 40;
                groupBox2.Location = new Point(X, groupBox2.Location.Y);
                tabPage5.Refresh();
                Thread.Sleep(50);                
            }
        }
      
        int component_enter_column = -1;
        int component_enter_row = -1;
        private void component_g_G_dGView_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            component_enter_column = e.ColumnIndex;
            component_enter_row = e.RowIndex;
            if (component_enter_column == 1) //开始时间
            {
                if (tabControl1.SelectedIndex == 4)
                {
                    matter_place_in_kind = 1;
                }             
            }
            else if (component_enter_column == 2) //结束时间
            {
                if (tabControl1.SelectedIndex == 4)
                {
                    matter_place_in_kind = 2;
                }
            }
            else if (component_enter_column == 4) //峰分离值
            {
               
            }
            else if (component_enter_column == 8) //归一系数
            {
             
            }
            else if (component_enter_column == 9) //峰颜色
            {

            }
            else if (component_enter_column == 10) //确定
            {

            }
            else if (component_enter_column == 11) //校正方式
            {

            }
            else 
            {

            }

        }
        private void componen_refresh_dgview_bt_Click(object sender, EventArgs e)
        {
            refresh_component();
        } 
        private void matter_place_delete_bt_Click(object sender, EventArgs e) //组分删除
        {
            if (matter_place >= 0 && matter_place < data_result.Rows.Count)
            {
                string name = component_g_G_dGView.Rows[matter_place].Cells[0].Value.ToString().Trim();
                if (name.Trim() == "")
                {
                    MessageBox.Show("请选择需操作的组分！");
                    return;
                }
                int index = -1;
                for (int t = 0; t < data_result.Rows.Count; t++)
                {
                    if (data_result.Rows[t][1].ToString().Trim() == name)
                    {
                        index = t;
                        break;
                    }
                }
                if (index < 0)
                {
                    MessageBox.Show("此组分已经不存在！");
                    return;
                }
               
                algorithm.calculate_tab.Rows.RemoveAt(index);
                algorithm.matter_2_line_value.Rows.RemoveAt(index);
                algorithm.matter_search_group_value.RemoveAt(index);
                algorithm.airs_non_line_group_quotiety.Tables.RemoveAt(index);
                //////airs_non_line_group_quotiety                
                component_g_G_dGView.Rows.RemoveAt(matter_place);
                foreach (DataGridViewRow row in component_g_G_dGView.Rows)
                {
                    row.Selected = false;
                }
                matter_place = -1;
            }
            else 
            {
                MessageBox.Show("请选择需操作的组分！");
            }
        }

        private void search_unline_rbt_Click(object sender, EventArgs e) //分段查表
        {
            if (matter_place < 0)
            {
                MessageBox.Show("请选择需操作的组分！");
                return;
            }
            algorithm.calculate_tab.Rows[matter_place][14] = "2";            
            adjust_unline_tab.SelectTab(0);
            component_g_G_dGView.Rows[matter_place].Cells[11].Value = "分段查表";

            DataView dv = algorithm.matter_search_group_value[matter_place].DefaultView;
            dv.Sort = "m_peak asc";
            algorithm.matter_search_group_value[matter_place] = dv.ToTable();
            fenduan_line_adjust.DataSource = algorithm.matter_search_group_value[matter_place];           
        }
        private void set_2_line_rbt_Click(object sender, EventArgs e) //二次曲线
        {
            if (matter_place < 0)
            {
                MessageBox.Show("请选择需操作的组分！");
                return;
            }
            algorithm.calculate_tab.Rows[matter_place][14] = "1";
            adjust_unline_tab.SelectTab(1);
            component_g_G_dGView.Rows[matter_place].Cells[11].Value = "二次曲线";

            coefficient_2x.Text = algorithm.matter_2_line_value.Rows[matter_place][0].ToString();
            coefficient_1x.Text = algorithm.matter_2_line_value.Rows[matter_place][1].ToString();
            coefficient_0x.Text = algorithm.matter_2_line_value.Rows[matter_place][2].ToString();
        }
      
        private void base_value_work_kind_cB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (algorithm.base_value_work_kind != base_value_work_kind_cB.Text)
            {
                algorithm.base_value_work_kind = base_value_work_kind_cB.Text;
            }
        }

        private void Y_autoscroll_bt_Click(object sender, EventArgs e)
        {
            chart1.ChartAreas[0].AxisY.Interval = double.NaN;
            chart1.ChartAreas[0].AxisY.Maximum = double.NaN;
            chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset(0);
        }
        private void X_autoscroll_bt_Click(object sender, EventArgs e)
        {
            chart1.ChartAreas[0].AxisX.Interval = double.NaN;
            chart1.ChartAreas[0].AxisX.Maximum = double.NaN;
            chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
        }
        private void pause_bt_Click(object sender, EventArgs e) ////运行暂停
        {
            if (algorithm.work_pause)
            {
                pause_bt.Text = "停止";               
                algorithm.work_pause = false;
            }
            else
            {
                pause_bt.Text = "开始";                
                algorithm.work_pause = true;
            }
        }

        private void system_set_save_bt_Click(object sender, EventArgs e) ////系统校准保存
        {
            bool find = false; int index = -1;
            for (int i = 0; i < SYS_setting.Rows.Count; i++)
            {
                if (SYS_setting.Rows[i][1].ToString() == algorithm.load_infor)
                {
                    index = i;
                    find = true;
                    break;
                }
            }
            if (find)
            {                
                DateTime TTT = DateTime.Now;
                SYS_setting.Rows[index][2] = TTT.ToString();
                algorithm.write_formatter_infor(algorithm.load_infor);
                if (algorithm_W.load_infor == algorithm.load_infor)
                {
                    algorithm_W.fresh_set = true;
                }
            }
            else 
            {
                MessageBox.Show("无此名配置,请先创建配置！");
                tabControl1.SelectTab(0);
            }
        }

        ////分段查表校准
        double fenduan_standard_chroma_value = 0;
        double m_peak_value_d = 0;
        double m_peak_area_value_d = 0;
        double m_area_half_width_d = 0;
        int in_point_num_d = 0;
        private void adjust_bt2_Click(object sender, EventArgs e)
        {
            ////////                 0     1      2        3       4      5      6      7        8           9            10        11          12      13          14       15       16       17       18       19        20                         
            ////////calculate_tab  编号；名称；起点间；结束时间；峰高；峰半宽；峰面积；浓度；切峰方式；叠峰分离方式；对应开始点；对应结束点；峰高点；峰绝对高； 校准方式；峰高开；峰半宽开；峰面积开；归一值；归一系数；归一方式
            try
            {
                int index_v = matter_place;
                if (index_v < 0)
                {
                    MessageBox.Show("请先选择要校准的元素！");
                    return;
                }
                if (algorithm.matter_search_group_value[index_v].Rows.Count <= 1)
                {
                    in_point_num.Text = "0";
                    in_point_num.Enabled = false;
                }
                else
                {
                    in_point_num.Enabled = true;
                }
                fenduan_standard_chroma_value = Convert.ToDouble(standard_gas_chroma3.Text);
                if (fenduan_standard_chroma_value <= 0) { MessageBox.Show("输入浓度必须大于零！"); return; }

                m_peak_value_tx.Text = algorithm.calculate_tab.Rows[index_v][4].ToString();
                m_peak_area_value_tx.Text = algorithm.calculate_tab.Rows[index_v][6].ToString();
                m_area_half_width_value_tx.Text = algorithm.calculate_tab.Rows[index_v][5].ToString();
                m_peak_value_d = Convert.ToDouble(m_peak_value_tx.Text);
                if (m_peak_value_d == 0) { MessageBox.Show("当前测量浓度为零不能校准!"); return; }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
                m_peak_area_value_tx.Text = "0000";
            }
        }
        private void insert_into_bt_Click(object sender, EventArgs e)
        {
            int index_v = matter_place;
            if (index_v < 0)
            {
                MessageBox.Show("请先选择要校准的元素！");
                return;
            }
            try
            {
                fenduan_standard_chroma_value = Convert.ToDouble(standard_gas_chroma3.Text);
                m_peak_value_d = Convert.ToDouble(m_peak_value_tx.Text);
                m_peak_area_value_d = Convert.ToDouble(m_peak_area_value_tx.Text);
                m_area_half_width_d = Convert.ToDouble(m_area_half_width_value_tx.Text);
                in_point_num_d = Convert.ToInt16(in_point_num.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("输入的数值有误!");
            }
            if (m_peak_value_d <= 0) { MessageBox.Show("峰高有误(<=0)"); return; }
            if (m_peak_area_value_d <= 0) { MessageBox.Show("峰面积有误(<=0)"); return; }
            if (m_area_half_width_d <= 0) { MessageBox.Show("峰半宽有误(<=0)"); return; }

            if (in_point_num_d < 0) { MessageBox.Show("插入点数有误(<0)"); return; }
            if (fenduan_standard_chroma_value <= 0) { MessageBox.Show("设置浓度小于等于零标定!"); return; }

            int length = algorithm.matter_search_group_value[index_v].Rows.Count;

            for (int i = 0; i < length; i++)
            {
                if (m_peak_value_d == Convert.ToDouble(algorithm.matter_search_group_value[index_v].Rows[i][0]) && m_peak_area_value_d == Convert.ToDouble(algorithm.matter_search_group_value[index_v].Rows[i][1]) && m_area_half_width_d == Convert.ToDouble(algorithm.matter_search_group_value[index_v].Rows[i][2]))
                {
                    MessageBox.Show("分段已经包含在内");
                    return;
                }

            }
            DataRow AAA = algorithm.matter_search_group_value[index_v].NewRow();
            AAA[0] = m_peak_value_d;
            AAA[1] = m_peak_area_value_d;
            AAA[2] = m_area_half_width_d;
            AAA[3] = fenduan_standard_chroma_value;
            AAA[4] = in_point_num_d;
            algorithm.matter_search_group_value[index_v].Rows.Add(AAA);
            DataView dv = algorithm.matter_search_group_value[index_v].DefaultView;
            dv.Sort = "m_peak ASC ,m_area ASC ,m_half_width ASC";

            algorithm.matter_search_group_value[index_v] = dv.ToTable();
            fenduan_line_adjust.DataSource = algorithm.matter_search_group_value[index_v];

            algorithm.change_fenduan_value(index_v);
        }
        private void fenduan_change_in_point_Click(object sender, EventArgs e)
        {
            int index_v = matter_place;
            int fenduan_data_select_index = fenduan_line_adjust.CurrentRow.Index;
            if (index_v < 0)
            {
                MessageBox.Show("请先选择要校准的元素！");
                return;
            }
            int num = 0;
            if (fenduan_data_select_index <= 0)
            {
                MessageBox.Show("不能更改!");
                return;
            }
            try
            {
                num = Convert.ToInt16(in_point_num2.Text);
                if (num < 0)
                {
                    MessageBox.Show("数值不能小于0");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入整数");
                return;
            }

            algorithm.matter_search_group_value[index_v].Rows[fenduan_data_select_index][4] = num.ToString();
            algorithm.change_fenduan_value(index_v);            
            fenduan_data_select_index = -1;
        }
        private void delete_row_bt_Click(object sender, EventArgs e)
        {
            int index_v = matter_place;
            int fenduan_data_select_index = fenduan_line_adjust.CurrentRow.Index;
            if (index_v < 0)
            {
                MessageBox.Show("请先选择要校准的元素！");
                return;
            }

            if (algorithm.matter_search_group_value[index_v].Rows.Count == 1)
            {
                in_point_num.Text = "0";
                in_point_num.Enabled = false;
            }
            else
            {
                in_point_num.Enabled = true;
            }

            int length = algorithm.matter_search_group_value[index_v].Rows.Count - 1;

            if (algorithm.matter_search_group_value[index_v].Rows.Count <= 1)
            {
                MessageBox.Show("一行不能删除");
                return;
            }
            else if (fenduan_data_select_index < 0)
            {
                MessageBox.Show("请先选择要校准的元素！");
                return;
            }
            else
            {
                algorithm.matter_search_group_value[index_v].Rows.RemoveAt(fenduan_data_select_index);
            }
            fenduan_line_adjust.DataSource = algorithm.matter_search_group_value[index_v];
            algorithm.change_fenduan_value(index_v);
            fenduan_data_select_index = -1;
        }
        private void save_fenduan_Click(object sender, EventArgs e)
        {
            try
            {
                bool find = false; int index = -1;
                for (int i = 0; i < SYS_setting.Rows.Count; i++)
                {
                    if (SYS_setting.Rows[i][1].ToString() == setting_name_tBox.Text.ToString())
                    {
                        index = i;
                        find = true;
                        break;
                    }
                }
                if (find)
                {
                    DateTime TTT = DateTime.Now;
                    SYS_setting.Rows[index][2] = TTT.ToString();
                    algorithm.write_formatter_infor(setting_name_tBox.Text.ToString());
                }
                else
                {
                    MessageBox.Show("无此名配置,请先创建配置！");
                }
                ////int index_v = adjust_metal_cBox.SelectedIndex;
                ////if (index_v < 0)
                ////{
                ////    MessageBox.Show("请先选择要校准的元素！");
                ////    return;
                ////}               
                ////dataGridView1.DataSource = airs_non_line_group_quotiety.Tables[index_v];             
            }
            catch (Exception)
            {

            }
        }

        ////二次曲线校准  
        List<double> X_value_group = new List<double>();
        List<double> Y_value_group = new List<double>();
        private void confirm_bt2_Click(object sender, EventArgs e)
        {
            try
            {
                int index_v = matter_place;
                if (index_v < 0)
                {
                    MessageBox.Show("请先选择校准元素！");
                    return;
                }
                algorithm.matter_2_line_value.Rows[index_v][0] = Convert.ToDouble(coefficient_2x.Text);
                algorithm.matter_2_line_value.Rows[index_v][1] = Convert.ToDouble(coefficient_1x.Text);
                algorithm.matter_2_line_value.Rows[index_v][2] = Convert.ToDouble(coefficient_0x.Text);               
            }
            catch (Exception)
            {
                MessageBox.Show("设置失败！");
                return;
            }
        }
        private void get_point_Click(object sender, EventArgs e)
        {
            try
            {
                int index_v = matter_place;
                if (index_v < 0)
                {
                    MessageBox.Show("请先选择要校准的元素！");
                    return;
                }

                double Y_value = Convert.ToDouble(standard_gas_chroma2.Text);
                double X_value = 0;

                if (component_g_G_dGView.Rows[matter_place].Cells[6].Value.ToString() == "峰高")
                {
                    X_value = Convert.ToDouble(algorithm.calculate_tab.Rows[index_v][4]);
                }
                else if (component_g_G_dGView.Rows[matter_place].Cells[6].Value.ToString() == "峰面积")
                {
                    X_value = Convert.ToDouble(algorithm.calculate_tab.Rows[index_v][6]);
                }
                else if (component_g_G_dGView.Rows[matter_place].Cells[6].Value.ToString() == "峰半宽")
                {
                    X_value = Convert.ToDouble(algorithm.calculate_tab.Rows[index_v][5]);
                }

                int index = 0;
                for (int i = 0; i < X_value_group.Count; i++)
                {
                    if (X_value > X_value_group[i])
                    {
                        index = i + 1;
                    }
                }
                Y_value_group.Insert(index, Y_value);
                X_value_group.Insert(index, X_value);

                quxian_2_line_adjust.Rows.Clear();
                for (int i = 0; i < X_value_group.Count; i++)
                {
                    quxian_2_line_adjust.Rows.Add(i, X_value_group[i], Y_value_group[i], "0");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值！");
            }
        }
        private void calculate_curve_Click(object sender, EventArgs e)
        {
            if (X_value_group.Count < 2)
            {
                MessageBox.Show("请先取点(>2)!");
                return;
            }
            if (X_value_group.Count >= 3)
            {
                double[] xishu = algorithm.caculate_non_line(X_value_group.ToArray(), Y_value_group.ToArray(), 2);
                coefficient_2x.Text = xishu[3].ToString();
                coefficient_1x.Text = xishu[2].ToString();
                coefficient_0x.Text = xishu[1].ToString();

                double fangcha = algorithm.correl(X_value_group, Y_value_group);
                xishu = algorithm.caculate_non_line(X_value_group.ToArray(), Y_value_group.ToArray(), 1);
                calculate_2line_curve_inf_lb.Text = "Y = " + xishu[2].ToString("0.0000") + "*X + " + xishu[1].ToString("0.0000") + Environment.NewLine + "R = " + fangcha;
            }
            else if (X_value_group.Count == 1)
            {
                if (X_value_group[0] != 0)
                {
                    double xishu_1 = Y_value_group[0] / X_value_group[0];

                    coefficient_2x.Text = "0";
                    coefficient_1x.Text = xishu_1.ToString();
                    coefficient_0x.Text = "0";
                }
                else 
                {
                    MessageBox.Show("X参数为零！");
                }
            }
            else if (X_value_group.Count == 2)
            {
                if (X_value_group[0] != 0)
                {
                    double xishu_1 = (Y_value_group[1] - Y_value_group[0]) / (X_value_group[1] - X_value_group[0]);
                    double xishu_0 = Y_value_group[1] - xishu_1 * X_value_group[1];

                    coefficient_2x.Text = "0";
                    coefficient_1x.Text = xishu_1.ToString();
                    coefficient_0x.Text = xishu_0.ToString();
                }
                else
                {
                    MessageBox.Show("X参数相同！");
                }
            }
            else 
            {
                MessageBox.Show("请先取点");
            }
        }
        private void adjust_unline_order_Click(object sender, EventArgs e)
        {

        }
        private void adjust_unline_delete_row_Click(object sender, EventArgs e)
        {
            try
            {
                int index = quxian_2_line_adjust.CurrentRow.Index;
                if (index < 0)
                {
                    MessageBox.Show("请先选择要删除的行！");
                    return;
                }

                Y_value_group.RemoveAt(index);
                X_value_group.RemoveAt(index);

                quxian_2_line_adjust.Rows.Clear();
                for (int i = 0; i < X_value_group.Count; i++)
                {
                    quxian_2_line_adjust.Rows.Add(i, X_value_group[i], Y_value_group[i], "0000");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请先选择要删除的行！");
            }
        }
        private void get_list_fenduan_bt_Click(object sender, EventArgs e)
        {
            try
            {
                double index_0 = 0;
                double index_1 = 0;
                double index_2 = 0;
                Y_value_group.Clear();
                X_value_group.Clear();
                quxian_2_line_adjust.Rows.Clear();
                if (algorithm.calculate_tab.Rows[matter_place][15].ToString() == "1") //峰高
                {
                    for (int i = 0; i < algorithm.matter_search_group_value[matter_place].Rows.Count; i++)
                    {
                        index_0 = i + 1;
                        index_1 = Convert.ToDouble(algorithm.matter_search_group_value[matter_place].Rows[i][0]);
                        index_2 = Convert.ToDouble(algorithm.matter_search_group_value[matter_place].Rows[i][3]);
                        Y_value_group.Add(index_2);
                        X_value_group.Add(index_1);
                        quxian_2_line_adjust.Rows.Add(index_0, index_1, index_2, "0");
                    }
                }
                else if (algorithm.calculate_tab.Rows[matter_place][17].ToString() == "1") //峰面积
                {
                    for (int i = 0; i < algorithm.matter_search_group_value[matter_place].Rows.Count; i++)
                    {
                        index_0 = i + 1;
                        index_1 = Convert.ToDouble(algorithm.matter_search_group_value[matter_place].Rows[i][1]);
                        index_2 = Convert.ToDouble(algorithm.matter_search_group_value[matter_place].Rows[i][3]);
                        Y_value_group.Add(index_2);
                        X_value_group.Add(index_1);
                        quxian_2_line_adjust.Rows.Add(index_0, index_1, index_2, "0");
                    }
                }
                else if (algorithm.calculate_tab.Rows[matter_place][16].ToString() == "1") //峰半宽
                {
                    for (int i = 0; i < algorithm.matter_search_group_value[matter_place].Rows.Count; i++)
                    {
                        index_0 = i + 1;
                        index_1 = Convert.ToDouble(algorithm.matter_search_group_value[matter_place].Rows[i][2]);
                        index_2 = Convert.ToDouble(algorithm.matter_search_group_value[matter_place].Rows[i][3]);
                        Y_value_group.Add(index_2);
                        X_value_group.Add(index_1);
                        quxian_2_line_adjust.Rows.Add(index_0, index_1, index_2, "0");
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void system_set_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                timer1.Stop();
                ////this.Close();
            }
            catch (Exception) 
            {
            }
        }
        private void read_history_F_bt_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "历史文件|*.zgmd";
            openFileDialog1.DefaultExt = "zgmd";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file_path = openFileDialog1.FileName;
                algorithm.load_data_file_infor(file_path);
            }
        }

        private void delete_one_row_bt_Click(object sender, EventArgs e)
        {
            int index = component_g_G_dGView.CurrentRow.Index;
            if (index >= 0)
            {
                algorithm.calculate_tab.Rows.RemoveAt(index);
                algorithm.matter_2_line_value.Rows.RemoveAt(index);
                algorithm.matter_search_group_value.RemoveAt(index);
                algorithm.airs_non_line_group_quotiety.Tables.RemoveAt(index);
                component_g_G_dGView.Rows.RemoveAt(index);
                //////airs_non_line_group_quotiety
                //////matter_place_name_tx.Text = "";
                foreach (DataGridViewRow row in component_g_G_dGView.Rows)
                {
                    row.Selected = false;
                }               
            }
            else
            {
                MessageBox.Show("请选择需操作的组分！");
            }
        }
        private void up_one_row_bt_Click(object sender, EventArgs e)
        {
            int index = component_g_G_dGView.CurrentRow.Index;
            if (index > 0)
            {
                int index0 = index - 1;
                algorithm.calculate_tab = SwapRow(index0, index, algorithm.calculate_tab);
                algorithm.matter_2_line_value = SwapRow(index0, index, algorithm.matter_2_line_value);

                DataTable DDD = algorithm.matter_search_group_value[index0];
                algorithm.matter_search_group_value[index0] = algorithm.matter_search_group_value[index];
                algorithm.matter_search_group_value[index] = DDD;
                algorithm.airs_non_line_group_quotiety = SwapRow(index0, index, algorithm.airs_non_line_group_quotiety);
                refresh_component();
                component_g_G_dGView.Rows[index0].Selected = true;
                component_g_G_dGView.CurrentCell = component_g_G_dGView.Rows[index0].Cells[0];  
            }           
        }
        private void down_one_row_bt_Click(object sender, EventArgs e)
        {
            int index = component_g_G_dGView.CurrentRow.Index;
            if (index < component_g_G_dGView.Rows.Count - 1)
            {
                int index0 = index + 1;
                algorithm.calculate_tab = SwapRow(index0, index, algorithm.calculate_tab);
                algorithm.matter_2_line_value = SwapRow(index0, index, algorithm.matter_2_line_value);

                DataTable DDD = algorithm.matter_search_group_value[index0];
                algorithm.matter_search_group_value[index0] = algorithm.matter_search_group_value[index];
                algorithm.matter_search_group_value[index] = DDD;
                algorithm.airs_non_line_group_quotiety = SwapRow(index0, index, algorithm.airs_non_line_group_quotiety);
                refresh_component();
                component_g_G_dGView.Rows[index0].Selected = true;
                component_g_G_dGView.CurrentCell = component_g_G_dGView.Rows[index0].Cells[0];
            }       
        }
       
        private DataTable SwapRow(int index1, int index2, DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dr.ItemArray = dt.Rows[index1].ItemArray;
            dt.Rows[index1].ItemArray = dt.Rows[index2].ItemArray;
            dt.Rows[index2].ItemArray = dr.ItemArray;
            return dt;
        }
        private DataSet SwapRow(int index1, int index2, DataSet dt)
        {
            DataSet DDD = new DataSet();
            for (int i = 0; i < dt.Tables.Count;i++ )
            {
                if (i == index2 && index1 < dt.Tables.Count)
                {
                    DDD.Tables.Add(dt.Tables[index1].Copy());
                }
                else if (i == index1 && index2 < dt.Tables.Count)
                {
                    DDD.Tables.Add(dt.Tables[index2].Copy());
                }
                else
                {
                    DDD.Tables.Add(dt.Tables[i].Copy());
                }
            }
            return DDD;          
        }    
    }
}
