BaseUtil.cs 42 KB

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