BaseUtil.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. ftpOperater ftp = new ftpOperater();
  234. return ftp.DownLoadFromSharePath(URL, LabelName);
  235. }
  236. /// <summary>
  237. /// 移除重复行
  238. /// </summary>
  239. /// <param name="dt"></param>
  240. /// <param name="field"></param>
  241. /// <returns></returns>
  242. public static DataTable DeleteSameRow(DataTable dt, string field)
  243. {
  244. ArrayList indexList = new ArrayList();
  245. // 找出待删除的行索引
  246. for (int i = 0; i < dt.Rows.Count - 1; i++)
  247. {
  248. if (!IsContain(indexList, i))
  249. {
  250. for (int j = i + 1; j < dt.Rows.Count; j++)
  251. {
  252. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  253. {
  254. indexList.Add(j);
  255. }
  256. }
  257. }
  258. }
  259. indexList.Sort();
  260. // 排序
  261. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  262. {
  263. int index = Convert.ToInt32(indexList[i]);
  264. dt.Rows.RemoveAt(index);
  265. }
  266. return dt;
  267. }
  268. /// <summary>
  269. /// 判断数组中是否存在
  270. /// </summary>
  271. /// <param name="indexList">数组</param>
  272. /// <param name="index">索引</param>
  273. /// <returns></returns>
  274. public static bool IsContain(ArrayList indexList, int index)
  275. {
  276. for (int i = 0; i < indexList.Count; i++)
  277. {
  278. int tempIndex = Convert.ToInt32(indexList[i]);
  279. if (tempIndex == index)
  280. {
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. //播放音频文件
  287. public static void PlaySound(string FileName)
  288. {
  289. //要加载COM组件:Microsoft speech object Library
  290. if (!System.IO.File.Exists(FileName))
  291. {
  292. return;
  293. }
  294. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  295. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  296. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  297. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  298. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  299. spFs.Close();
  300. }
  301. /// <summary>
  302. /// 从DGV获取指定的列的数据形式是数组的形式
  303. /// </summary>
  304. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  305. {
  306. ArrayList[] array = new ArrayList[ColumnName.Length];
  307. //实例化和查询参数个数一样的ArrayList
  308. for (int i = 0; i < ColumnName.Length; i++)
  309. {
  310. array[i] = new ArrayList();
  311. }
  312. DataTable dt = (DataTable)dgv.DataSource;
  313. //如果第一列是否选框的话
  314. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  315. {
  316. for (int i = 0; i < dt.Rows.Count; i++)
  317. {
  318. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  319. {
  320. for (int j = 0; j < ColumnName.Length; j++)
  321. {
  322. array[j].Add(dt.Rows[i][ColumnName[j]]);
  323. }
  324. }
  325. }
  326. }
  327. //否则直接获取全部的数据
  328. else
  329. {
  330. for (int i = 0; i < dgv.RowCount; i++)
  331. {
  332. for (int j = 0; j < ColumnName.Length; j++)
  333. {
  334. array[j].Add(dt.Rows[i][ColumnName[j]]);
  335. }
  336. }
  337. }
  338. return array;
  339. }
  340. /// <summary>
  341. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  342. /// </summary>
  343. /// <param name="dgv"></param>
  344. /// <param name="field"></param>
  345. public static void HideField(DataGridView dgv, string[] field)
  346. {
  347. DataTable dt = (DataTable)dgv.DataSource;
  348. foreach (DataColumn dc in dt.Columns)
  349. {
  350. foreach (string s in field)
  351. {
  352. if (dc.Caption == s)
  353. dgv.Columns[dc.ColumnName].Visible = false;
  354. }
  355. }
  356. }
  357. /// <summary>
  358. ///
  359. /// </summary>
  360. /// <param name="dgv"></param>
  361. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  362. {
  363. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  364. {
  365. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  366. {
  367. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  368. if (CollapseRowCount > 0)
  369. {
  370. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  371. {
  372. try
  373. {
  374. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  375. }
  376. catch (Exception) { }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. /// <summary>
  383. /// 通过查询的内容获取到字段的描述
  384. /// </summary>
  385. /// <param name="field"></param>
  386. /// <returns></returns>
  387. public static string[] GetCaptionFromField(string field)
  388. {
  389. string[] caption = field.Split(',');
  390. for (int i = 0; i < caption.Length; i++)
  391. {
  392. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  393. }
  394. return caption;
  395. }
  396. /// <summary>
  397. /// 通过查询的语句获取查询的字段
  398. /// </summary>
  399. /// <param name="field"></param>
  400. /// <returns></returns>
  401. public static string[] GetField(string field)
  402. {
  403. string[] fields = field.Split(',');
  404. for (int i = 0; i < fields.Length; i++)
  405. {
  406. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  407. }
  408. return fields;
  409. }
  410. /// <summary>
  411. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  412. /// </summary>
  413. /// <param name="dt"></param>
  414. /// <param name="caption"></param>
  415. /// <returns></returns>
  416. public static string GetColumnNameByCaption(DataTable dt, string caption)
  417. {
  418. foreach (DataColumn dc in dt.Columns)
  419. {
  420. if (dc.Caption.ToLower() == caption)
  421. {
  422. return dc.ColumnName;
  423. }
  424. }
  425. return null;
  426. }
  427. //用于封装异常,也可以用于错误的提示
  428. public static void ShowError(string errorMessage)
  429. {
  430. throw new Exception(errorMessage);
  431. }
  432. /// <summary>
  433. /// 判断控件的某个事件是否已经绑定了方法
  434. /// </summary>
  435. /// <param name="Control1"></param>
  436. /// <param name="EventName"></param>
  437. /// <returns></returns>
  438. public static bool ControlHasEvent(Control Control1, string EventName)
  439. {
  440. //需要查询的内容
  441. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  442. Assembly a = Assembly.GetAssembly(Control1.GetType());
  443. Type t = a.GetType(Control1.GetType().FullName, true);
  444. //获取控件的事件
  445. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  446. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  447. //判断事件的委托数量是否大于0
  448. if (ehl != null)
  449. {
  450. Delegate d = ehl[fi.GetValue(Control1)];
  451. if (d != null && d.GetInvocationList().Length > 0)
  452. {
  453. return true;
  454. }
  455. }
  456. return false;
  457. }
  458. /// <summary>
  459. /// 获取控件的事件列表
  460. /// </summary>
  461. /// <param name="Control1"></param>
  462. /// <returns></returns>
  463. public static FieldInfo[] GetControlsEvent(Control Control1)
  464. {
  465. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  466. Assembly a = Assembly.GetAssembly(Control1.GetType());
  467. Type t = a.GetType(Control1.GetType().FullName, true);
  468. FieldInfo[] finf = t.GetFields(myBindingFlags);
  469. return finf;
  470. }
  471. /// <summary>
  472. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  473. /// </summary>
  474. /// <param name="dt"></param>
  475. public static void CleanDataTable(DataTable dt)
  476. {
  477. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  478. dt.Columns.Remove(dt.Columns[i]);
  479. }
  480. /// <summary>
  481. /// 获取标签的路径
  482. /// </summary>
  483. /// <param name="URL"></param>
  484. /// <param name="LabelName"></param>
  485. /// <returns></returns>
  486. public static string GetLabelUrl(string URL, string LabelName)
  487. {
  488. ftpOperater ftp = new ftpOperater();
  489. return ftp.DownLoadFromSharePath(URL, LabelName);
  490. }
  491. /// <summary>
  492. /// 往DataTable中添加数据
  493. /// </summary>
  494. public static void AddDataToDataTable(DataTable dt, params string[] param)
  495. {
  496. DataRow dr = dt.NewRow();
  497. for (int i = 0; i < dt.Columns.Count; i++)
  498. {
  499. dr[dt.Columns[i].ColumnName] = param[i];
  500. }
  501. }
  502. /// <summary>
  503. /// 不清除表结构,只清除数据
  504. /// </summary>
  505. /// <param name="dt"></param>
  506. public static void CleanDataTableData(DataTable dt)
  507. {
  508. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  509. {
  510. dt.Rows.Remove(dt.Rows[i]);
  511. }
  512. }
  513. /// <summary>
  514. /// 获取拼接的字段
  515. /// </summary>
  516. /// <param name="dt"></param>
  517. /// <returns></returns>
  518. public static string GetFieldFromDataTable(DataTable dt)
  519. {
  520. StringBuilder sb = new StringBuilder();
  521. foreach (DataColumn dc in dt.Columns)
  522. {
  523. sb.Append(dc.Caption + ",");
  524. }
  525. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  526. }
  527. /// <summary>
  528. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  529. /// </summary>
  530. /// <param name="dgv"></param>
  531. /// <param name="dt"></param>
  532. /// <param name="AddOpetateColumn"></param>
  533. /// <param name="operate"></param>
  534. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  535. {
  536. dgv.AutoGenerateColumns = false;
  537. dgv.DataSource = dt;
  538. if (operate.Length > 0)
  539. {
  540. if (dgv.Columns[operate[0].Name] != null)
  541. {
  542. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  543. }
  544. dgv.Columns.Add(operate[0]);
  545. }
  546. ////纯英文的列不予展示
  547. //Regex regEnglish = new Regex("^[A-z]+$");
  548. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  549. //{
  550. // if (regEnglish.IsMatch(dgvc.HeaderText))
  551. // {
  552. // dgvc.Visible = false;
  553. // }
  554. //}
  555. }
  556. /// <summary>
  557. /// 清除DataGridView的数据
  558. /// </summary>
  559. /// <param name="dgv"></param>
  560. public static void CleanDGVData(DataGridView dgv)
  561. {
  562. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  563. {
  564. dgv.Rows.RemoveAt(i);
  565. }
  566. }
  567. public static void CleanForm(Form Form)
  568. {
  569. for (int i = 0; i < Form.Controls.Count; i++)
  570. {
  571. 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)
  572. Form.Controls[i].Text = "";
  573. if (Form.Controls[i] is DataGridView)
  574. CleanDGVData((DataGridView)Form.Controls[i]);
  575. }
  576. }
  577. public static void CleanControlsText(params Control[] ctl)
  578. {
  579. foreach (Control item in ctl)
  580. item.Text = "";
  581. }
  582. /// <summary>
  583. /// 需要SQL的顺序和DGV的列的顺序一致
  584. /// </summary>
  585. /// <param name="dgv"></param>
  586. /// <param name="dt"></param>
  587. /// <param name="CheckBox"></param>
  588. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  589. {
  590. CleanDGVData(dgv);
  591. if (CheckBox)
  592. {
  593. for (int i = 0; i < dt.Rows.Count; i++)
  594. {
  595. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  596. collapseRow.IsCollapse = true;
  597. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  598. collapseRow.Cells.Add(checkcell);
  599. checkcell.Value = CheckBoxTrue;
  600. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  601. for (int j = 0; j < dt.Columns.Count; j++)
  602. {
  603. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  604. textcell.Value = dt.Rows[i][j].ToString();
  605. collapseRow.Cells.Add(textcell);
  606. textcell.ReadOnly = true;
  607. }
  608. collapseRow.Tag = "MainRow";
  609. dgv.Rows.Add(collapseRow);
  610. }
  611. }
  612. else
  613. {
  614. for (int i = 0; i < dt.Rows.Count; i++)
  615. {
  616. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  617. collapseRow.IsCollapse = true;
  618. for (int j = 1; j <= dt.Columns.Count; j++)
  619. {
  620. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  621. textcell.Value = dt.Rows[i][j - 1].ToString();
  622. collapseRow.Cells.Add(textcell);
  623. }
  624. dgv.Rows.Add(collapseRow);
  625. collapseRow.ReadOnly = true;
  626. }
  627. }
  628. }
  629. /// <summary>
  630. /// 用于给DGV中的Combox列赋静态值
  631. /// </summary>
  632. /// <param name="dgvc"></param>
  633. /// <param name="displayField"></param>
  634. /// <param name="valueField"></param>
  635. /// <param name="Value"></param>
  636. /// <returns></returns>
  637. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  638. {
  639. DataTable dt = new DataTable();
  640. dt.Columns.Add(displayField);
  641. dt.Columns.Add(valueField);
  642. for (int i = 0; i < Value.Length; i++)
  643. {
  644. DataGridViewRow row = new DataGridViewRow();
  645. dt.Rows.Add(row);
  646. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  647. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  648. }
  649. dgvc.DataPropertyName = DataPropertyName;
  650. dgvc.DataSource = dt;
  651. dgvc.DisplayMember = displayField;
  652. dgvc.ValueMember = valueField;
  653. }
  654. /// <summary>
  655. /// 用于给DGV中的ComboxCell赋静态值
  656. /// </summary>
  657. /// <param name="dgvcc"></param>
  658. /// <param name="displayField"></param>
  659. /// <param name="valueField"></param>
  660. /// <param name="Value"></param>
  661. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  662. {
  663. DataTable dt = new DataTable();
  664. dt.Columns.Add(displayField);
  665. dt.Columns.Add(valueField);
  666. for (int i = 0; i < Value.Length; i++)
  667. {
  668. DataRow dr = dt.NewRow();
  669. dr[displayField] = Value[i].Split('#')[0];
  670. dr[valueField] = Value[i].Split('#')[1];
  671. dt.Rows.Add(dr);
  672. }
  673. dgvcc.DisplayMember = displayField;
  674. dgvcc.ValueMember = valueField;
  675. dgvcc.DataSource = dt;
  676. }
  677. /// <summary>
  678. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  679. /// </summary>
  680. /// <param name="SQL"></param>
  681. /// <param name="Condition"></param>
  682. /// <returns></returns>
  683. public static string GetScreenSqlCondition(params Control[] Condition)
  684. {
  685. string condition = "";
  686. //用于统计传入的控件的空值数
  687. int EmptyControlCount = 0;
  688. for (int i = 0; i < Condition.Length; i++)
  689. {
  690. //如果Text不为空再进行条件的拼接
  691. if (Condition[i].Text != "")
  692. {
  693. if (Condition[i] is ComboBox)
  694. {
  695. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  696. }
  697. else
  698. {
  699. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  700. }
  701. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  702. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  703. for (int j = i + 1; j < Condition.Length; j++)
  704. {
  705. if (j < Condition.Length)
  706. {
  707. if (Condition[j].Text != "")
  708. {
  709. condition += " and ";
  710. break;
  711. }
  712. }
  713. }
  714. }
  715. else
  716. {
  717. EmptyControlCount = EmptyControlCount + 1;
  718. }
  719. }
  720. //如果所有的控件传入的都是空值则返回也为空
  721. if (EmptyControlCount == Condition.Length)
  722. return "";
  723. else
  724. condition = " where " + condition;
  725. return condition;
  726. }
  727. public static void CleanDataGridView(DataGridView dgv)
  728. {
  729. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  730. {
  731. dgv.Columns.RemoveAt(i);
  732. }
  733. }
  734. /// <summary>
  735. /// 取出SQL中的参数占位符
  736. /// </summary>
  737. /// <param name="SQL"></param>
  738. /// <returns></returns>
  739. public static string[] GetParamFromSQL(string SQL)
  740. {
  741. string[] par = SQL.Split(':');
  742. //用来存参数的数组
  743. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  744. string[] param = new string[par.Length - 1];
  745. for (int i = 0; i < par.Length - 1; i++)
  746. {
  747. //新建一个char类型的数组用来存储每个字节的变量
  748. char[] c = par[i + 1].ToCharArray();
  749. addpar[i] = new StringBuilder();
  750. for (int j = 0; j < c.Length; j++)
  751. {
  752. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  753. {
  754. addpar[i].Append(c[j]);
  755. }
  756. else
  757. {
  758. break;
  759. }
  760. }
  761. }
  762. for (int i = 0; i < par.Length - 1; i++)
  763. {
  764. param[i] = addpar[i].ToString();
  765. }
  766. return param;
  767. }
  768. public static void SetFormCenter(Form form)
  769. {
  770. form.StartPosition = FormStartPosition.CenterParent;
  771. }
  772. /// <summary>
  773. /// 设置DataGridView的指定列可编辑
  774. /// </summary>
  775. /// <param name="DGV"></param>
  776. /// <param name="EditAbleField"></param>
  777. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  778. {
  779. foreach (DataGridViewColumn dc in DGV.Columns)
  780. {
  781. dc.ReadOnly = true;
  782. foreach (string s in EditAbleField)
  783. {
  784. if (dc.Name.ToLower() == s.ToLower())
  785. {
  786. DGV.Columns[dc.Name].ReadOnly = false;
  787. }
  788. }
  789. }
  790. }
  791. //判断带有CheckBox的DGV是否有项目勾选了
  792. public static DataTable DGVIfChecked(DataGridView dgv)
  793. {
  794. int CheckCount = 0;
  795. DataTable dt = new DataTable();
  796. //第一列是勾选框,排除在循环之外
  797. for (int i = 1; i < dgv.Columns.Count; i++)
  798. {
  799. dt.Columns.Add(dgv.Columns[i].Name);
  800. }
  801. for (int i = 0; i < dgv.RowCount; i++)
  802. {
  803. if (dgv.Rows[i].Cells[0].Value != null)
  804. {
  805. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  806. {
  807. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  808. {
  809. DataRow dr = dt.NewRow();
  810. for (int j = 1; j < dgv.ColumnCount; j++)
  811. {
  812. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  813. }
  814. dt.Rows.Add(dr);
  815. CheckCount++;
  816. }
  817. }
  818. }
  819. }
  820. //判断是否勾选了明细
  821. if (CheckCount == 0)
  822. return null;
  823. return dt;
  824. }
  825. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  826. {
  827. //第一列是勾选框,排除在循环之外
  828. if (dt.Columns.Count == 0)
  829. {
  830. for (int i = 0; i < dgv.Columns.Count; i++)
  831. dt.Columns.Add(dgv.Columns[i].Name);
  832. }
  833. //是展开的子行的数据
  834. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  835. {
  836. DataRow dr = dt.NewRow();
  837. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  838. //判断值是否存在,存在移除重新添加
  839. if (datarow.Length > 0)
  840. {
  841. dt.Rows.Remove(datarow[0]);
  842. }
  843. for (int j = 0; j < dgv.ColumnCount; j++)
  844. {
  845. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  846. }
  847. dt.Rows.Add(dr);
  848. }
  849. }
  850. /// <summary>
  851. /// 设置只允许输入数字
  852. /// </summary>
  853. /// <param name="sender"></param>
  854. /// <param name="e"></param>
  855. public static void NumOnly(object sender, KeyPressEventArgs e)
  856. {
  857. if (e.KeyChar != '\b')//这是允许输入退格键
  858. {
  859. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  860. {
  861. e.Handled = true;
  862. }
  863. }
  864. }
  865. public static string AddField(string[] Fields)
  866. {
  867. string sql = " ";
  868. foreach (string field in Fields)
  869. sql += field + ",";
  870. return sql.Substring(0, sql.Length - 1);
  871. }
  872. /// <summary>
  873. /// 筛选DataTable
  874. /// </summary>
  875. /// <param name="dt"></param>
  876. /// <param name="condition"></param>
  877. /// <returns></returns>
  878. public static DataTable filterDataTable(DataTable dt, String condition)
  879. {
  880. //获取筛选条件中的列名,值
  881. DataRow[] dataRows = dt.Select(condition);
  882. DataTable ndt = dt.Clone();
  883. for (int i = 0; i < dataRows.Length; i++)
  884. {
  885. ndt.Rows.Add(dataRows[i].ItemArray);
  886. }
  887. return ndt;
  888. }
  889. /// <summary>
  890. /// 图表绘制公共方法样本
  891. /// </summary>
  892. /// <param name="chart1"></param>
  893. /// <param name="_dt"></param>
  894. /// <param name="seriesChartType"></param>
  895. /// <param name="_title"></param>
  896. /// <param name="XValueMember"></param>
  897. /// <param name="YValueMembers"></param>
  898. /// <param name="Xname"></param>
  899. /// <param name="Yname"></param>
  900. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  901. {
  902. chart1.Series[0].ChartType = seriesChartType;
  903. chart1.DataSource = _dt;
  904. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  905. chart1.Series[0].MarkerSize = 8;
  906. chart1.Series[0].XValueMember = XValueMember;
  907. chart1.Series[0].YValueMembers = YValueMembers;
  908. chart1.Series[0].Label = "#VAL";
  909. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  910. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  911. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  912. chart1.Series[0].LabelBackColor = Color.Transparent;
  913. chart1.Series[0].LegendText = Yname;
  914. chart1.Legends[0].Title = _title;
  915. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  916. }
  917. /// <summary>
  918. /// 将新增打印进程信息写入静态文件
  919. /// </summary>
  920. /// <param name="lbl"></param>
  921. public static void WriteLbl(ApplicationClass lbl)
  922. {
  923. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  924. List<int> PidList = new List<int>();
  925. List<System.DateTime> Pstarttime = new List<System.DateTime>();
  926. int PID = 0;
  927. for (int i = 0; i < processes.Length; i++)
  928. {
  929. PidList.Add(processes[i].Id);
  930. Pstarttime.Add(processes[i].StartTime);
  931. }
  932. System.DateTime Temp = Pstarttime[0];
  933. if (Pstarttime.ToArray().Length > 0)
  934. {
  935. PID = PidList[0];
  936. }
  937. for (int i = 0; i < PidList.ToArray().Length; i++)
  938. {
  939. if (Temp < Pstarttime[i])
  940. {
  941. PID = PidList[i];
  942. }
  943. else
  944. {
  945. Temp = Pstarttime[i];
  946. }
  947. }
  948. String str = SystemInf.ProcessesID + "|" + PID;
  949. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  950. FileStream fs = new FileStream(Directory.GetCurrentDirectory() + @"\" + "lblprocess" + ".txt", FileMode.Append, FileAccess.Write);
  951. StreamWriter sw = new StreamWriter(fs);
  952. sw.WriteLine(str, Encoding.UTF8);
  953. sw.Close();
  954. fs.Close();
  955. }
  956. public static Object GetCacheData(string ParamName)
  957. {
  958. Object o = null;
  959. //根据地址读取xml文件
  960. XmlDocument doc = new XmlDocument();
  961. XmlReaderSettings settings = new XmlReaderSettings();
  962. //忽略文档里面的注释
  963. settings.IgnoreComments = true;
  964. XmlReader reader = XmlReader.Create(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml", settings);
  965. doc.Load(reader);
  966. //先得到根节点
  967. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  968. //再由根节点去找制定的节点
  969. XmlNodeList nodeList = rootNode.ChildNodes;
  970. foreach (XmlNode node in nodeList)
  971. {
  972. //找到了这个节点名字
  973. if (node.Name == ParamName)
  974. {
  975. //返回节点的内容
  976. switch (((XmlElement)node).GetAttribute("Type"))
  977. {
  978. case "System.String":
  979. o = node.InnerText;
  980. break;
  981. case "System.Int32":
  982. o = int.Parse(node.InnerText);
  983. break;
  984. case "System.Boolean":
  985. o = node.InnerText == "True" ? true : false;
  986. break;
  987. default:
  988. break;
  989. }
  990. break;
  991. }
  992. }
  993. //关闭reader
  994. reader.Close();
  995. if (o == null)
  996. return "";
  997. else
  998. return o;
  999. }
  1000. public static void SetCacheData(string ParamName, object Value)
  1001. {
  1002. //根据地址读取xml文件
  1003. XmlDocument doc = new XmlDocument();
  1004. XmlReaderSettings settings = new XmlReaderSettings();
  1005. //忽略文档里面的注释
  1006. settings.IgnoreComments = true;
  1007. XmlReader reader = XmlReader.Create(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml", settings);
  1008. doc.Load(reader);
  1009. //先得到根节点
  1010. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1011. //再由根节点去找制定的节点
  1012. XmlNodeList nodeList = rootNode.ChildNodes;
  1013. bool flag = false;
  1014. foreach (XmlNode node in nodeList)
  1015. {
  1016. //找到了这个节点名字
  1017. if (node.Name == ParamName)
  1018. {
  1019. //就直接赋值
  1020. node.InnerText = Value.ToString();
  1021. flag = true;
  1022. }
  1023. }
  1024. //如果没有该节点,就创建节点保存结果
  1025. if (!flag)
  1026. {
  1027. //创建节点
  1028. XmlElement newNode = doc.CreateElement(ParamName);
  1029. XmlAttribute attr = doc.CreateAttribute("Type");
  1030. attr.InnerText = Value.GetType().ToString();
  1031. newNode.InnerText = Value.ToString();
  1032. newNode.SetAttributeNode(attr);
  1033. //讲新建的节点挂到根节点上
  1034. rootNode.AppendChild(newNode);
  1035. }
  1036. //关闭Reader
  1037. reader.Close();
  1038. doc.Save(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml");
  1039. }
  1040. public static void ClosePrint(ApplicationClass lbl)
  1041. {
  1042. lblpro = lbl;
  1043. Thread close = new Thread(ClosePrintProcess);
  1044. close.Start();
  1045. }
  1046. static ApplicationClass lblpro;
  1047. private static void ClosePrintProcess()
  1048. {
  1049. try
  1050. {
  1051. lblpro.Quit();
  1052. }
  1053. catch (Exception)
  1054. {
  1055. }
  1056. }
  1057. }
  1058. }