FrmSPC.cs 8.1 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Comm;
using Dal;

namespace App
{
    public partial class FrmSPC : App.FrmBase
    {
        Fuction fuction = new Fuction();
        List<string> product;
        bool isInit;
        DataTable dt_ProductTestData;

        public FrmSPC()
        {
            isInit = true;
            InitializeComponent();
            Init();
            this.SetLanguage(this);
        }
        private void Init()
        {
            GetProductList();
            cmb_SPCType.SelectedIndex = 0;
        }
        private void GetProductList()
        {
            this.lb_Product.Items.Clear();
            product = fuction.GetAllProjectName();
            for (int i = 0; i < product.Count; i++)
            {
                this.lb_Product.Items.Add(product[i]);
            }
            this.lb_Product.SelectedIndex = 0;
            GetDataGrid();
        }
        private void GetDataGrid()
        {
            string ProductName = this.lb_Product.SelectedItem.ToString();

            dt_ProductTestData = fuction.GetProductTest(ProductName);

            dataGridView1.DataSource = GetTable(Convert.ToDateTime("1900-1-1"), Convert.ToDateTime("2900-1-1"));

            DataGridViewCellStyle dataGridViewCellStyle0 = CreatDataGridViewCellStyle();
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                CreatColumn(dataGridView1.Columns[i], dataGridViewCellStyle0, dt_ProductTestData.Columns[i].ColumnName);
            }
            if (dataGridView1.RowCount > 0)
            {
                dataGridView1.Rows[0].Selected = true;
            }
        }
        private void CreatColumn(DataGridViewColumn Column, DataGridViewCellStyle CellStyle, string Name)
        {
            Column.DefaultCellStyle = CellStyle;
            Column.FillWeight = 40F;
            Column.Name = Name;
            Column.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
            Column.Width = 150;
            if (Name == Const.TEST_MAX || Name == Const.TEST_MIN)
            {
                Column.Visible = false;
            }
        }
        private void btn_Query_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = GetTable(Convert.ToDateTime(this.dtp_StartTime.Value.ToShortDateString()), Convert.ToDateTime(this.dtp_EndTime.Value.ToShortDateString()));
        }
        private DataTable GetTable(DateTime StartTime, DateTime EndTime)
        {
            DataRow[] drTemp = dt_ProductTestData.Select
                (Const.TEST_STARTTIME + ">= #" + StartTime + "# and " + Const.TEST_STARTTIME + "<= #" + EndTime + "#", Const.TEST_STARTTIME);

            DataTable table1 = new DataTable(Const.PARA_PRODUCT);
            DataColumnCollection columns = table1.Columns;
            columns.Add(Const.TestData, typeof(System.String));
            columns.Add(Const.PARA_PRODUCT, typeof(System.String));
            columns.Add(Const.TEST_STARTTIME, typeof(System.DateTime));
            columns.Add(Const.TEST_MAX, typeof(System.Double));
            columns.Add(Const.TEST_MIN, typeof(System.Double));
            DataRow newRow;
            foreach (DataRow dr in drTemp)
            {
                newRow = table1.NewRow();
                newRow[Const.TestData] = dr[Const.TestData];
                newRow[Const.PARA_PRODUCT] = dr[Const.PARA_PRODUCT];
                newRow[Const.TEST_STARTTIME] = dr[Const.TEST_STARTTIME];
                newRow[Const.TEST_MAX] = Fuction.m_UserResult == Const.RESULT_IN ?
                        Convert.ToDouble(fuction.ChangeAreaUnit(dr[Const.TEST_MAX].ToString())) :
                    Convert.ToDouble(dr[Const.TEST_MAX]);
                newRow[Const.TEST_MIN] = Fuction.m_UserResult == Const.RESULT_IN ?
                            Convert.ToDouble(fuction.ChangeAreaUnit(dr[Const.TEST_MIN].ToString())) :
                        Convert.ToDouble(dr[Const.TEST_MIN]);
                table1.Rows.Add(newRow);
            }
            return table1;
        }


        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow !=null)
            {
                ((DataTable)this.dataGridView1.DataSource).Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            #region checkValid
            if (!CheckNull(this.lbl_minValue.Text, txt_minValue) || !CheckNumber(this.lbl_minValue.Text, txt_minValue) ||
                !CheckNull(this.lbl_maxValue.Text, txt_maxValue) || !CheckNumber(this.lbl_maxValue.Text, txt_maxValue) ||
                !CheckNull(this.label1.Text, txt_TargetValue) || !CheckNumber(this.label1.Text, txt_TargetValue))
                return;
            #endregion

            double minValue = Convert.ToDouble(txt_minValue.Text);
            double maxValue = Convert.ToDouble(this.txt_maxValue.Text);
            double targetValue = Convert.ToDouble(this.txt_TargetValue.Text);

            if (minValue > maxValue ||
                minValue > targetValue ||
                targetValue > maxValue)
            {
                ShowMessageBox.ShowError(Const.MESSAGEBOX_TITLE_NOTE, "", Const.ERROR_GAUGE_DATA);
                return;
            }
            FrmSPCCurve frmSPCCurve = new FrmSPCCurve((DataTable)this.dataGridView1.DataSource, maxValue, targetValue, minValue, cmb_SPCType.SelectedItem.ToString());
            frmSPCCurve.Owner = this.ParentForm;
            frmSPCCurve.MdiParent = this.ParentForm;
            frmSPCCurve.ShowInTaskbar = false;
            frmSPCCurve.StartPosition = FormStartPosition.CenterScreen;
            frmSPCCurve.Show();
            frmSPCCurve.Activate();//
            this.Close();

        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lb_Product_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!isInit)
            {
                GetDataGrid();
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                dataGridView1.Rows[e.RowIndex].Selected = true;
                this.btn_Delete.Enabled = true;
            }
            else
            {
                this.btn_Delete.Enabled = false;
            }
        }

        private void ChangeNewPoint(Control ctrl,int bottom)
        {
            ctrl.Location = new Point(ctrl.Left, bottom - ctrl.Height-5);
        }
        private void FrmSPC_Shown(object sender, EventArgs e)
        {
            ResizeForm();
            isInit = false;
            int bottom = this.Bottom - 65;
            lb_Product.Height = this.Height - lb_Product.Top-50;

            ChangeNewPoint(label1,bottom);
            ChangeNewPoint(txt_TargetValue,bottom-3);
            ChangeNewPoint(lbl_SPCType,bottom);
            ChangeNewPoint(cmb_SPCType, bottom-5);
            

            int bottom1=this.Bottom - 100;
            ChangeNewPoint(lbl_maxValue,bottom1);
            ChangeNewPoint(lbl_minValue,bottom1);
            ChangeNewPoint(txt_maxValue, bottom1-3);
            ChangeNewPoint(txt_minValue, bottom1-3);


            dataGridView1.Height = this.Height - dataGridView1.Top -150;

            dataGridView1.Width  = this.Width -dataGridView1.Top -150;
            int right = dataGridView1.Right;

            btn_OK.Location = new Point(right - btn_Cancel.Width - 70, bottom - btn_OK.Height);
            btn_Delete.Location = new Point(right - btn_Delete.Width, bottom1 - btn_Delete.Height);
            btn_Query.Location = new Point(right - btn_Query.Width, btn_Query.Top);
            btn_Cancel.Location = new Point(right - btn_Cancel.Width, bottom - btn_Cancel.Height);
        }
       

        private void FrmSPC_LocationChanged(object sender, EventArgs e)
        {
            if (isInit)
            {
                this.Location = new Point(0, 0);
            }
        }


     }
}