BaseUtil.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. using LabelManager2;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading;
  16. using System.Windows.Forms;
  17. using System.Windows.Forms.DataVisualization.Charting;
  18. using System.Xml;
  19. using UAS_MES.CustomControl.DataGrid_View;
  20. using UAS_MES.CustomControl.GroupBoxWithBorder;
  21. using UAS_MES.CustomControl.TextBoxWithIcon;
  22. using UAS_MES.CustomControl.ValueLabel;
  23. using UAS_MES.Entity;
  24. using static System.Windows.Forms.Control;
  25. namespace UAS_MES.PublicMethod
  26. {
  27. class BaseUtil
  28. {
  29. /// <summary>
  30. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  31. /// </summary>
  32. /// <param name=""></param>
  33. /// <returns></returns>
  34. public static string GetGridViewSelectContent(DataGridView d)
  35. {
  36. StringBuilder selectConetnt = new StringBuilder();
  37. DataTable dt = (DataTable)d.DataSource;
  38. if (dt == null)
  39. {
  40. foreach (DataGridViewColumn dc in d.Columns)
  41. {
  42. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  43. {
  44. selectConetnt.Append(dc.Name + " as " + dc.Name + ",");
  45. }
  46. }
  47. }
  48. else
  49. {
  50. foreach (DataColumn dc in dt.Columns)
  51. {
  52. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  53. }
  54. }
  55. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  56. }
  57. /// <summary>
  58. /// 禁止DataGirdView排序
  59. /// </summary>
  60. /// <param name="Dgv"></param>
  61. public static void DataGridViewNotSort(DataGridView Dgv)
  62. {
  63. foreach (DataGridViewColumn item in Dgv.Columns)
  64. item.SortMode = DataGridViewColumnSortMode.NotSortable;
  65. }
  66. public static string GetLocalIP()
  67. {
  68. try
  69. {
  70. string HostName = Dns.GetHostName(); //得到主机名
  71. IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
  72. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  73. {
  74. //从IP地址列表中筛选出IPv4类型的IP地址
  75. //AddressFamily.InterNetwork表示此IP为IPv4,
  76. //AddressFamily.InterNetworkV6表示此地址为IPv6类型
  77. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  78. {
  79. return IpEntry.AddressList[i].ToString();
  80. }
  81. }
  82. return "";
  83. }
  84. catch (Exception ex)
  85. {
  86. MessageBox.Show("获取本机IP出错:" + ex.Message);
  87. return "";
  88. }
  89. }
  90. /// <summary>
  91. /// 通过字段和其展示的中文值获取查询的内容
  92. /// </summary>
  93. /// <param name="field"></param>
  94. /// <param name="cnfield"></param>
  95. /// <returns></returns>
  96. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  97. {
  98. StringBuilder sb = new StringBuilder();
  99. for (int i = 0; i < field.Length; i++)
  100. {
  101. sb.Append(field[i] + " as " + cnfield[i] + ",");
  102. }
  103. //去掉多余的逗号
  104. sb.Remove(sb.Length - 1, 1);
  105. return sb.ToString();
  106. }
  107. /// <summary>
  108. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  109. /// </summary>
  110. /// <param name="collection"></param>
  111. /// <param name="dt"></param>
  112. public static void SetFormValue(ControlCollection collection, DataTable dt)
  113. {
  114. //DataTable存在数据才进行赋值操作
  115. if (dt.Rows.Count > 0)
  116. {
  117. for (int i = 0; i < collection.Count; i++)
  118. {
  119. //如果含有子控件则进行递归调用
  120. if (collection[i].Controls.Count > 0)
  121. SetFormValue(collection[i].Controls, dt);
  122. string controlName = collection[i].Name;
  123. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  124. //默认给TextBox和Label赋值
  125. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox || collection[i] is MaCodeSearchTextBox || collection[i] is EnterTextBox || collection[i] is TextBoxGeneratePaCode || collection[i] is NumericUpDown)
  126. {
  127. for (int j = 0; j < dt.Columns.Count; j++)
  128. {
  129. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  130. {
  131. //字段含有Status内容的才进行转换
  132. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  133. {
  134. //对审批状态进行判断
  135. switch (dt.Rows[0][j].ToString().ToUpper())
  136. {
  137. case "ENTERING":
  138. collection[i].Text = "在录入";
  139. break;
  140. case "UNAPPROVED":
  141. collection[i].Text = "未批准";
  142. break;
  143. case "COMMITED":
  144. collection[i].Text = "已提交";
  145. break;
  146. case "APPROVE":
  147. collection[i].Text = "已批准";
  148. break;
  149. case "AUDITED":
  150. collection[i].Text = "已审核";
  151. break;
  152. case "STARTED":
  153. collection[i].Text = "已下放";
  154. break;
  155. case "UNCHECK":
  156. collection[i].Text = "待检验";
  157. break;
  158. case "CHECKING":
  159. collection[i].Text = "检验中";
  160. break;
  161. default:
  162. collection[i].Text = dt.Rows[0][j].ToString();
  163. break;
  164. }
  165. }
  166. else
  167. collection[i].Text = dt.Rows[0][j].ToString();
  168. }
  169. }
  170. }
  171. //对封装在GroupBox的和Panel的控件进行批量赋值
  172. if (collection[i] is GroupBox || collection[i] is Panel || collection[i] is GroupBoxWithBorder)
  173. {
  174. for (int j = 0; j < collection[i].Controls.Count; j++)
  175. {
  176. controlName = collection[i].Controls[j].Name;
  177. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is Label || collection[i].Controls[j] is SearchTextBox || collection[i].Controls[j] is MaCodeSearchTextBox)
  178. {
  179. for (int k = 0; k < dt.Columns.Count; k++)
  180. {
  181. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  182. {
  183. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. //如果没有记录的话,将dt中含有的列的对应值设置为空
  192. else
  193. {
  194. for (int i = 0; i < collection.Count; i++)
  195. {
  196. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox)
  197. {
  198. for (int j = 0; j < dt.Columns.Count; j++)
  199. {
  200. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  201. {
  202. collection[i].Text = "";
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 获取打印标签
  211. /// </summary>
  212. /// <param name="labelName"></param>
  213. /// <param name="labelUrl"></param>
  214. /// <param name="indate"></param>
  215. public static void GetPrintLabel(string labelName, string labelUrl, string indate)
  216. {
  217. string LabelUrl = labelUrl;
  218. string LabelName = labelName;
  219. System.DateTime time = Convert.ToDateTime(indate);
  220. FileInfo file = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  221. if (time.ToString() != file.LastWriteTime.ToString())
  222. BaseUtil.GetLabelUrl(LabelUrl, LabelName, time);
  223. }
  224. /// <summary>
  225. /// 获取标签的路径
  226. /// </summary>
  227. /// <param name="URL"></param>
  228. /// <param name="LabelName"></param>
  229. /// <param name="time"></param>
  230. /// <returns></returns>
  231. public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
  232. {
  233. //如果是传入的数据是从FTP取的文件
  234. if (URL.Contains("ftp:"))
  235. {
  236. ftpOperater ftp = new ftpOperater();
  237. return ftp.Download(LabelName);
  238. }
  239. else
  240. {
  241. return URL;
  242. }
  243. }
  244. /// <summary>
  245. /// 移除重复行
  246. /// </summary>
  247. /// <param name="dt"></param>
  248. /// <param name="field"></param>
  249. /// <returns></returns>
  250. public static DataTable DeleteSameRow(DataTable dt, string field)
  251. {
  252. ArrayList indexList = new ArrayList();
  253. // 找出待删除的行索引
  254. for (int i = 0; i < dt.Rows.Count - 1; i++)
  255. {
  256. if (!IsContain(indexList, i))
  257. {
  258. for (int j = i + 1; j < dt.Rows.Count; j++)
  259. {
  260. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  261. {
  262. indexList.Add(j);
  263. }
  264. }
  265. }
  266. }
  267. indexList.Sort();
  268. // 排序
  269. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  270. {
  271. int index = Convert.ToInt32(indexList[i]);
  272. dt.Rows.RemoveAt(index);
  273. }
  274. return dt;
  275. }
  276. /// <summary>
  277. /// 判断数组中是否存在
  278. /// </summary>
  279. /// <param name="indexList">数组</param>
  280. /// <param name="index">索引</param>
  281. /// <returns></returns>
  282. public static bool IsContain(ArrayList indexList, int index)
  283. {
  284. for (int i = 0; i < indexList.Count; i++)
  285. {
  286. int tempIndex = Convert.ToInt32(indexList[i]);
  287. if (tempIndex == index)
  288. {
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. //播放音频文件
  295. public static void PlaySound(string FileName)
  296. {
  297. //要加载COM组件:Microsoft speech object Library
  298. if (!System.IO.File.Exists(FileName))
  299. {
  300. return;
  301. }
  302. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  303. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  304. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  305. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  306. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  307. spFs.Close();
  308. }
  309. /// <summary>
  310. /// 从DGV获取指定的列的数据形式是数组的形式
  311. /// </summary>
  312. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  313. {
  314. ArrayList[] array = new ArrayList[ColumnName.Length];
  315. //实例化和查询参数个数一样的ArrayList
  316. for (int i = 0; i < ColumnName.Length; i++)
  317. {
  318. array[i] = new ArrayList();
  319. }
  320. DataTable dt = (DataTable)dgv.DataSource;
  321. //如果第一列是否选框的话
  322. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  323. {
  324. for (int i = 0; i < dt.Rows.Count; i++)
  325. {
  326. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  327. {
  328. for (int j = 0; j < ColumnName.Length; j++)
  329. {
  330. array[j].Add(dt.Rows[i][ColumnName[j]]);
  331. }
  332. }
  333. }
  334. }
  335. //否则直接获取全部的数据
  336. else
  337. {
  338. for (int i = 0; i < dgv.RowCount; i++)
  339. {
  340. for (int j = 0; j < ColumnName.Length; j++)
  341. {
  342. array[j].Add(dt.Rows[i][ColumnName[j]]);
  343. }
  344. }
  345. }
  346. return array;
  347. }
  348. /// <summary>
  349. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  350. /// </summary>
  351. /// <param name="dgv"></param>
  352. /// <param name="field"></param>
  353. public static void HideField(DataGridView dgv, string[] field)
  354. {
  355. DataTable dt = (DataTable)dgv.DataSource;
  356. foreach (DataColumn dc in dt.Columns)
  357. {
  358. foreach (string s in field)
  359. {
  360. if (dc.Caption == s)
  361. dgv.Columns[dc.ColumnName].Visible = false;
  362. }
  363. }
  364. }
  365. /// <summary>
  366. ///
  367. /// </summary>
  368. /// <param name="dgv"></param>
  369. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  370. {
  371. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  372. {
  373. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  374. {
  375. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  376. if (CollapseRowCount > 0)
  377. {
  378. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  379. {
  380. try
  381. {
  382. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  383. }
  384. catch (Exception) { }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// 通过查询的内容获取到字段的描述
  392. /// </summary>
  393. /// <param name="field"></param>
  394. /// <returns></returns>
  395. public static string[] GetCaptionFromField(string field)
  396. {
  397. string[] caption = field.Split(',');
  398. for (int i = 0; i < caption.Length; i++)
  399. {
  400. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  401. }
  402. return caption;
  403. }
  404. /// <summary>
  405. /// 通过查询的语句获取查询的字段
  406. /// </summary>
  407. /// <param name="field"></param>
  408. /// <returns></returns>
  409. public static string[] GetField(string field)
  410. {
  411. string[] fields = field.Split(',');
  412. for (int i = 0; i < fields.Length; i++)
  413. {
  414. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  415. }
  416. return fields;
  417. }
  418. /// <summary>
  419. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  420. /// </summary>
  421. /// <param name="dt"></param>
  422. /// <param name="caption"></param>
  423. /// <returns></returns>
  424. public static string GetColumnNameByCaption(DataTable dt, string caption)
  425. {
  426. foreach (DataColumn dc in dt.Columns)
  427. {
  428. if (dc.Caption.ToLower() == caption)
  429. {
  430. return dc.ColumnName;
  431. }
  432. }
  433. return null;
  434. }
  435. //用于封装异常,也可以用于错误的提示
  436. public static void ShowError(string errorMessage)
  437. {
  438. throw new Exception(errorMessage);
  439. }
  440. /// <summary>
  441. /// 判断控件的某个事件是否已经绑定了方法
  442. /// </summary>
  443. /// <param name="Control1"></param>
  444. /// <param name="EventName"></param>
  445. /// <returns></returns>
  446. public static bool ControlHasEvent(Control Control1, string EventName)
  447. {
  448. //需要查询的内容
  449. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  450. Assembly a = Assembly.GetAssembly(Control1.GetType());
  451. Type t = a.GetType(Control1.GetType().FullName, true);
  452. //获取控件的事件
  453. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  454. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  455. //判断事件的委托数量是否大于0
  456. if (ehl != null)
  457. {
  458. Delegate d = ehl[fi.GetValue(Control1)];
  459. if (d != null && d.GetInvocationList().Length > 0)
  460. {
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. /// <summary>
  467. /// 获取控件的事件列表
  468. /// </summary>
  469. /// <param name="Control1"></param>
  470. /// <returns></returns>
  471. public static FieldInfo[] GetControlsEvent(Control Control1)
  472. {
  473. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  474. Assembly a = Assembly.GetAssembly(Control1.GetType());
  475. Type t = a.GetType(Control1.GetType().FullName, true);
  476. FieldInfo[] finf = t.GetFields(myBindingFlags);
  477. return finf;
  478. }
  479. /// <summary>
  480. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  481. /// </summary>
  482. /// <param name="dt"></param>
  483. public static void CleanDataTable(DataTable dt)
  484. {
  485. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  486. dt.Columns.Remove(dt.Columns[i]);
  487. }
  488. /// <summary>
  489. /// 获取标签的路径
  490. /// </summary>
  491. /// <param name="URL"></param>
  492. /// <param name="LabelName"></param>
  493. /// <returns></returns>
  494. public static string GetLabelUrl(string URL, string LabelName)
  495. {
  496. //如果是传入的数据是从FTP取的文件
  497. if (URL.Contains("ftp:"))
  498. {
  499. ftpOperater ftp = new ftpOperater();
  500. return ftp.Download(LabelName);
  501. }
  502. else
  503. return URL;
  504. }
  505. /// <summary>
  506. /// 往DataTable中添加数据
  507. /// </summary>
  508. public static void AddDataToDataTable(DataTable dt, params string[] param)
  509. {
  510. DataRow dr = dt.NewRow();
  511. for (int i = 0; i < dt.Columns.Count; i++)
  512. {
  513. dr[dt.Columns[i].ColumnName] = param[i];
  514. }
  515. }
  516. /// <summary>
  517. /// 不清除表结构,只清除数据
  518. /// </summary>
  519. /// <param name="dt"></param>
  520. public static void CleanDataTableData(DataTable dt)
  521. {
  522. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  523. {
  524. dt.Rows.Remove(dt.Rows[i]);
  525. }
  526. }
  527. /// <summary>
  528. /// 获取拼接的字段
  529. /// </summary>
  530. /// <param name="dt"></param>
  531. /// <returns></returns>
  532. public static string GetFieldFromDataTable(DataTable dt)
  533. {
  534. StringBuilder sb = new StringBuilder();
  535. foreach (DataColumn dc in dt.Columns)
  536. {
  537. sb.Append(dc.Caption + ",");
  538. }
  539. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  540. }
  541. /// <summary>
  542. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  543. /// </summary>
  544. /// <param name="dgv"></param>
  545. /// <param name="dt"></param>
  546. /// <param name="AddOpetateColumn"></param>
  547. /// <param name="operate"></param>
  548. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  549. {
  550. dgv.AutoGenerateColumns = false;
  551. dgv.DataSource = dt;
  552. if (operate.Length > 0)
  553. {
  554. if (dgv.Columns[operate[0].Name] != null)
  555. {
  556. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  557. }
  558. dgv.Columns.Add(operate[0]);
  559. }
  560. ////纯英文的列不予展示
  561. //Regex regEnglish = new Regex("^[A-z]+$");
  562. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  563. //{
  564. // if (regEnglish.IsMatch(dgvc.HeaderText))
  565. // {
  566. // dgvc.Visible = false;
  567. // }
  568. //}
  569. }
  570. /// <summary>
  571. /// 清除DataGridView的数据
  572. /// </summary>
  573. /// <param name="dgv"></param>
  574. public static void CleanDGVData(DataGridView dgv)
  575. {
  576. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  577. {
  578. dgv.Rows.RemoveAt(i);
  579. }
  580. }
  581. public static void CleanForm(Form Form)
  582. {
  583. for (int i = 0; i < Form.Controls.Count; i++)
  584. {
  585. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is ValueLabel || Form.Controls[i] is SearchTextBox || Form.Controls[i] is ValueNumLabel || Form.Controls[i] is MaCodeSearchTextBox)
  586. Form.Controls[i].Text = "";
  587. if (Form.Controls[i] is DataGridView)
  588. CleanDGVData((DataGridView)Form.Controls[i]);
  589. }
  590. }
  591. public static void CleanControlsText(params Control[] ctl)
  592. {
  593. foreach (Control item in ctl)
  594. item.Text = "";
  595. }
  596. /// <summary>
  597. /// 需要SQL的顺序和DGV的列的顺序一致
  598. /// </summary>
  599. /// <param name="dgv"></param>
  600. /// <param name="dt"></param>
  601. /// <param name="CheckBox"></param>
  602. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  603. {
  604. CleanDGVData(dgv);
  605. if (CheckBox)
  606. {
  607. for (int i = 0; i < dt.Rows.Count; i++)
  608. {
  609. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  610. collapseRow.IsCollapse = true;
  611. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  612. collapseRow.Cells.Add(checkcell);
  613. checkcell.Value = CheckBoxTrue;
  614. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  615. for (int j = 0; j < dt.Columns.Count; j++)
  616. {
  617. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  618. textcell.Value = dt.Rows[i][j].ToString();
  619. collapseRow.Cells.Add(textcell);
  620. textcell.ReadOnly = true;
  621. }
  622. collapseRow.Tag = "MainRow";
  623. dgv.Rows.Add(collapseRow);
  624. }
  625. }
  626. else
  627. {
  628. for (int i = 0; i < dt.Rows.Count; i++)
  629. {
  630. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  631. collapseRow.IsCollapse = true;
  632. for (int j = 1; j <= dt.Columns.Count; j++)
  633. {
  634. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  635. textcell.Value = dt.Rows[i][j - 1].ToString();
  636. collapseRow.Cells.Add(textcell);
  637. }
  638. dgv.Rows.Add(collapseRow);
  639. collapseRow.ReadOnly = true;
  640. }
  641. }
  642. }
  643. /// <summary>
  644. /// 用于给DGV中的Combox列赋静态值
  645. /// </summary>
  646. /// <param name="dgvc"></param>
  647. /// <param name="displayField"></param>
  648. /// <param name="valueField"></param>
  649. /// <param name="Value"></param>
  650. /// <returns></returns>
  651. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  652. {
  653. DataTable dt = new DataTable();
  654. dt.Columns.Add(displayField);
  655. dt.Columns.Add(valueField);
  656. for (int i = 0; i < Value.Length; i++)
  657. {
  658. DataGridViewRow row = new DataGridViewRow();
  659. dt.Rows.Add(row);
  660. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  661. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  662. }
  663. dgvc.DataPropertyName = DataPropertyName;
  664. dgvc.DataSource = dt;
  665. dgvc.DisplayMember = displayField;
  666. dgvc.ValueMember = valueField;
  667. }
  668. /// <summary>
  669. /// 用于给DGV中的ComboxCell赋静态值
  670. /// </summary>
  671. /// <param name="dgvcc"></param>
  672. /// <param name="displayField"></param>
  673. /// <param name="valueField"></param>
  674. /// <param name="Value"></param>
  675. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  676. {
  677. DataTable dt = new DataTable();
  678. dt.Columns.Add(displayField);
  679. dt.Columns.Add(valueField);
  680. for (int i = 0; i < Value.Length; i++)
  681. {
  682. DataRow dr = dt.NewRow();
  683. dr[displayField] = Value[i].Split('#')[0];
  684. dr[valueField] = Value[i].Split('#')[1];
  685. dt.Rows.Add(dr);
  686. }
  687. dgvcc.DisplayMember = displayField;
  688. dgvcc.ValueMember = valueField;
  689. dgvcc.DataSource = dt;
  690. }
  691. /// <summary>
  692. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  693. /// </summary>
  694. /// <param name="SQL"></param>
  695. /// <param name="Condition"></param>
  696. /// <returns></returns>
  697. public static string GetScreenSqlCondition(params Control[] Condition)
  698. {
  699. string condition = "";
  700. //用于统计传入的控件的空值数
  701. int EmptyControlCount = 0;
  702. for (int i = 0; i < Condition.Length; i++)
  703. {
  704. //如果Text不为空再进行条件的拼接
  705. if (Condition[i].Text != "")
  706. {
  707. if (Condition[i] is ComboBox)
  708. {
  709. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  710. }
  711. else
  712. {
  713. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  714. }
  715. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  716. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  717. for (int j = i + 1; j < Condition.Length; j++)
  718. {
  719. if (j < Condition.Length)
  720. {
  721. if (Condition[j].Text != "")
  722. {
  723. condition += " and ";
  724. break;
  725. }
  726. }
  727. }
  728. }
  729. else
  730. {
  731. EmptyControlCount = EmptyControlCount + 1;
  732. }
  733. }
  734. //如果所有的控件传入的都是空值则返回也为空
  735. if (EmptyControlCount == Condition.Length)
  736. return "";
  737. else
  738. condition = " where " + condition;
  739. return condition;
  740. }
  741. public static void CleanDataGridView(DataGridView dgv)
  742. {
  743. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  744. {
  745. dgv.Columns.RemoveAt(i);
  746. }
  747. }
  748. /// <summary>
  749. /// 取出SQL中的参数占位符
  750. /// </summary>
  751. /// <param name="SQL"></param>
  752. /// <returns></returns>
  753. public static string[] GetParamFromSQL(string SQL)
  754. {
  755. string[] par = SQL.Split(':');
  756. //用来存参数的数组
  757. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  758. string[] param = new string[par.Length - 1];
  759. for (int i = 0; i < par.Length - 1; i++)
  760. {
  761. //新建一个char类型的数组用来存储每个字节的变量
  762. char[] c = par[i + 1].ToCharArray();
  763. addpar[i] = new StringBuilder();
  764. for (int j = 0; j < c.Length; j++)
  765. {
  766. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  767. {
  768. addpar[i].Append(c[j]);
  769. }
  770. else
  771. {
  772. break;
  773. }
  774. }
  775. }
  776. for (int i = 0; i < par.Length - 1; i++)
  777. {
  778. param[i] = addpar[i].ToString();
  779. }
  780. return param;
  781. }
  782. public static void SetFormCenter(Form form)
  783. {
  784. form.StartPosition = FormStartPosition.CenterParent;
  785. }
  786. /// <summary>
  787. /// 设置DataGridView的指定列可编辑
  788. /// </summary>
  789. /// <param name="DGV"></param>
  790. /// <param name="EditAbleField"></param>
  791. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  792. {
  793. foreach (DataGridViewColumn dc in DGV.Columns)
  794. {
  795. dc.ReadOnly = true;
  796. foreach (string s in EditAbleField)
  797. {
  798. if (dc.Name.ToLower() == s.ToLower())
  799. {
  800. DGV.Columns[dc.Name].ReadOnly = false;
  801. }
  802. }
  803. }
  804. }
  805. //判断带有CheckBox的DGV是否有项目勾选了
  806. public static DataTable DGVIfChecked(DataGridView dgv)
  807. {
  808. int CheckCount = 0;
  809. DataTable dt = new DataTable();
  810. //第一列是勾选框,排除在循环之外
  811. for (int i = 1; i < dgv.Columns.Count; i++)
  812. {
  813. dt.Columns.Add(dgv.Columns[i].Name);
  814. }
  815. for (int i = 0; i < dgv.RowCount; i++)
  816. {
  817. if (dgv.Rows[i].Cells[0].Value != null)
  818. {
  819. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  820. {
  821. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  822. {
  823. DataRow dr = dt.NewRow();
  824. for (int j = 1; j < dgv.ColumnCount; j++)
  825. {
  826. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  827. }
  828. dt.Rows.Add(dr);
  829. CheckCount++;
  830. }
  831. }
  832. }
  833. }
  834. //判断是否勾选了明细
  835. if (CheckCount == 0)
  836. return null;
  837. return dt;
  838. }
  839. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  840. {
  841. //第一列是勾选框,排除在循环之外
  842. if (dt.Columns.Count == 0)
  843. {
  844. for (int i = 0; i < dgv.Columns.Count; i++)
  845. dt.Columns.Add(dgv.Columns[i].Name);
  846. }
  847. //是展开的子行的数据
  848. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  849. {
  850. DataRow dr = dt.NewRow();
  851. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  852. //判断值是否存在,存在移除重新添加
  853. if (datarow.Length > 0)
  854. {
  855. dt.Rows.Remove(datarow[0]);
  856. }
  857. for (int j = 0; j < dgv.ColumnCount; j++)
  858. {
  859. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  860. }
  861. dt.Rows.Add(dr);
  862. }
  863. }
  864. /// <summary>
  865. /// 设置只允许输入数字
  866. /// </summary>
  867. /// <param name="sender"></param>
  868. /// <param name="e"></param>
  869. public static void NumOnly(object sender, KeyPressEventArgs e)
  870. {
  871. if (e.KeyChar != '\b')//这是允许输入退格键
  872. {
  873. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  874. {
  875. e.Handled = true;
  876. }
  877. }
  878. }
  879. public static string AddField(string[] Fields)
  880. {
  881. string sql = " ";
  882. foreach (string field in Fields)
  883. sql += field + ",";
  884. return sql.Substring(0, sql.Length - 1);
  885. }
  886. /// <summary>
  887. /// 筛选DataTable
  888. /// </summary>
  889. /// <param name="dt"></param>
  890. /// <param name="condition"></param>
  891. /// <returns></returns>
  892. public static DataTable filterDataTable(DataTable dt, String condition)
  893. {
  894. //获取筛选条件中的列名,值
  895. DataRow[] dataRows = dt.Select(condition);
  896. DataTable ndt = dt.Clone();
  897. for (int i = 0; i < dataRows.Length; i++)
  898. {
  899. ndt.Rows.Add(dataRows[i].ItemArray);
  900. }
  901. return ndt;
  902. }
  903. /// <summary>
  904. /// 图表绘制公共方法样本
  905. /// </summary>
  906. /// <param name="chart1"></param>
  907. /// <param name="_dt"></param>
  908. /// <param name="seriesChartType"></param>
  909. /// <param name="_title"></param>
  910. /// <param name="XValueMember"></param>
  911. /// <param name="YValueMembers"></param>
  912. /// <param name="Xname"></param>
  913. /// <param name="Yname"></param>
  914. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  915. {
  916. chart1.Series[0].ChartType = seriesChartType;
  917. chart1.DataSource = _dt;
  918. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  919. chart1.Series[0].MarkerSize = 8;
  920. chart1.Series[0].XValueMember = XValueMember;
  921. chart1.Series[0].YValueMembers = YValueMembers;
  922. chart1.Series[0].Label = "#VAL";
  923. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  924. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  925. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  926. chart1.Series[0].LabelBackColor = Color.Transparent;
  927. chart1.Series[0].LegendText = Yname;
  928. chart1.Legends[0].Title = _title;
  929. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  930. }
  931. /// <summary>
  932. /// 将新增打印进程信息写入静态文件
  933. /// </summary>
  934. /// <param name="lbl"></param>
  935. public static void WriteLbl(ApplicationClass lbl)
  936. {
  937. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  938. List<int> PidList = new List<int>();
  939. List<System.DateTime> Pstarttime = new List<System.DateTime>();
  940. int PID = 0;
  941. for (int i = 0; i < processes.Length; i++)
  942. {
  943. PidList.Add(processes[i].Id);
  944. Pstarttime.Add(processes[i].StartTime);
  945. }
  946. System.DateTime Temp = Pstarttime[0];
  947. if (Pstarttime.ToArray().Length > 0)
  948. {
  949. PID = PidList[0];
  950. }
  951. for (int i = 0; i < PidList.ToArray().Length; i++)
  952. {
  953. if (Temp < Pstarttime[i])
  954. {
  955. PID = PidList[i];
  956. }
  957. else
  958. {
  959. Temp = Pstarttime[i];
  960. }
  961. }
  962. String str = SystemInf.ProcessesID + "|" + PID;
  963. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  964. FileStream fs = new FileStream(Directory.GetCurrentDirectory() + @"\" + "lblprocess" + ".txt", FileMode.Append, FileAccess.Write);
  965. StreamWriter sw = new StreamWriter(fs);
  966. sw.WriteLine(str, Encoding.UTF8);
  967. sw.Close();
  968. fs.Close();
  969. }
  970. public static Object GetCacheData(string ParamName)
  971. {
  972. Object o = null;
  973. //根据地址读取xml文件
  974. XmlDocument doc = new XmlDocument();
  975. XmlReaderSettings settings = new XmlReaderSettings();
  976. //忽略文档里面的注释
  977. settings.IgnoreComments = true;
  978. XmlReader reader = XmlReader.Create(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml", settings);
  979. doc.Load(reader);
  980. //先得到根节点
  981. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  982. //再由根节点去找制定的节点
  983. XmlNodeList nodeList = rootNode.ChildNodes;
  984. foreach (XmlNode node in nodeList)
  985. {
  986. //找到了这个节点名字
  987. if (node.Name == ParamName)
  988. {
  989. //返回节点的内容
  990. switch (((XmlElement)node).GetAttribute("Type"))
  991. {
  992. case "System.String":
  993. o = node.InnerText;
  994. break;
  995. case "System.Int32":
  996. o = int.Parse(node.InnerText);
  997. break;
  998. case "System.Boolean":
  999. o = node.InnerText == "True" ? true : false;
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. break;
  1005. }
  1006. }
  1007. //关闭reader
  1008. reader.Close();
  1009. if (o == null)
  1010. return "";
  1011. else
  1012. return o;
  1013. }
  1014. public static void SetCacheData(string ParamName, object Value)
  1015. {
  1016. //根据地址读取xml文件
  1017. XmlDocument doc = new XmlDocument();
  1018. XmlReaderSettings settings = new XmlReaderSettings();
  1019. //忽略文档里面的注释
  1020. settings.IgnoreComments = true;
  1021. XmlReader reader = XmlReader.Create(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml", settings);
  1022. doc.Load(reader);
  1023. //先得到根节点
  1024. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1025. //再由根节点去找制定的节点
  1026. XmlNodeList nodeList = rootNode.ChildNodes;
  1027. bool flag = false;
  1028. foreach (XmlNode node in nodeList)
  1029. {
  1030. //找到了这个节点名字
  1031. if (node.Name == ParamName)
  1032. {
  1033. //就直接赋值
  1034. node.InnerText = Value.ToString();
  1035. flag = true;
  1036. }
  1037. }
  1038. //如果没有该节点,就创建节点保存结果
  1039. if (!flag)
  1040. {
  1041. //创建节点
  1042. XmlElement newNode = doc.CreateElement(ParamName);
  1043. XmlAttribute attr = doc.CreateAttribute("Type");
  1044. attr.InnerText = Value.GetType().ToString();
  1045. newNode.InnerText = Value.ToString();
  1046. newNode.SetAttributeNode(attr);
  1047. //讲新建的节点挂到根节点上
  1048. rootNode.AppendChild(newNode);
  1049. }
  1050. //关闭Reader
  1051. reader.Close();
  1052. doc.Save(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml");
  1053. }
  1054. public static void ClosePrint(ApplicationClass lbl)
  1055. {
  1056. lblpro = lbl;
  1057. Thread close = new Thread(ClosePrintProcess);
  1058. close.Start();
  1059. }
  1060. static ApplicationClass lblpro;
  1061. private static void ClosePrintProcess()
  1062. {
  1063. try
  1064. {
  1065. lblpro.Quit();
  1066. }
  1067. catch (Exception)
  1068. {
  1069. }
  1070. }
  1071. }
  1072. }