/******************************************************************************* * Copyright(c) 2014 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:F_MST_0413.cs * 2.功能描述:PLC连接参数 * 编辑履历: * 作者 日期 版本 修改内容 * 陈晓野 2017/11/14 1.00 新建 *******************************************************************************/ using System; using System.Data; using System.Windows.Forms; using Dongke.IBOSS.PRD.Basics.BaseResources; using Dongke.IBOSS.PRD.Basics.BaseControls; using Dongke.IBOSS.PRD.Client.CommonModule; using Dongke.IBOSS.PRD.Basics.Library; using Dongke.IBOSS.PRD.WCF.Proxys; using Dongke.IBOSS.PRD.Client.DataModels; using Dongke.IBOSS.PRD.WCF.DataModels; namespace Dongke.IBOSS.PRD.Client.SystemModule { public partial class F_MST_0413 : FormBase { #region 成员变量 // 窗体的单例模式 private static F_MST_0413 _instance; // 条码打印机数据源 private DataTable _dtSourse; #endregion #region 构造函数 /// /// 构造函数 /// public F_MST_0413() { InitializeComponent(); // 窗口标题 //this.Text = FormTitles.F_MST_0411; // 按钮 this.btnSave.Text = ButtonText.BTN_SAVE; this.btnCancel.Text = ButtonText.BTN_CLOSE; } #endregion #region 单例模式 /// /// 单例模式,防止重复创建窗体 /// public static F_MST_0413 Instance { get { if (_instance == null) { _instance = new F_MST_0413(); } return _instance; } } #endregion #region 事件 /// /// 窗体加载 /// /// /// private void F_MST_0411_Load(object sender, System.EventArgs e) { try { // 设置datagridview不自动创建列 this.dgvPrinter.AutoGenerateColumns = false; // 加载打印机数据 ClientRequestEntity cre = new ClientRequestEntity(); cre.NameSpace = "MST0413"; cre.Name = "GetAllPLC"; ServiceResultEntity sre = CommonModuleProxy.Service.DoRequest(cre); this._dtSourse = sre.Data.Tables[0]; this.RefreshDataGridViewData(); this.dgvPrinter.IsSetInputColumnsColor = true; //权限控制 //FormPermissionManager.FormPermissionControl(this.Name, this, // LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData, // LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData); this.dgvPrinter.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 点击右上角关闭按钮 /// /// /// private void F_MST_0411_FormClosing(object sender, FormClosingEventArgs e) { try { // 关闭的时候需要判断是否有数据变化 if (DataJudge.IsChange((DataTable)this.dgvPrinter.DataSource)) { DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); // 保存改变的数据 if (dialogResult == DialogResult.Yes) { if (Conservation() == -1) { e.Cancel = true; return; } // 异步处理 object result = DoAsync(new BaseAsyncMethod(SaveData)); // 保存成功,不关闭窗体 if (Convert.ToInt32(result) > Constant.INT_IS_ZERO) { e.Cancel = true; MessageBox.Show(string.Format(Messages.MSG_CMN_I001, this.Text, "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnSave.Enabled = false; // 加载产品缺陷管理数据 this._dtSourse.AcceptChanges(); } else if (Convert.ToInt32(result) == -1) { e.Cancel = true; MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "PLC名"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { e.Cancel = true; MessageBox.Show(string.Format(Messages.MSG_CMN_W001, this.Text, "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else if (dialogResult == DialogResult.Cancel) { e.Cancel = true; } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 窗体关闭后,释放单例资源 /// /// /// private void F_MST_0411_FormClosed(object sender, FormClosedEventArgs e) { _instance = null; } /// /// 选中新行时,设置默认值 /// /// /// private void dgvDataDefect_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { // 设置有效属性为选中状态 e.Row.Cells["ValueFlag"].Value = Constant.INT_IS_ONE; e.Row.Cells["DisplayNo"].Value = e.Row.Index + 1; } /// /// 产品缺陷管理信息输入格式控制 /// /// /// private void dgvDataDefect_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { // 只对文本框做控制 if (e.Control is DataGridViewTextBoxEditingControl) { DataGridViewTextBoxEditingControl EditingControl = (DataGridViewTextBoxEditingControl)e.Control; } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 单元格验证,检查 /// /// /// private void dgvDataDefect_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { try { if (this.ActiveControl != null && "btnClose".Equals(this.ActiveControl.Name)) { return; } if (!dgvPrinter.CurrentRow.IsNewRow) { DataGridViewRow rowItem = dgvPrinter.Rows[e.RowIndex]; DataGridViewColumn columnItem = dgvPrinter.Columns[e.ColumnIndex]; object columnvalue = rowItem.Cells[columnItem.Name].EditedFormattedValue; // 打印机名 if ("PLCName".Equals(columnItem.Name)) { if (columnvalue != null && !string.IsNullOrEmpty(columnvalue + string.Empty)) { if (!Utility.IsUnique(columnvalue.ToString(), dgvPrinter.CurrentRow.Index, this.dgvPrinter, "PLCName")) { // 单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Format( Messages.MSG_CMN_W006, "PLC名"); e.Cancel = true; this.btnSave.Enabled = false; return; } } } // 清除单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Empty; } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 根据数据是否变化,设置保存按钮的可用状态 /// /// /// private void dgvDataDefect_CellValidated(object sender, DataGridViewCellEventArgs e) { try { this.SetSaveBtnStatus(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 行校验 /// /// /// private void dgvDataDefect_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { try { // 按Esc键时不校验 if (!dgvPrinter.IsCurrentRowDirty) { this.dgvPrinter.IsSetInputColumnsColor = true; return; } if (this.ActiveControl != null && "btnClose".Equals(this.ActiveControl.Name)) { // 清除单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Empty; return; } DataGridViewRow row = dgvPrinter.CurrentRow; if (!row.IsNewRow) { // 判断打印机名不能为空 if (row.Cells["PLCName"].Value == null || string.IsNullOrEmpty(row.Cells["PLCName"].Value + string.Empty)) { // 单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "PLC名"); e.Cancel = true; this.btnSave.Enabled = false; return; } if (row.Cells["IP"].Value == null || string.IsNullOrEmpty(row.Cells["IP"].Value + string.Empty)) { // 单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "IP"); e.Cancel = true; this.btnSave.Enabled = false; return; } if (row.Cells["Port"].Value == null || string.IsNullOrEmpty(row.Cells["Port"].Value + string.Empty)) { // 单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "端口"); e.Cancel = true; this.btnSave.Enabled = false; return; } if (row.Cells["dec"].Value == null || string.IsNullOrEmpty(row.Cells["dec"].Value + string.Empty)) { // 单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Format(Messages.MSG_CMN_W005, "软元件描述"); e.Cancel = true; this.btnSave.Enabled = false; return; } // 清除单元格的错误消息 this.dgvPrinter.CurrentRow.ErrorText = string.Empty; } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 根据数据是否变化,设置保存按钮的可用状态 /// /// /// private void dgvDataDefect_RowValidated(object sender, DataGridViewCellEventArgs e) { try { this.SetSaveBtnStatus(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 根据是否选中停用数据,加载数据 /// /// /// private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e) { try { this.RefreshDataGridViewData(); this.SetSaveBtnStatus(); this.dgvPrinter.IsSetInputColumnsColor = true; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 单元格输入时,给自动生成的行设置颜色 /// /// /// private void dgvDataDefect_UserAddedRow(object sender, DataGridViewRowEventArgs e) { this.dgvPrinter.IsSetInputColumnsColor = true; } /// /// 设置排序时列表的颜色 /// private void dgvDataDefect_Sorted(object sender, EventArgs e) { this.dgvPrinter.IsSetInputColumnsColor = true; } /// /// 保存 /// /// /// private void btnSave_Click(object sender, EventArgs e) { try { int results = Conservation(); //判断单元格必输项不能为空 if (results == -1) { return; } // 异步处理 this.btnSave.Enabled = false; this.btnCancel.Enabled = false; int result = (int)DoAsync(new BaseAsyncMethod(SaveData)); this.btnSave.Enabled = true; this.btnCancel.Enabled = true; // 缺陷类别数据保存成功 if (result == 1) { MessageBox.Show(string.Format(Messages.MSG_CMN_I001, this.Text, "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); // 加载缺陷类别管理数据 this._dtSourse.AcceptChanges(); this.btnSave.Enabled = false; } else if (result == -1) { MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "PLC名"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show(string.Format(Messages.MSG_CMN_W001, this.Text, "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } this.dgvPrinter.IsSetInputColumnsColor = true; } catch (Exception ex) { this.btnSave.Enabled = true; this.btnCancel.Enabled = true; // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 点击关闭按钮 /// /// /// private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } /// /// 添加新行 /// /// /// private void dgvPrinter_NewRowNeeded(object sender, DataGridViewRowEventArgs e) { try { //DataTable dt =(dgvPrinter.DataSource as DataTable); //if(dt == null || dt.Rows.Count == 0) //{ // e.Row.Cells["DispalyNo"].Value = 1; //} //DataRow[] drs = dt.Select("valueflag = 1","DispalyNo desc"); //if(drs == null || drs.Length == 0) //{ // e.Row.Cells["DispalyNo"].Value = 1; //} //else //{ // e.Row.Cells["DispalyNo"].Value = Convert.ToInt32(drs[0]) + 1; //} e.Row.Cells["DispalyNo"].Value = e.Row.Index + 1; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } #endregion #region 私有方法 /// /// 得到显示数据 /// /// private void RefreshDataGridViewData() { try { if (this.chkDisplayDisabledData.Checked == false) { this._dtSourse.DefaultView.RowFilter = "ValueFlag=1"; } else { this._dtSourse.DefaultView.RowFilter = null; } this.dgvPrinter.DataSource = _dtSourse; this.dgvPrinter.AutoResizeColumns(); this.btnSave.Enabled = false; } catch (Exception ex) { throw ex; } } /// /// 保存数据 /// /// 是否成功 private object SaveData() { try { // 获取当前数据源 ClientRequestEntity cre = new ClientRequestEntity(); cre.NameSpace = "MST0413"; cre.Name = "SetPLC"; cre.Data = this._dtSourse.DataSet; ServiceResultEntity sre = CommonModuleProxy.Service.DoRequest(cre); if (sre.Status == Constant.ServiceResultStatus.Success) { return 1; } else if (sre.Status == Constant.ServiceResultStatus.DataDuplicated) { return -1; } else { return 0; } } catch (Exception ex) { throw ex; } } /// /// 设置保存按钮的可用状态 /// private void SetSaveBtnStatus() { if (DataJudge.IsChange((DataTable)this.dgvPrinter.DataSource)) { this.btnSave.Enabled = true; } else { this.btnSave.Enabled = false; } } /// /// 保存时单元格必输项不能为空 /// private int Conservation() { DataTable dataDefectData = (DataTable)this.dgvPrinter.DataSource; DataRow[] d = dataDefectData.Select("PLCName is null or PLCName = '' or IP is null or IP = '' or Port is null or PLCDescription is null or PLCDescription = ''"); if (d != null && d.Length > 0) { MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "PLC名、IP、端口、描述"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); this.btnSave.Enabled = false; return -1; } return 0; } #endregion } }