FormNewLJFile.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Xml;
  13. namespace OTSIncAReportApp.OTSMgrInfo
  14. {
  15. public partial class FormNewLJFile : Form
  16. {
  17. private DataTable partsize = new DataTable();
  18. public FormNewLJFile(DataTable a_dataTable)
  19. {
  20. partsize = a_dataTable;
  21. InitializeComponent();
  22. }
  23. private bool Verify()
  24. {
  25. if (tb_FlieName.Text == "")
  26. {
  27. MessageBox.Show("输入的粒级名错误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  28. tb_FlieName.Focus();
  29. return false;
  30. }
  31. 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符号“,”与“.”的输入法是否为英文,及是否半角格式。";
  32. //粒级值判断
  33. if (tb_new_ljz.Text == "")
  34. {
  35. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  36. tb_new_ljz.Focus();
  37. return false;
  38. }
  39. //粒级值判断,判断是否能通过,纯数字和.及,号组合的正则表达式判断
  40. string pat = @"^[-.,0-9]+$";//纯数字,和.及,
  41. Regex rg = new Regex(pat);
  42. if (false == rg.Match(tb_new_ljz.Text).Success)
  43. {
  44. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  45. tb_new_ljz.Focus();
  46. return false;
  47. }
  48. //粒级值判断,对输入的值进行拆分,然后再对拆分出的各值判断
  49. string[] spstr = tb_new_ljz.Text.Split(',');
  50. for (int i = 0; i < spstr.Length; i++)
  51. {
  52. string lsstr = spstr[i];
  53. pat = @"^\d+(\.\d+)?$";//纯正浮点数数值,含0
  54. rg = new Regex(pat);
  55. if (false == rg.Match(lsstr).Success)
  56. {
  57. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  58. tb_new_ljz.Focus();
  59. return false;
  60. }
  61. }
  62. //最后转换,是否抱错
  63. try
  64. {
  65. for (int i = 0; i < spstr.Length; i++)
  66. {
  67. double lsd = Convert.ToDouble(spstr[i].Trim());
  68. }
  69. }
  70. catch
  71. {
  72. MessageBox.Show(tipstr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  73. tb_new_ljz.Focus();
  74. return false;
  75. }
  76. return true;
  77. }
  78. private void bt_save_Click(object sender, EventArgs e)
  79. {
  80. //校验输入合法性
  81. if (Verify() == false)
  82. {
  83. return;
  84. }
  85. for (int i=0; i< partsize.Rows.Count;i++ )
  86. {
  87. if(partsize.Rows[i]["FileName"].ToString()== tb_FlieName.Text+ ".psf")
  88. {
  89. MessageBox.Show("File name conflict");
  90. return;
  91. }
  92. if(partsize.Rows[i]["Name"].ToString() == tb_new_ljm.Text)
  93. {
  94. MessageBox.Show("Particle name conflict");
  95. return;
  96. }
  97. }
  98. FileStream file = new FileStream(Environment.CurrentDirectory + "\\Config\\ProData\\" + tb_FlieName.Text + ".psf", FileMode.CreateNew);
  99. byte[] data = System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n<XMLData FileMark=\"626\" Name=\"" + tb_new_ljm.Text.Trim() + "\" Sizes=\"" + tb_new_ljz.Text.Trim() + "\" Version=\"1.1.1\" /> ");
  100. file.Write(data, 0, data.Length);
  101. file.Flush();
  102. file.Close();
  103. this.Close();
  104. }
  105. private void bt_export_Click(object sender, EventArgs e)
  106. {
  107. this.Close();
  108. }
  109. }
  110. }