|
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ICSSoft.Base.ReferForm.AppReferForm; using ICSSoft.Base.Config.DBHelper; using ICSSoft.Base.Config.AppConfig; using System.Xml; using System.IO; using System.Reflection; using DevExpress.XtraEditors; using System.Configuration;
namespace ICSSoft.Frame.WatchPanel { public partial class WatchSet : Form { public WatchSet() { InitializeComponent(); this.Width = 700; this.Height = 533; ControlStatus(false); }
#region 退出
private void StripMenuExit_Click(object sender, EventArgs e) { this.Close(); }
private void btnExit_Click(object sender, EventArgs e) { this.Close(); } #endregion
private void StripMenu_Click(object sender, EventArgs e) { comName.Text = ""; ControlStatus(true); comName.DropDownStyle = ComboBoxStyle.DropDown; }
private void StripMenuAlter_Click(object sender, EventArgs e) { comName.Text = ""; ControlStatus(true); comName.DropDownStyle = ComboBoxStyle.DropDownList; }
#region 保存和修改
private void btnSave_Click(object sender, EventArgs e) { if (BoolNull() == false) { MessageBox.Show("数值不能为空!"); return; } String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String strFullPathToMyFile = Path.Combine(strAppDir, "WatchConfig.xml"); XmlDocument xmlDoc = loadXml(strFullPathToMyFile); int m = 0; XmlNodeList xnls = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig").ChildNodes; //XML中排序节点的集合
for (int i = 0; i < xnls.Count; i++) //遍历节点集合
{ XmlElement xe = (XmlElement)xnls.Item(i); //获取相应节点的元素
if (xe.GetAttribute("Name") == comName.Text) { m++; } } //增加
if (m == 0) { XmlNode root = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig");//查找<FacWatchConfig>
XmlElement xe1 = xmlDoc.CreateElement("FacWatchConfig");//创建一个<FacWatchConfig>节点
xe1.SetAttribute("Name", comName.Text);//设置该节点属性
xe1.SetAttribute("CarStatus", cheCarStatus.Checked.ToString()); xe1.SetAttribute("CarWatchStatus", cheCarWatchStatus.Checked.ToString()); xe1.SetAttribute("CarPro", cheCarPro.Checked.ToString()); xe1.SetAttribute("SSStatus", cheSSStatus.Checked.ToString()); xe1.SetAttribute("AutoBetTime", txtAutoBetTime.Text); xe1.SetAttribute("ChangeTime", txtChangeTime.Text); xe1.SetAttribute("SS", btnSS.Text); xe1.SetAttribute("CarSS", btnCarSS.Text); xe1.SetAttribute("EquipID", btnEquipID.Text); xe1.SetAttribute("RightArea", cheRight.Checked.ToString()); root.AppendChild(xe1);//添加到<FacWatchConfig>节点中
saveXml(xmlDoc, strFullPathToMyFile); //最后必须要保存到XML文件中
} else { XmlNode root = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig"); XmlNodeList xnl = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig").ChildNodes; for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); if (xe.GetAttribute("Name") == comName.Text) { if (cheCarStatus.Checked.ToString() != xe.GetAttribute("CarStatus")) { xe.SetAttribute("CarStatus", cheCarStatus.Checked.ToString()); } if (cheCarWatchStatus.Checked.ToString() != xe.GetAttribute("CarWatchStatus")) { xe.SetAttribute("CarWatchStatus", cheCarWatchStatus.Checked.ToString()); } if (cheCarPro.Checked.ToString() != xe.GetAttribute("CarPro")) { xe.SetAttribute("CarPro", cheCarPro.Checked.ToString()); } if (cheSSStatus.Checked.ToString() != xe.GetAttribute("SSStatus")) { xe.SetAttribute("SSStatus", cheSSStatus.Checked.ToString()); } if (txtAutoBetTime.Text != xe.GetAttribute("AutoBetTime")) { xe.SetAttribute("AutoBetTime", txtAutoBetTime.Text); } if (txtChangeTime.Text != xe.GetAttribute("ChangeTime")) { xe.SetAttribute("ChangeTime", txtChangeTime.Text); } if (btnSS.Text != xe.GetAttribute("SS")) { xe.SetAttribute("SS", btnSS.Text); } if (btnCarSS.Text != xe.GetAttribute("CarSS")) { xe.SetAttribute("CarSS", btnCarSS.Text); } if (btnEquipID.Text != xe.GetAttribute("EquipID")) { xe.SetAttribute("EquipID", btnEquipID.Text); } //如果不相同则修改属性
} } saveXml(xmlDoc, strFullPathToMyFile); } MessageBox.Show("Success!"); resert(); } #endregion
#region 加载XML数据方法
private XmlDocument loadXml(string filePath) { FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); XmlDocument doc = new XmlDocument(); doc.Load(stream); stream.Close(); return doc; } #endregion
#region 保存XML数据方法
private void saveXml(XmlDocument doc, string filePath) { FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write); doc.Save(stream); stream.Close(); } #endregion
#region 名称下拉框数据加载
private void comName_Click(object sender, EventArgs e) { comName.Items.Clear(); String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String strFullPathToMyFile = Path.Combine(strAppDir, "WatchConfig.xml"); XmlDocument xmlDoc = loadXml(strFullPathToMyFile); XmlNode root = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig"); XmlNodeList xnl = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig").ChildNodes; for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); comName.Items.Add(xe.GetAttribute("Name")); } } #endregion
#region 下拉框选择后加载数据
private void comName_SelectedIndexChanged(object sender, EventArgs e) { String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String strFullPathToMyFile = Path.Combine(strAppDir, "WatchConfig.xml"); XmlDocument xmlDoc = loadXml(strFullPathToMyFile); XmlNode root = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig"); XmlNodeList xnl = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig").ChildNodes; for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); if (xe.GetAttribute("Name") == comName.Text) { cheCarStatus.Checked = xe.GetAttribute("CarStatus") == "True" ? true : false; cheCarWatchStatus.Checked = xe.GetAttribute("CarWatchStatus") == "True" ? true : false; cheCarPro.Checked = xe.GetAttribute("CarPro") == "True" ? true : false; cheSSStatus.Checked = xe.GetAttribute("SSStatus") == "True" ? true : false; txtAutoBetTime.Text = xe.GetAttribute("AutoBetTime"); txtChangeTime.Text = xe.GetAttribute("ChangeTime"); btnSS.Text = xe.GetAttribute("SS"); btnCarSS.Text = xe.GetAttribute("CarSS"); btnEquipID.Text = xe.GetAttribute("EquipID"); cheRight.Checked = xe.GetAttribute("RightArea") == "True" ? true : false; } }
} #endregion
#region 判断控件值是否为空
private bool BoolNull() { if (comName.Text == null) { return false; } if (txtAutoBetTime.Text == null) { return false; } if (txtChangeTime.Text == null) { return false; } if (btnSS.Text == null) { return false; } if (btnCarSS.Text == null) { return false; } if (btnEquipID.Text == null) { return false; } return true; } #endregion
#region 包含的产线 buttonclick事件
private void btnSS_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { btnSS.Text = ""; ButtonEdit btn = (ButtonEdit)sender; string sql = @"select SSCODE as 产线代码,SSTYPE as 产线类型,SSDESC as 产线描述 from dbo.ICSSS"; sql = string.Format(sql); DataTable data = DBHelper.ExecuteDataset(GetConnectionStringsConfig(), CommandType.Text, sql).Tables[0]; FormDataRefer reForm = new FormDataRefer(); reForm.FormTitle = "产线"; DataTable menuData = data; reForm.DataSource = menuData; reForm.MSelectFlag = true; reForm.RowIndexWidth = 35; //reForm.HideCols.Add("ID");
reForm.FormWidth = 800; reForm.FormHeight = 600; reForm.FilterKey = btn.Text; if (reForm.ShowDialog() == DialogResult.OK) { DataTable retData = reForm.ReturnData; int count = retData.Rows.Count; foreach (DataRow dr in retData.Rows) { btnSS.Text += "," + dr["产线代码"].ToString(); } } if (btnSS.Text.StartsWith(",")) { btnSS.Text = btnSS.Text.Substring(1, btnSS.Text.Length-1); } } #endregion
#region 车间包含的产线 buttonclick事件
private void btnCarSS_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { btnCarSS.Text = ""; ButtonEdit btn = (ButtonEdit)sender; string sql = @"select SSCODE as 产线代码,SSTYPE as 产线类型,SSDESC as 产线描述 from dbo.ICSSS"; sql = string.Format(sql); DataTable data = DBHelper.ExecuteDataset(GetConnectionStringsConfig(), CommandType.Text, sql).Tables[0]; FormDataRefer reForm = new FormDataRefer(); reForm.FormTitle = "产线"; DataTable menuData = data; reForm.DataSource = menuData; reForm.MSelectFlag = true; reForm.RowIndexWidth = 35; //reForm.HideCols.Add("ID");
reForm.FormWidth = 800; reForm.FormHeight = 600; reForm.FilterKey = btn.Text; if (reForm.ShowDialog() == DialogResult.OK) { DataTable retData = reForm.ReturnData; int count = retData.Rows.Count; foreach (DataRow dr in retData.Rows) { btnCarSS.Text += ","+dr["产线代码"].ToString(); } if (btnCarSS.Text.StartsWith(",")) { btnCarSS.Text = btnCarSS.Text.Substring(1, btnCarSS.Text.Length - 1); } } } #endregion
#region 设备ID buttonclick事件
private void btnEquipID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { btnEquipID.Text = ""; ButtonEdit btn = (ButtonEdit)sender; string sql = @"select EQPID as 设备ID,EQPCode as 设备编号,EQPName as 设备名称,EQPStatus as 设备状态,EQPDESC as 设备描述 from dbo.ICSEquipment"; sql = string.Format(sql); DataTable data = DBHelper.ExecuteDataset(GetConnectionStringsConfig(), CommandType.Text, sql).Tables[0]; FormDataRefer reForm = new FormDataRefer(); reForm.FormTitle = "设备"; DataTable menuData = data; reForm.DataSource = menuData; reForm.MSelectFlag = true; reForm.HideCols.Add("设备ID"); reForm.RowIndexWidth = 35; //reForm.HideCols.Add("ID");
reForm.FormWidth = 800; reForm.FormHeight = 600; reForm.FilterKey = btn.Text; if (reForm.ShowDialog() == DialogResult.OK) { DataTable retData = reForm.ReturnData; int count = retData.Rows.Count; foreach (DataRow dr in retData.Rows) { btnEquipID.Text += "," + dr["设备编号"].ToString(); } } if (btnEquipID.Text.StartsWith(",")) { btnEquipID.Text = btnEquipID.Text.Substring(1, btnEquipID.Text.Length - 1); } } #endregion
#region 获取数据库链接字符串
private string GetConnectionStringsConfig() { string connectionstring = ""; String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String strFullPathToMyFile = Path.Combine(strAppDir, "appwatch.xml"); XmlDocument xmlDoc = loadXml(strFullPathToMyFile); XmlNodeList xnl = xmlDoc.SelectSingleNode("configuration").ChildNodes; for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); connectionstring = xe.GetAttribute("value"); } return connectionstring; } #endregion
#region 删除
private void btnDel_Click(object sender, EventArgs e) { if (comName.Text == "") { MessageBox.Show("名称不可为空!"); return; } if (MessageBox.Show("确定要删除该条数据吗?", "删除前确认", MessageBoxButtons.YesNo) == DialogResult.Yes) { String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String strFullPathToMyFile = Path.Combine(strAppDir, "WatchConfig.xml"); XmlDocument xmlDoc = loadXml(strFullPathToMyFile); XmlNode root = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig"); XmlNodeList xnl = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig").ChildNodes; for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); if (xe.GetAttribute("Name") == comName.Text) { root.RemoveChild(xe); } } saveXml(xmlDoc, strFullPathToMyFile); resert(); } } #endregion
private void ControlStatus(bool bol) { comName.Visible = bol; txtAutoBetTime.ReadOnly = !bol; txtChangeTime.ReadOnly = !bol; btnSS.Properties.ReadOnly = !bol; btnCarSS.Properties.ReadOnly = !bol; btnEquipID.Properties.ReadOnly = !bol; cheCarStatus.Visible = bol; cheCarWatchStatus.Visible = bol; cheCarPro.Visible = bol; cheSSStatus.Visible = bol; cheRight.Visible = bol; } private void resert() { comName.Text = ""; txtAutoBetTime.Text = ""; txtChangeTime.Text = ""; btnSS.Text = ""; btnCarSS.Text = ""; btnEquipID.Text = ""; cheCarStatus.Checked = false; cheCarWatchStatus.Checked = false; cheCarPro.Checked = false; cheSSStatus.Checked = false; cheRight.Checked = false; }
private void comName_TextChanged(object sender, EventArgs e) { }
private void btnOutPut_Click(object sender, EventArgs e) {
#region XML
String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String strFullPathToMyFile = Path.Combine(strAppDir, "WatchConfig.xml"); XmlDocument xmlDoc = loadXml(strFullPathToMyFile); int m = 0; XmlNodeList xnls = xmlDoc.SelectSingleNode("WatchConfig/FacWatchConfig").ChildNodes; //XML中排序节点的集合
for (int i = 0; i < xnls.Count; i++) //遍历节点集合
{ XmlElement xe = (XmlElement)xnls.Item(i); //获取相应节点的元素
if (xe.GetAttribute("Name") == comName.Text) { m++; } } #endregion
if( comName.Text=="" ) { MessageBox.Show("名称不能为空!"); return; } if (m == 0) { MessageBox.Show("该名称不存在!"); return; } if(button1.BackColor!=Color.Blue&&button2.BackColor!=Color.Blue) { MessageBox.Show("车间产线必须选中一个!"); return; } if (button1.BackColor == Color.Blue && button2.BackColor == Color.Blue) { MessageBox.Show("车间产线不能两个都选中!"); return; } WatchData data = new WatchData(); for (int i = 0; i < xnls.Count; i++) { XmlElement xe = (XmlElement)xnls.Item(i); if (xe.GetAttribute("Name") == comName.Text) { data.Name=comName.Text; data.CarStatus= xe.GetAttribute("CarStatus")=="True"?true:false; data.CarWatchStatus = xe.GetAttribute("CarWatchStatus") == "True" ? true : false;
data.CarPro = xe.GetAttribute("CarPro") == "True" ? true : false;
data.SSStatus = xe.GetAttribute("SSStatus") == "True" ? true : false; data.AutoBetTime= xe.GetAttribute("AutoBetTime"); data.ChangeTime= xe.GetAttribute("ChangeTime"); data.SS = xe.GetAttribute("SS"); data.CarSS = xe.GetAttribute("CarSS"); data.EquipID = xe.GetAttribute("EquipID");
data.Right = xe.GetAttribute("RightArea") == "True" ? true : false; //如果不相同则修改属性
} } if(button1.BackColor==Color.Blue) { FormWatch2 form1 = new FormWatch2(); form1.ShowDialog(); } if (button2.BackColor == Color.Blue) { FormWatch1 form1 = new FormWatch1(data); form1.ShowDialog(); } }
private void WatchSet_Load(object sender, EventArgs e) {
}
private void button1_Click(object sender, EventArgs e) { if (button1.BackColor == Color.White) { button1.BackColor = Color.Blue; button2.BackColor = Color.White; } else if (button1.BackColor == Color.Blue) { button1.BackColor = Color.White; } }
private void button2_Click(object sender, EventArgs e) { if (button2.BackColor == Color.White) { button2.BackColor = Color.Blue; button1.BackColor = Color.White; } else if (button2.BackColor == Color.Blue) { button2.BackColor = Color.White; } }
} }
|