BaseUtil.cs 48 KB

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