frmPartSizeEditorNew.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using OTSIncAReportApp.Controls;
  2. using OTSIncAReportApp.OTSMgrInfo;
  3. using System;
  4. using System.Data;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7. using System.Windows.Forms;
  8. namespace OTSIncAReportApp
  9. {
  10. public partial class frmPartSizeEditorNew : Form
  11. {
  12. #region 变量定义
  13. /// <summary>
  14. /// 保存的全局主窗体对象
  15. /// </summary>
  16. frmReportApp m_ReportApp;
  17. OTSDataMgrFunction.DataMgrFun m_DataMgrFun = null;
  18. //变量
  19. public DataTable m_dt_partsize = new DataTable(); //粒级
  20. string mPartSizeFilePath = "";
  21. string str_selectID = "-1";
  22. /// <summary>
  23. /// 选择使用的粒级名
  24. /// </summary>
  25. public string PartSizeName
  26. {
  27. get;
  28. set;
  29. }
  30. #endregion
  31. #region 窗体加载及构造函数
  32. public frmPartSizeEditorNew(frmReportApp infrmReportApp, string in_partsizefilepath)
  33. {
  34. InitializeComponent();
  35. m_ReportApp = infrmReportApp;
  36. m_DataMgrFun = infrmReportApp.m_DataMgrFun;
  37. mPartSizeFilePath = in_partsizefilepath;
  38. m_dt_partsize.Columns.Add("ID");
  39. m_dt_partsize.Columns.Add("FilePath");
  40. m_dt_partsize.Columns.Add("FileName");
  41. m_dt_partsize.Columns.Add("Name");
  42. m_dt_partsize.Columns.Add("Value");
  43. }
  44. private void frmPartSizeEditorNew_Load(object sender, EventArgs e)
  45. {
  46. //设置GridView样式
  47. SetGridViewStyle();
  48. //绑定GridView
  49. BindGridView();
  50. Init();
  51. }
  52. void Init()
  53. {
  54. if (Gview_LJ.RowCount >= 0)
  55. {
  56. str_selectID = Gview_LJ.Rows[0].Cells[0].Value.ToString();
  57. tb_lj.Text = Gview_LJ.Rows[0].Cells[1].Value.ToString();//路径
  58. tb_ljm.Text = Gview_LJ.Rows[0].Cells[3].Value.ToString();//粒级名
  59. tb_ljz.Text = Gview_LJ.Rows[0].Cells[4].Value.ToString();//粒级值
  60. }
  61. }
  62. #endregion
  63. #region 自定义方法
  64. /// <summary>
  65. /// 提供编号列名,获取DataTable中,编号列自增长后的ID标识
  66. /// </summary>
  67. /// <param name="dt"></param>
  68. /// <param name="col_name"></param>
  69. /// <returns></returns>
  70. public int Get_MaxBH(DataTable dt, string col_name)
  71. {
  72. int rownumber = 0;
  73. if (dt.Rows.Count == 0)
  74. {
  75. rownumber = 0;
  76. return rownumber;
  77. }
  78. else
  79. {
  80. //取最大的并且小于10000的表自增长id,然后再加1
  81. for (int i = 0; i < dt.Rows.Count; i++)
  82. {
  83. int ls_int = Convert.ToInt32(dt.Rows[i][col_name].ToString());
  84. if (rownumber <= ls_int && 10000 > ls_int)
  85. {
  86. rownumber = ls_int + 1;
  87. }
  88. }
  89. }
  90. return rownumber;
  91. }
  92. /// <summary>
  93. /// 校验输入合法性
  94. /// </summary>
  95. /// <returns></returns>
  96. private bool Verify()
  97. {
  98. if (tb_ljm.Text == "")
  99. {
  100. MessageBox.Show("输入的粒级名错误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  101. tb_ljm.Focus();
  102. return false;
  103. }
  104. 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符号“,”与“.”的输入法是否为英文,及是否半角格式。";
  105. //粒级值判断
  106. if (tb_ljz.Text == "")
  107. {
  108. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  109. tb_ljz.Focus();
  110. return false;
  111. }
  112. //粒级值判断,判断是否能通过,纯数字和.及,号组合的正则表达式判断
  113. string pat = @"^[-.,0-9]+$";//纯数字,和.及,
  114. Regex rg = new Regex(pat);
  115. if (false == rg.Match(tb_ljz.Text).Success)
  116. {
  117. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  118. tb_ljz.Focus();
  119. return false;
  120. }
  121. //粒级值判断,对输入的值进行拆分,然后再对拆分出的各值判断
  122. string[] spstr = tb_ljz.Text.Split(',');
  123. for (int i = 0; i < spstr.Length; i++)
  124. {
  125. string lsstr = spstr[i];
  126. pat = @"^\d+(\.\d+)?$";//纯正浮点数数值,含0
  127. rg = new Regex(pat);
  128. if (false == rg.Match(lsstr).Success)
  129. {
  130. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  131. tb_ljz.Focus();
  132. return false;
  133. }
  134. }
  135. //最后转换,是否抱错
  136. try
  137. {
  138. for (int i = 0; i < spstr.Length; i++)
  139. {
  140. double lsd = Convert.ToDouble(spstr[i].Trim());
  141. }
  142. }
  143. catch
  144. {
  145. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  146. tb_ljz.Focus();
  147. return false;
  148. }
  149. return true;
  150. }
  151. /// <summary>
  152. /// 绑定GridView粒级信息,从文件中读取信息进行绑定
  153. /// </summary>
  154. private void BindGridView()
  155. {
  156. //遍历粒级文件夹
  157. DirectoryInfo theFolder = new DirectoryInfo(mPartSizeFilePath);
  158. if (!theFolder.Exists)
  159. return;
  160. m_dt_partsize.Clear();
  161. //读取遍历粒级文件信息
  162. foreach (FileInfo nextifile in theFolder.GetFiles())
  163. {
  164. //找出粒级文件
  165. if (nextifile.Name.Contains(".psf") == true || nextifile.Name.Contains(".PSF") == true)
  166. {
  167. DataRow dr = m_dt_partsize.NewRow();
  168. dr["ID"] = Get_MaxBH(m_dt_partsize, "ID");
  169. dr["FilePath"] = nextifile.FullName;
  170. dr["FileName"] = nextifile.Name;
  171. //根据xml读取内容
  172. XmlConfigUtil xmlutil = new XmlConfigUtil(nextifile.FullName);
  173. dr["Name"] = xmlutil.GetAttribute("Name", "XMLData");
  174. dr["Value"] = xmlutil.GetAttribute("Sizes", "XMLData");
  175. m_dt_partsize.Rows.Add(dr);
  176. }
  177. }
  178. //绑定显示到GridView中
  179. Gview_LJ.Rows.Clear();
  180. for (int i = 0; i < m_dt_partsize.Rows.Count; i++)
  181. {
  182. int index = Gview_LJ.Rows.Add();
  183. Gview_LJ.Rows[index].Cells["ID"].Value = m_dt_partsize.Rows[i]["ID"].ToString();
  184. Gview_LJ.Rows[index].Cells["文件路径"].Value = m_dt_partsize.Rows[i]["FilePath"].ToString();
  185. Gview_LJ.Rows[index].Cells["文件名"].Value = m_dt_partsize.Rows[i]["FileName"].ToString();
  186. Gview_LJ.Rows[index].Cells["粒级名"].Value = m_dt_partsize.Rows[i]["Name"].ToString();
  187. Gview_LJ.Rows[index].Cells["粒级值"].Value = m_dt_partsize.Rows[i]["Value"].ToString();
  188. }
  189. }
  190. /// <summary>
  191. /// 设置GridView样式
  192. /// </summary>
  193. private void SetGridViewStyle()
  194. {
  195. //无效?
  196. Gview_LJ.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  197. Gview_LJ.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  198. Gview_LJ.AllowUserToResizeColumns = false;
  199. Gview_LJ.AllowUserToResizeRows = false;
  200. //改变行的高度;
  201. Gview_LJ.RowTemplate.Height = 20;
  202. //改变标题的高度;
  203. Gview_LJ.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  204. Gview_LJ.ColumnHeadersHeight = 20;
  205. //禁用排序,无效??
  206. for (int i = 0; i < this.Gview_LJ.Columns.Count; i++)
  207. {
  208. this.Gview_LJ.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  209. }
  210. //点击选择整行
  211. Gview_LJ.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  212. //设置数据列
  213. Gview_LJ.Columns.Add("ID", "ID");
  214. Gview_LJ.Columns.Add("文件路径", "文件路径");
  215. Gview_LJ.Columns.Add("文件名", "文件名");
  216. Gview_LJ.Columns.Add("粒级名", "粒级名");
  217. Gview_LJ.Columns.Add("粒级值", "粒级值");
  218. //增加linkButton,删除按钮
  219. string str5 = "删除";
  220. DataGridViewLinkColumn dlink = new DataGridViewLinkColumn();
  221. dlink.Text = str5;//添加的这列的显示文字,即每行最后一列显示的文字。
  222. dlink.Name = "delLink";
  223. dlink.HeaderText = str5;//列的标题
  224. dlink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
  225. Gview_LJ.Columns.Add(dlink);
  226. //设置每列的宽度
  227. Gview_LJ.Columns[0].Width = 0;
  228. Gview_LJ.Columns[0].Visible = false;
  229. Gview_LJ.Columns[1].Width = 0;
  230. Gview_LJ.Columns[1].Visible = false;
  231. Gview_LJ.Columns[2].Width = 100;
  232. Gview_LJ.Columns[3].Width = 100;
  233. Gview_LJ.Columns[4].Width = 200;
  234. Gview_LJ.Columns[5].Width = 35;
  235. //禁止排序
  236. Gview_LJ.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
  237. Gview_LJ.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
  238. Gview_LJ.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
  239. Gview_LJ.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
  240. Gview_LJ.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
  241. Gview_LJ.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;
  242. //居中显示
  243. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  244. dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
  245. Gview_LJ.DefaultCellStyle = dataGridViewCellStyle1;
  246. Gview_LJ.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  247. //再次重覆禁用拖动表头高度,居然有效果了
  248. Gview_LJ.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  249. }
  250. /// <summary>
  251. /// 重新初始化变量及控件的值
  252. /// </summary>
  253. private void ReControlsValue()
  254. {
  255. str_selectID = "-1";
  256. tb_lj.Text = "";
  257. tb_ljm.Text = "";
  258. tb_ljz.Text = "";
  259. }
  260. #endregion
  261. #region 控件事件
  262. private void Gview_LJ_CellClick(object sender, DataGridViewCellEventArgs e)
  263. {
  264. if (e.RowIndex >= 0)
  265. {
  266. str_selectID = Gview_LJ.Rows[e.RowIndex].Cells[0].Value.ToString();
  267. tb_lj.Text = Gview_LJ.Rows[e.RowIndex].Cells[1].Value.ToString();//路径
  268. tb_ljm.Text = Gview_LJ.Rows[e.RowIndex].Cells[3].Value.ToString();//粒级名
  269. tb_ljz.Text = Gview_LJ.Rows[e.RowIndex].Cells[4].Value.ToString();//粒级值
  270. if (Gview_LJ.Columns[e.ColumnIndex].Name == "delLink")
  271. {
  272. if (MessageBox.Show("确定是否删除该粒级文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  273. {
  274. //删除,删除文件
  275. File.Delete(tb_lj.Text);
  276. MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  277. //重新加载绑定GridView
  278. BindGridView();
  279. //为全局选择变量进行重新赋值
  280. ReControlsValue();
  281. }
  282. }
  283. }
  284. }
  285. private void button1_Click(object sender, EventArgs e)
  286. {
  287. //保存,同时保存到文件
  288. if (str_selectID == "-1")
  289. {
  290. MessageBox.Show("请选择并编辑后再进行保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  291. return;
  292. }
  293. //校验输入合法性
  294. if (Verify() == false)
  295. {
  296. return;
  297. }
  298. XmlConfigUtil xmlutil = new XmlConfigUtil(tb_lj.Text);
  299. xmlutil.SetAttribute("Name", tb_ljm.Text.Trim(), "XMLData");
  300. xmlutil.SetAttribute("Sizes", tb_ljz.Text.Trim(), "XMLData");
  301. MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  302. //再重新绑定显示
  303. BindGridView();
  304. //为全局选择变量进行重新赋值
  305. ReControlsValue();
  306. }
  307. private void button4_Click(object sender, EventArgs e)
  308. {
  309. //校验输入合法性
  310. if (Verify() == false)
  311. {
  312. return;
  313. }
  314. //另存,新建文件
  315. SaveFileDialog savePath = new SaveFileDialog();
  316. savePath.RestoreDirectory = true;
  317. savePath.InitialDirectory = mPartSizeFilePath;
  318. savePath.Title = "保存文件";
  319. savePath.Filter = "psf文件(*.psf)|*.psf";
  320. savePath.RestoreDirectory = false;
  321. if (savePath.ShowDialog() == DialogResult.OK)
  322. {
  323. FileStream file = new FileStream(savePath.FileName, FileMode.CreateNew);
  324. 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\" /> ");
  325. file.Write(data, 0, data.Length);
  326. file.Flush();
  327. file.Close();
  328. //再重新绑定显示
  329. BindGridView();
  330. //为全局选择变量进行重新赋值
  331. ReControlsValue();
  332. }
  333. }
  334. private void button2_Click(object sender, EventArgs e)
  335. {
  336. //先不切换粒级,直接返回,相当于确定无功能
  337. PartSizeName = tb_ljm.Text.Trim();
  338. this.DialogResult = DialogResult.OK;
  339. this.Close();
  340. }
  341. private void button3_Click(object sender, EventArgs e)
  342. {
  343. //返回,取消
  344. this.Close();
  345. }
  346. #endregion
  347. private void newFileToolStripMenuItem_Click(object sender, EventArgs e)
  348. {
  349. FormNewLJFile newLJFile = new FormNewLJFile(m_dt_partsize);
  350. newLJFile.ShowDialog();
  351. //再重新绑定显示
  352. BindGridView();
  353. //为全局选择变量进行重新赋值
  354. ReControlsValue();
  355. }
  356. }
  357. }