frmPartSizeEditorNew.cs 16 KB

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