123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- using OTSIncAReportApp.Controls;
- using OTSIncAReportApp.OTSMgrInfo;
- using System;
- using System.Data;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- namespace OTSIncAReportApp
- {
- public partial class frmPartSizeEditorNew : Form
- {
- #region 变量定义
- /// <summary>
- /// 保存的全局主窗体对象
- /// </summary>
- frmReportApp m_ReportApp;
- OTSDataMgrFunction.DataMgrFun m_DataMgrFun = null;
- //变量
- public DataTable m_dt_partsize = new DataTable(); //粒级
- string mPartSizeFilePath = "";
- string str_selectID = "-1";
- /// <summary>
- /// 选择使用的粒级名
- /// </summary>
- public string PartSizeName
- {
- get;
- set;
- }
- #endregion
- #region 窗体加载及构造函数
- public frmPartSizeEditorNew(frmReportApp infrmReportApp, string in_partsizefilepath)
- {
- InitializeComponent();
- m_ReportApp = infrmReportApp;
- m_DataMgrFun = infrmReportApp.m_DataMgrFun;
- 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");
- }
- 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 自定义方法
- /// <summary>
- /// 提供编号列名,获取DataTable中,编号列自增长后的ID标识
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="col_name"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 校验输入合法性
- /// </summary>
- /// <returns></returns>
- private bool Verify()
- {
- if (tb_ljm.Text == "")
- {
- MessageBox.Show("输入的粒级名错误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- tb_ljm.Focus();
- return false;
- }
- string tipstr = "粒级值输入错误,请输入粒级的阶段,中间使用“,”号进行分隔,例如:\r\n “1,5,10,15,20,30,40,50” \r\n或可带有小数,例如: \r\n “0.5,1.0,2.22,5.55,8.55,10.33,15,30,40” \r\n如果出现错误,请详细检查输入的格式是否正确,\r\n符号“,”与“.”的输入法是否为英文,及是否半角格式。";
- //粒级值判断
- if (tb_ljz.Text == "")
- {
- MessageBox.Show(tipstr, "提示", 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, "提示", 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, "提示", 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, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- tb_ljz.Focus();
- return false;
- }
- return true;
- }
- /// <summary>
- /// 绑定GridView粒级信息,从文件中读取信息进行绑定
- /// </summary>
- 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读取内容
- XmlConfigUtil xmlutil = new XmlConfigUtil(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["文件路径"].Value = m_dt_partsize.Rows[i]["FilePath"].ToString();
- Gview_LJ.Rows[index].Cells["文件名"].Value = m_dt_partsize.Rows[i]["FileName"].ToString();
- Gview_LJ.Rows[index].Cells["粒级名"].Value = m_dt_partsize.Rows[i]["Name"].ToString();
- Gview_LJ.Rows[index].Cells["粒级值"].Value = m_dt_partsize.Rows[i]["Value"].ToString();
- }
- }
- /// <summary>
- /// 设置GridView样式
- /// </summary>
- 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("文件路径", "文件路径");
- Gview_LJ.Columns.Add("文件名", "文件名");
- Gview_LJ.Columns.Add("粒级名", "粒级名");
- Gview_LJ.Columns.Add("粒级值", "粒级值");
- //增加linkButton,删除按钮
- string str5 = "删除";
- 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 = 100;
- Gview_LJ.Columns[4].Width = 200;
- Gview_LJ.Columns[5].Width = 35;
- //禁止排序
- 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;
- }
- /// <summary>
- /// 重新初始化变量及控件的值
- /// </summary>
- 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("确定是否删除该粒级文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- //删除,删除文件
- File.Delete(tb_lj.Text);
- MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- //重新加载绑定GridView
- BindGridView();
- //为全局选择变量进行重新赋值
- ReControlsValue();
- }
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //保存,同时保存到文件
- if (str_selectID == "-1")
- {
- MessageBox.Show("请选择并编辑后再进行保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- //校验输入合法性
- if (Verify() == false)
- {
- return;
- }
- XmlConfigUtil xmlutil = new XmlConfigUtil(tb_lj.Text);
- xmlutil.SetAttribute("Name", tb_ljm.Text.Trim(), "XMLData");
- xmlutil.SetAttribute("Sizes", tb_ljz.Text.Trim(), "XMLData");
- MessageBox.Show("保存成功!", "提示", 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 = "保存文件";
- savePath.Filter = "psf文件(*.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("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n<XMLData FileMark=\"626\" Name=\"" + tb_ljm.Text.Trim() + "\" Sizes=\"" + tb_ljz.Text.Trim() + "\" Version=\"1.1.1\" /> ");
- 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();
- }
- }
- }
|