transitioninfodlg.cs

来自「这个是自己制作的工作流设计器,可以可视化的拖拉」· CS 代码 · 共 432 行

CS
432
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WorkflowDesigner.Designer
{
    /// <summary>
    /// 流转对话框
    /// </summary>
    public partial class TransitionInfoDlg : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TransitionInfoDlg()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 流转
        /// </summary>
        private WfTransition _wfTransition;

        /// <summary>
        /// 流转
        /// </summary>
        public WfTransition WfTransition
        {
            get { return _wfTransition; }
            set { _wfTransition = value; }
        }

        private IList<TransCondition> _list = new List<TransCondition>();

        public IList<TransCondition> List
        {
            get { return _list; }
            set { _list = value; }
        }

      

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            // 新增一行
            if (this.IsVaild())
            {
                SaveInfo();
                AddRow();
            }

        }
        private void SaveInfo()
        {
            this._list.Clear();
            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                TransCondition tc = new TransCondition();
                tc.Param = this.GetSelectedComboBoxItem(dr.Cells[0] as DataGridViewComboBoxCell);
                tc.Oper = (dr.Cells[1] as DataGridViewComboBoxCell).Value.ToString();
                if (tc.Param.ParamType == "0")
                { 
                    //整型
                    tc.Value = dr.Cells[2].Value.ToString();
                }
                else if (tc.Param.ParamType == "1")
                { 
                    //字符串
                    tc.Value = dr.Cells[2].Value.ToString();
                }
                else if (tc.Param.ParamType == "2")
                {
                    tc.Value = (dr.Cells[3] as DataGridViewComboBoxCell).Value.ToString();
                }
                this._list.Add(tc);
            }
            
        }

        private void BindParams(DataGridViewComboBoxCell ComboBox)
        {
            ComboBox.DisplayMember = "Name";
            ComboBox.ValueMember = "Name";
            //得到所有的参数
            if (!(this._wfTransition.StartActivity is WfStartActivity))
            {
                IList<WfParam> list = (this._wfTransition.StartActivity as WfActivity).ParamsInfo;
                //得到目前条件中已使用的参数
                foreach(WfParam param in list)
                {
                    IList<TransCondition> tcs = this._list;
                    bool IsExists = false;
                    foreach(TransCondition tc in tcs)
                    {
                        if (tc.Param == param)
                        {
                            IsExists = true;
                            break;
                        }
                    }
                    if (!IsExists)
                    ComboBox.Items.Add(param);
                }

            }
        }
        private void BindOpers(DataGridViewComboBoxCell ComboBox)
        {
            //构建DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(String));
            dt.Columns.Add("Name", typeof(String));

            DataRow dr = dt.NewRow();
            dr["ID"] = "=";
            dr["Name"] = "=";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "!=";
            dr["Name"] = "!=";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = ">";
            dr["Name"] = ">";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = ">=";
            dr["Name"] = ">=";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["ID"] = "<";
            dr["Name"] = "<";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["ID"] = "<=";
            dr["Name"] = "<=";
            dt.Rows.Add(dr);


            ComboBox.DataSource = dt;
            ComboBox.DisplayMember = "Name";
            ComboBox.ValueMember = "ID";
        }
        private void BindValues(DataGridViewComboBoxCell ComboBox)
        {
            //构建DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(String));
            dt.Columns.Add("Name", typeof(String));

            DataRow dr = dt.NewRow();
            dr["ID"] = "是";
            dr["Name"] = "True";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "否";
            dr["Name"] = "False";
            dt.Rows.Add(dr);

            ComboBox.DataSource = dt;
            ComboBox.DisplayMember = "Name";
            ComboBox.ValueMember = "ID";
        }
        private void AddRow()
        {
            //将之前的数据保存到xml中去
            //新增行
            DataGridViewRow dr = new DataGridViewRow();
            //新增列参数名
            DataGridViewComboBoxCell ComboBox = new DataGridViewComboBoxCell();
            BindParams(ComboBox);
            
            
            //操作
            DataGridViewComboBoxCell ComboBoxOper = new DataGridViewComboBoxCell();
            BindOpers(ComboBoxOper);

            //判断参数名的类型
            //值
            DataGridViewTextBoxCell TextValue = new DataGridViewTextBoxCell();
            //TextValue.ReadOnly = true;
             //值2
            DataGridViewComboBoxCell ComboBoxValue = new DataGridViewComboBoxCell();
            BindValues(ComboBoxValue);

            dr.Cells.Add(ComboBox);
            dr.Cells.Add(ComboBoxOper);
            dr.Cells.Add(TextValue);
            dr.Cells.Add(ComboBoxValue);
            this.dataGridView1.Rows.Add(dr);
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if(this.IsVaild())
            {
                this.Save();
            }
        }

        private bool IsVaild()
        {
            int i=1;
            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                TransCondition tc = new TransCondition();
                if (dr.Cells[0].Value== null||dr.Cells[0].Value.ToString() == "")
                {
                    MessageBox.Show("请选择第" + i.ToString() + "行的参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                if (dr.Cells[1].Value == null || dr.Cells[1].Value.ToString() == "")
                {
                    MessageBox.Show("请选择第" + i.ToString() + "行的操作类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                tc.Param = this.GetSelectedComboBoxItem(dr.Cells[0] as DataGridViewComboBoxCell);
                


                if (tc.Param.ParamType == "0")
                {
                    if (dr.Cells[2].Value == null || dr.Cells[2].Value.ToString() == "")
                    {
                        MessageBox.Show("请填写第"+i.ToString()+"行的值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    }
                    //整型
                    if (!CommFunction.IsInteger(dr.Cells[2].Value.ToString()))
                    {
                        MessageBox.Show(tc.Param.Name+"的类型为整型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        dr.Cells[2].Value = "";
                        return false;
                    }
                    //tc.Value = (dr.Cells[2] as DataGridViewComboBoxCell).Value;
                }
                else if (tc.Param.ParamType == "1")
                {
                    if (dr.Cells[2].Value == null||dr.Cells[2].Value.ToString() == "")
                    {
                        MessageBox.Show("请填写第" + i.ToString() + "行的值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    
                    }
                //    //字符串
                //    tc.Value = (dr.Cells[2] as DataGridViewComboBoxCell).Value;
                }
                else if (tc.Param.ParamType == "2")
                {
                    if (dr.Cells[3].Value == null ||dr.Cells[3].Value.ToString() == "")
                    {
                        MessageBox.Show("请填写第" + i.ToString() + "行的值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    }
                //    tc.Value = (dr.Cells[3] as DataGridViewComboBoxCell).Value;
                }
                 i++;
            }
            return true;
        }

        private void Save()
        {
            

            try
            {
                //保存信息
                this.SaveInfo();
                this._wfTransition.Name = this.textBoxName.Text.Trim();
                this._wfTransition.Conditions = this._list;
                MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void TransitionInfoDlg_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            LoadTCInfo();
            this.textBoxName.Text = this._wfTransition.Name;
        }

        private void LoadTCInfo()
        {
            IList<TransCondition> list = this._wfTransition.Conditions;
            int i = 0;
            foreach (TransCondition tc in list)
            { 
                this.AddRow();
                this.dataGridView1.Rows[i].Cells[0].Value = tc.Param.Name;
                this.dataGridView1.Rows[i].Cells[1].Value = tc.Oper;

                String ParamType = "1";
                if (tc.Param != null)
                {
                    ParamType = tc.Param.ParamType;

                }

                this.dataGridView1.Rows[i].Cells[2].ReadOnly = true;
                this.dataGridView1.Rows[i].Cells[3].ReadOnly = true;
                if (ParamType == "2")
                {
                    this.dataGridView1.Rows[i].Cells[2].Value = "";
                    this.dataGridView1.Rows[i].Cells[3].ReadOnly = false;
                    this.dataGridView1.Rows[i].Cells[3].Value = tc.Value;
                }
                else
                {
                    this.dataGridView1.Rows[i].Cells[3].Value = "";
                    this.dataGridView1.Rows[i].Cells[2].ReadOnly = false;
                    this.dataGridView1.Rows[i].Cells[2].Value = tc.Value;
                }
                
                i++;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private WfParam GetSelectedComboBoxItem(DataGridViewComboBoxCell ComboBox)
        { 
            foreach(WfParam param in ComboBox.Items)
            {
                if (param.Name == ComboBox.Value.ToString())
                {
                    return param;
                }
            }
            return null;
        }
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;
            int cellindex = e.ColumnIndex;
            if (rowindex >=0 && cellindex == 0)
            {
                DataGridViewComboBoxCell ComboBox = (DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells[0];
                WfParam param = GetSelectedComboBoxItem(ComboBox);
                String ParamType = "1";
                if (param != null)
                {
                     ParamType = param.ParamType;
                
                }
                
                this.dataGridView1.Rows[rowindex].Cells[2].ReadOnly = true;
                this.dataGridView1.Rows[rowindex].Cells[3].ReadOnly = true;
                if (ParamType == "2")
                {
                    this.dataGridView1.Rows[rowindex].Cells[2].Value = "";
                    this.dataGridView1.Rows[rowindex].Cells[3].ReadOnly = false;
                }
                else
                {
                    this.dataGridView1.Rows[rowindex].Cells[3].Value = "";
                    this.dataGridView1.Rows[rowindex].Cells[2].ReadOnly = false;
                }
                
            }
            if (rowindex >= 0 && cellindex == 2)
            {
                DataGridViewComboBoxCell ComboBox = (DataGridViewComboBoxCell)this.dataGridView1.Rows[rowindex].Cells[0];
                WfParam param = GetSelectedComboBoxItem(ComboBox);
                String ParamType = "1";
                if (param != null)
                {
                    ParamType = param.ParamType;

                }

                if (ParamType == "0")
                {
                    if (this.dataGridView1.Rows[rowindex].Cells[2].Value.ToString() != "")
                    {
                        if (!CommFunction.IsInteger(this.dataGridView1.Rows[rowindex].Cells[2].Value.ToString()))
                        {
                            MessageBox.Show(param.Name + "的值为整型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.dataGridView1.Rows[rowindex].Cells[2].Value = "";
                            this.dataGridView1.Rows[rowindex].Cells[2].Selected = true;
                        }
                    }
                }
            }

        }
        
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            //if (this.IsVaild())
            //{
               // SaveInfo();

                if (MessageBox.Show("是否删除该条件!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DeleteRow();
                }
           // }
        }

        private void DeleteRow()
        {

            foreach (DataGridViewRow dr in this.dataGridView1.SelectedRows)
            {
                this.dataGridView1.Rows.Remove(dr);
            }
        }
 
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?