frmPartSizeEditorNew.cs 16 KB

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