using OTSIncAReportApp.Controls; using OTSIncAReportApp.OTSMgrInfo; using OTSIncAReportApp.SysMgrTools; using System; using System.Collections; using System.Data; using System.IO; using System.Text.RegularExpressions; using System.Windows.Forms; namespace OTSIncAReportApp { public partial class frmPartSizeEditorNew : Form { #region 变量定义 /// /// 保存的全局主窗体对象 /// frmReportApp m_ReportApp; OTSDataMgrFunction.ResultDataMgr m_DataMgrFun = null; //变量 public DataTable m_dt_partsize = new DataTable(); //粒级 Hashtable table; string mPartSizeFilePath = ""; string str_selectID = "-1"; /// /// 选择使用的粒级名 /// public string PartSizeName { get; set; } #endregion #region 窗体加载及构造函数 public frmPartSizeEditorNew(frmReportApp infrmReportApp, string in_partsizefilepath) { InitializeComponent(); m_ReportApp = infrmReportApp; m_DataMgrFun = infrmReportApp.m_rstDataMgr; mPartSizeFilePath = in_partsizefilepath; m_dt_partsize.Columns.Add("ID"); m_dt_partsize.Columns.Add("FilePath"); m_dt_partsize.Columns.Add("FileName"); m_dt_partsize.Columns.Add("Name"); m_dt_partsize.Columns.Add("Value"); #region 国际化语言 Language lan = new Language(this); table = lan.GetNameTable(this.Name); #endregion } private void frmPartSizeEditorNew_Load(object sender, EventArgs e) { //设置GridView样式 SetGridViewStyle(); //绑定GridView BindGridView(); Init(); } void Init() { if (Gview_LJ.RowCount >= 0) { str_selectID = Gview_LJ.Rows[0].Cells[0].Value.ToString(); tb_lj.Text = Gview_LJ.Rows[0].Cells[1].Value.ToString();//路径 tb_ljm.Text = Gview_LJ.Rows[0].Cells[3].Value.ToString();//粒级名 tb_ljz.Text = Gview_LJ.Rows[0].Cells[4].Value.ToString();//粒级值 } } #endregion #region 自定义方法 /// /// 提供编号列名,获取DataTable中,编号列自增长后的ID标识 /// /// /// /// public int Get_MaxBH(DataTable dt, string col_name) { int rownumber = 0; if (dt.Rows.Count == 0) { rownumber = 0; return rownumber; } else { //取最大的并且小于10000的表自增长id,然后再加1 for (int i = 0; i < dt.Rows.Count; i++) { int ls_int = Convert.ToInt32(dt.Rows[i][col_name].ToString()); if (rownumber <= ls_int && 10000 > ls_int) { rownumber = ls_int + 1; } } } return rownumber; } /// /// 校验输入合法性 /// /// private bool Verify() { if (tb_ljm.Text == "") { MessageBox.Show("The entered particle size name is wrong, please re-enter", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); tb_ljm.Focus(); return false; } string tipstr = "The input of particle size value is wrong. Please enter the phase of particle size, separated by “,” in the middle, for example: \r\n “1,5,10,15,20,30,40,50” \r\nor can it contain decimal points, for example:\r\n “0.5,1.0,2.22,5.55,8.55,10.33,15,30,40” \r\n IF there is an error, please check whether the input format is correct, and whether the input methods of symbols “,” and “.” are in English, And whether half width format."; //粒级值判断 if (tb_ljz.Text == "") { MessageBox.Show(tipstr, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); tb_ljz.Focus(); return false; } //粒级值判断,判断是否能通过,纯数字和.及,号组合的正则表达式判断 string pat = @"^[-.,0-9]+$";//纯数字,和.及, Regex rg = new Regex(pat); if (false == rg.Match(tb_ljz.Text).Success) { MessageBox.Show(tipstr, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); tb_ljz.Focus(); return false; } //粒级值判断,对输入的值进行拆分,然后再对拆分出的各值判断 string[] spstr = tb_ljz.Text.Split(','); for (int i = 0; i < spstr.Length; i++) { string lsstr = spstr[i]; pat = @"^\d+(\.\d+)?$";//纯正浮点数数值,含0 rg = new Regex(pat); if (false == rg.Match(lsstr).Success) { MessageBox.Show(tipstr, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); tb_ljz.Focus(); return false; } } //最后转换,是否抱错 try { for (int i = 0; i < spstr.Length; i++) { double lsd = Convert.ToDouble(spstr[i].Trim()); } } catch { MessageBox.Show(tipstr, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); tb_ljz.Focus(); return false; } return true; } /// /// 绑定GridView粒级信息,从文件中读取信息进行绑定 /// private void BindGridView() { //遍历粒级文件夹 DirectoryInfo theFolder = new DirectoryInfo(mPartSizeFilePath); if (!theFolder.Exists) return; m_dt_partsize.Clear(); //读取遍历粒级文件信息 foreach (FileInfo nextifile in theFolder.GetFiles()) { //找出粒级文件 if (nextifile.Name.Contains(".psf") == true || nextifile.Name.Contains(".PSF") == true) { DataRow dr = m_dt_partsize.NewRow(); dr["ID"] = Get_MaxBH(m_dt_partsize, "ID"); dr["FilePath"] = nextifile.FullName; dr["FileName"] = nextifile.Name; //根据xml读取内容 XmlOperateUtil xmlutil = new XmlOperateUtil(nextifile.FullName); dr["Name"] = xmlutil.GetAttribute("Name", "XMLData"); dr["Value"] = xmlutil.GetAttribute("Sizes", "XMLData"); m_dt_partsize.Rows.Add(dr); } } //绑定显示到GridView中 Gview_LJ.Rows.Clear(); for (int i = 0; i < m_dt_partsize.Rows.Count; i++) { int index = Gview_LJ.Rows.Add(); Gview_LJ.Rows[index].Cells["ID"].Value = m_dt_partsize.Rows[i]["ID"].ToString(); Gview_LJ.Rows[index].Cells["FilePath"].Value = m_dt_partsize.Rows[i]["FilePath"].ToString(); Gview_LJ.Rows[index].Cells["FileName"].Value = m_dt_partsize.Rows[i]["FileName"].ToString(); Gview_LJ.Rows[index].Cells["ParticleSizeName"].Value = m_dt_partsize.Rows[i]["Name"].ToString(); Gview_LJ.Rows[index].Cells["ParticleSizeValue"].Value = m_dt_partsize.Rows[i]["Value"].ToString(); } } /// /// 设置GridView样式 /// private void SetGridViewStyle() { //无效? Gview_LJ.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; Gview_LJ.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; Gview_LJ.AllowUserToResizeColumns = false; Gview_LJ.AllowUserToResizeRows = false; //改变行的高度; Gview_LJ.RowTemplate.Height = 20; //改变标题的高度; Gview_LJ.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; Gview_LJ.ColumnHeadersHeight = 20; //禁用排序,无效?? for (int i = 0; i < this.Gview_LJ.Columns.Count; i++) { this.Gview_LJ.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } //点击选择整行 Gview_LJ.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //设置数据列 Gview_LJ.Columns.Add("ID", "ID"); Gview_LJ.Columns.Add("FilePath", "FilePath"); Gview_LJ.Columns.Add("FileName", "FileName"); Gview_LJ.Columns.Add("ParticleSizeName", "ParticleSizeName"); Gview_LJ.Columns.Add("ParticleSizeValue", "ParticleSizeValue"); //增加linkButton,删除按钮 string str5 = "delete"; DataGridViewLinkColumn dlink = new DataGridViewLinkColumn(); dlink.Text = str5;//添加的这列的显示文字,即每行最后一列显示的文字。 dlink.Name = "delLink"; dlink.HeaderText = str5;//列的标题 dlink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示 Gview_LJ.Columns.Add(dlink); //设置每列的宽度 Gview_LJ.Columns[0].Width = 0; Gview_LJ.Columns[0].Visible = false; Gview_LJ.Columns[1].Width = 0; Gview_LJ.Columns[1].Visible = false; Gview_LJ.Columns[2].Width = 100; Gview_LJ.Columns[3].Width = 150; Gview_LJ.Columns[4].Width = 250; Gview_LJ.Columns[5].Width = 60; //禁止排序 Gview_LJ.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable; Gview_LJ.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable; Gview_LJ.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable; Gview_LJ.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable; Gview_LJ.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable; Gview_LJ.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable; //居中显示 System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; Gview_LJ.DefaultCellStyle = dataGridViewCellStyle1; Gview_LJ.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //再次重覆禁用拖动表头高度,居然有效果了 Gview_LJ.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; } /// /// 重新初始化变量及控件的值 /// private void ReControlsValue() { str_selectID = "-1"; tb_lj.Text = ""; tb_ljm.Text = ""; tb_ljz.Text = ""; } #endregion #region 控件事件 private void Gview_LJ_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { str_selectID = Gview_LJ.Rows[e.RowIndex].Cells[0].Value.ToString(); tb_lj.Text = Gview_LJ.Rows[e.RowIndex].Cells[1].Value.ToString();//路径 tb_ljm.Text = Gview_LJ.Rows[e.RowIndex].Cells[3].Value.ToString();//粒级名 tb_ljz.Text = Gview_LJ.Rows[e.RowIndex].Cells[4].Value.ToString();//粒级值 if (Gview_LJ.Columns[e.ColumnIndex].Name == "delLink") { if (MessageBox.Show("Are you sure to delete the granularity file?", "Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { //删除,删除文件 File.Delete(tb_lj.Text); MessageBox.Show("Delete succeeded!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); //重新加载绑定GridView BindGridView(); //为全局选择变量进行重新赋值 ReControlsValue(); } } } } private void button1_Click(object sender, EventArgs e) { //保存,同时保存到文件 if (str_selectID == "-1") { MessageBox.Show("Please select and edit before saving!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //校验输入合法性 if (Verify() == false) { return; } XmlOperateUtil xmlutil = new XmlOperateUtil(tb_lj.Text); xmlutil.SetAttribute("Name", tb_ljm.Text.Trim(), "XMLData"); xmlutil.SetAttribute("Sizes", tb_ljz.Text.Trim(), "XMLData"); MessageBox.Show("Saved successfully!", "Tips", MessageBoxButtons.OK, MessageBoxIcon.Information); //再重新绑定显示 BindGridView(); //为全局选择变量进行重新赋值 ReControlsValue(); } private void button4_Click(object sender, EventArgs e) { //校验输入合法性 if (Verify() == false) { return; } //另存,新建文件 SaveFileDialog savePath = new SaveFileDialog(); savePath.RestoreDirectory = true; savePath.InitialDirectory = mPartSizeFilePath; savePath.Title = "Save file"; savePath.Filter = "psf File(*.psf)|*.psf"; savePath.RestoreDirectory = false; if (savePath.ShowDialog() == DialogResult.OK) { FileStream file = new FileStream(savePath.FileName, FileMode.CreateNew); byte[] data = System.Text.Encoding.UTF8.GetBytes(" \r\n "); file.Write(data, 0, data.Length); file.Flush(); file.Close(); //再重新绑定显示 BindGridView(); //为全局选择变量进行重新赋值 ReControlsValue(); } } private void button2_Click(object sender, EventArgs e) { //先不切换粒级,直接返回,相当于确定无功能 PartSizeName = tb_ljm.Text.Trim(); this.DialogResult = DialogResult.OK; this.Close(); } private void button3_Click(object sender, EventArgs e) { //返回,取消 this.Close(); } #endregion private void newFileToolStripMenuItem_Click(object sender, EventArgs e) { FormNewLJFile newLJFile = new FormNewLJFile(m_dt_partsize); newLJFile.ShowDialog(); //再重新绑定显示 BindGridView(); //为全局选择变量进行重新赋值 ReControlsValue(); } } }