BaseUtil.cs 43 KB

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