BaseUtil.cs 42 KB

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