BaseUtil.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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, string indate)
  217. {
  218. string LabelUrl = labelUrl;
  219. string LabelName = labelName;
  220. System.DateTime time = Convert.ToDateTime(indate);
  221. FileInfo file = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  222. if (time.ToString() != file.LastWriteTime.ToString())
  223. BaseUtil.GetLabelUrl(LabelUrl, LabelName, time);
  224. }
  225. /// <summary>
  226. /// 获取标签的路径
  227. /// </summary>
  228. /// <param name="URL"></param>
  229. /// <param name="LabelName"></param>
  230. /// <param name="time"></param>
  231. /// <returns></returns>
  232. public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
  233. {
  234. ftpOperater ftp = new ftpOperater();
  235. return ftp.DownLoadFromSharePath(URL, LabelName);
  236. }
  237. /// <summary>
  238. /// 移除重复行
  239. /// </summary>
  240. /// <param name="dt"></param>
  241. /// <param name="field"></param>
  242. /// <returns></returns>
  243. public static DataTable DeleteSameRow(DataTable dt, string field)
  244. {
  245. ArrayList indexList = new ArrayList();
  246. // 找出待删除的行索引
  247. for (int i = 0; i < dt.Rows.Count - 1; i++)
  248. {
  249. if (!IsContain(indexList, i))
  250. {
  251. for (int j = i + 1; j < dt.Rows.Count; j++)
  252. {
  253. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  254. {
  255. indexList.Add(j);
  256. }
  257. }
  258. }
  259. }
  260. indexList.Sort();
  261. // 排序
  262. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  263. {
  264. int index = Convert.ToInt32(indexList[i]);
  265. dt.Rows.RemoveAt(index);
  266. }
  267. return dt;
  268. }
  269. /// <summary>
  270. /// 判断数组中是否存在
  271. /// </summary>
  272. /// <param name="indexList">数组</param>
  273. /// <param name="index">索引</param>
  274. /// <returns></returns>
  275. public static bool IsContain(ArrayList indexList, int index)
  276. {
  277. for (int i = 0; i < indexList.Count; i++)
  278. {
  279. int tempIndex = Convert.ToInt32(indexList[i]);
  280. if (tempIndex == index)
  281. {
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. //播放音频文件
  288. public static void PlaySound(string FileName)
  289. {
  290. //要加载COM组件:Microsoft speech object Library
  291. if (!System.IO.File.Exists(FileName))
  292. {
  293. return;
  294. }
  295. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  296. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  297. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  298. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  299. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  300. spFs.Close();
  301. }
  302. /// <summary>
  303. /// 从DGV获取指定的列的数据形式是数组的形式
  304. /// </summary>
  305. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  306. {
  307. ArrayList[] array = new ArrayList[ColumnName.Length];
  308. //实例化和查询参数个数一样的ArrayList
  309. for (int i = 0; i < ColumnName.Length; i++)
  310. {
  311. array[i] = new ArrayList();
  312. }
  313. DataTable dt = (DataTable)dgv.DataSource;
  314. //如果第一列是否选框的话
  315. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  316. {
  317. for (int i = 0; i < dt.Rows.Count; i++)
  318. {
  319. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  320. {
  321. for (int j = 0; j < ColumnName.Length; j++)
  322. {
  323. array[j].Add(dt.Rows[i][ColumnName[j]]);
  324. }
  325. }
  326. }
  327. }
  328. //否则直接获取全部的数据
  329. else
  330. {
  331. for (int i = 0; i < dgv.RowCount; i++)
  332. {
  333. for (int j = 0; j < ColumnName.Length; j++)
  334. {
  335. array[j].Add(dt.Rows[i][ColumnName[j]]);
  336. }
  337. }
  338. }
  339. return array;
  340. }
  341. /// <summary>
  342. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  343. /// </summary>
  344. /// <param name="dgv"></param>
  345. /// <param name="field"></param>
  346. public static void HideField(DataGridView dgv, string[] field)
  347. {
  348. DataTable dt = (DataTable)dgv.DataSource;
  349. foreach (DataColumn dc in dt.Columns)
  350. {
  351. foreach (string s in field)
  352. {
  353. if (dc.Caption == s)
  354. dgv.Columns[dc.ColumnName].Visible = false;
  355. }
  356. }
  357. }
  358. /// <summary>
  359. ///
  360. /// </summary>
  361. /// <param name="dgv"></param>
  362. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  363. {
  364. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  365. {
  366. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  367. {
  368. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  369. if (CollapseRowCount > 0)
  370. {
  371. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  372. {
  373. try
  374. {
  375. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  376. }
  377. catch (Exception) { }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. /// <summary>
  384. /// 通过查询的内容获取到字段的描述
  385. /// </summary>
  386. /// <param name="field"></param>
  387. /// <returns></returns>
  388. public static string[] GetCaptionFromField(string field)
  389. {
  390. string[] caption = field.Split(',');
  391. for (int i = 0; i < caption.Length; i++)
  392. {
  393. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  394. }
  395. return caption;
  396. }
  397. /// <summary>
  398. /// 通过查询的语句获取查询的字段
  399. /// </summary>
  400. /// <param name="field"></param>
  401. /// <returns></returns>
  402. public static string[] GetField(string field)
  403. {
  404. string[] fields = field.Split(',');
  405. for (int i = 0; i < fields.Length; i++)
  406. {
  407. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  408. }
  409. return fields;
  410. }
  411. /// <summary>
  412. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  413. /// </summary>
  414. /// <param name="dt"></param>
  415. /// <param name="caption"></param>
  416. /// <returns></returns>
  417. public static string GetColumnNameByCaption(DataTable dt, string caption)
  418. {
  419. foreach (DataColumn dc in dt.Columns)
  420. {
  421. if (dc.Caption.ToLower() == caption)
  422. {
  423. return dc.ColumnName;
  424. }
  425. }
  426. return null;
  427. }
  428. //用于封装异常,也可以用于错误的提示
  429. public static void ShowError(string errorMessage)
  430. {
  431. throw new Exception(errorMessage);
  432. }
  433. /// <summary>
  434. /// 判断控件的某个事件是否已经绑定了方法
  435. /// </summary>
  436. /// <param name="Control1"></param>
  437. /// <param name="EventName"></param>
  438. /// <returns></returns>
  439. public static bool ControlHasEvent(Control Control1, string EventName)
  440. {
  441. //需要查询的内容
  442. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  443. Assembly a = Assembly.GetAssembly(Control1.GetType());
  444. Type t = a.GetType(Control1.GetType().FullName, true);
  445. //获取控件的事件
  446. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  447. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  448. //判断事件的委托数量是否大于0
  449. if (ehl != null)
  450. {
  451. Delegate d = ehl[fi.GetValue(Control1)];
  452. if (d != null && d.GetInvocationList().Length > 0)
  453. {
  454. return true;
  455. }
  456. }
  457. return false;
  458. }
  459. /// <summary>
  460. /// 获取控件的事件列表
  461. /// </summary>
  462. /// <param name="Control1"></param>
  463. /// <returns></returns>
  464. public static FieldInfo[] GetControlsEvent(Control Control1)
  465. {
  466. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  467. Assembly a = Assembly.GetAssembly(Control1.GetType());
  468. Type t = a.GetType(Control1.GetType().FullName, true);
  469. FieldInfo[] finf = t.GetFields(myBindingFlags);
  470. return finf;
  471. }
  472. /// <summary>
  473. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  474. /// </summary>
  475. /// <param name="dt"></param>
  476. public static void CleanDataTable(DataTable dt)
  477. {
  478. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  479. dt.Columns.Remove(dt.Columns[i]);
  480. }
  481. /// <summary>
  482. /// 获取标签的路径
  483. /// </summary>
  484. /// <param name="URL"></param>
  485. /// <param name="LabelName"></param>
  486. /// <returns></returns>
  487. public static string GetLabelUrl(string URL, string LabelName)
  488. {
  489. ftpOperater ftp = new ftpOperater();
  490. return ftp.DownLoadFromSharePath(URL, LabelName);
  491. }
  492. /// <summary>
  493. /// 往DataTable中添加数据
  494. /// </summary>
  495. public static void AddDataToDataTable(DataTable dt, params string[] param)
  496. {
  497. DataRow dr = dt.NewRow();
  498. for (int i = 0; i < dt.Columns.Count; i++)
  499. {
  500. dr[dt.Columns[i].ColumnName] = param[i];
  501. }
  502. }
  503. /// <summary>
  504. /// 不清除表结构,只清除数据
  505. /// </summary>
  506. /// <param name="dt"></param>
  507. public static void CleanDataTableData(DataTable dt)
  508. {
  509. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  510. {
  511. dt.Rows.Remove(dt.Rows[i]);
  512. }
  513. }
  514. /// <summary>
  515. /// 获取拼接的字段
  516. /// </summary>
  517. /// <param name="dt"></param>
  518. /// <returns></returns>
  519. public static string GetFieldFromDataTable(DataTable dt)
  520. {
  521. StringBuilder sb = new StringBuilder();
  522. foreach (DataColumn dc in dt.Columns)
  523. {
  524. sb.Append(dc.Caption + ",");
  525. }
  526. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  527. }
  528. /// <summary>
  529. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  530. /// </summary>
  531. /// <param name="dgv"></param>
  532. /// <param name="dt"></param>
  533. /// <param name="AddOpetateColumn"></param>
  534. /// <param name="operate"></param>
  535. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  536. {
  537. dgv.AutoGenerateColumns = false;
  538. dgv.DataSource = dt;
  539. if (operate.Length > 0)
  540. {
  541. if (dgv.Columns[operate[0].Name] != null)
  542. {
  543. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  544. }
  545. dgv.Columns.Add(operate[0]);
  546. }
  547. ////纯英文的列不予展示
  548. //Regex regEnglish = new Regex("^[A-z]+$");
  549. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  550. //{
  551. // if (regEnglish.IsMatch(dgvc.HeaderText))
  552. // {
  553. // dgvc.Visible = false;
  554. // }
  555. //}
  556. }
  557. /// <summary>
  558. /// 清除DataGridView的数据
  559. /// </summary>
  560. /// <param name="dgv"></param>
  561. public static void CleanDGVData(DataGridView dgv)
  562. {
  563. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  564. {
  565. dgv.Rows.RemoveAt(i);
  566. }
  567. }
  568. public static void CleanForm(Form Form)
  569. {
  570. for (int i = 0; i < Form.Controls.Count; i++)
  571. {
  572. 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)
  573. Form.Controls[i].Text = "";
  574. if (Form.Controls[i] is DataGridView)
  575. CleanDGVData((DataGridView)Form.Controls[i]);
  576. }
  577. }
  578. public static void CleanControls(ControlCollection collection)
  579. {
  580. for (int i = 0; i < collection.Count; i++)
  581. {
  582. if (collection[i].Controls.Count > 0)
  583. CleanControls(collection[i].Controls);
  584. 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)
  585. collection[i].Text = "";
  586. if (collection[i] is DataGridView)
  587. CleanDGVData((DataGridView)collection[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. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  937. Dictionary<string, int> ProInf = new Dictionary<string, int>();
  938. int PID = 0;
  939. for (int i = 0; i < processes.Length; i++)
  940. {
  941. ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  942. }
  943. var temp = ProInf.Keys.Max();
  944. String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  945. string lblpath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo";
  946. FileStream fs = new FileStream(lblpath + @"\" + "lblprocess" + ".txt", FileMode.Append, FileAccess.Write);
  947. StreamWriter sw = new StreamWriter(fs);
  948. sw.WriteLine(str, Encoding.UTF8);
  949. sw.Close();
  950. fs.Close();
  951. }
  952. public static Object GetCacheData(string ParamName)
  953. {
  954. try
  955. {
  956. Object o = null;
  957. //根据地址读取xml文件
  958. XmlDocument doc = new XmlDocument();
  959. XmlReaderSettings settings = new XmlReaderSettings();
  960. //忽略文档里面的注释
  961. settings.IgnoreComments = true;
  962. XmlReader reader = XmlReader.Create(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml", settings);
  963. doc.Load(reader);
  964. //先得到根节点
  965. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  966. //再由根节点去找制定的节点
  967. XmlNodeList nodeList = rootNode.ChildNodes;
  968. foreach (XmlNode node in nodeList)
  969. {
  970. //找到了这个节点名字
  971. if (node.Name == ParamName)
  972. {
  973. //返回节点的内容
  974. switch (((XmlElement)node).GetAttribute("Type"))
  975. {
  976. case "System.String":
  977. o = node.InnerText;
  978. break;
  979. case "System.Int32":
  980. o = int.Parse(node.InnerText);
  981. break;
  982. case "System.Boolean":
  983. o = node.InnerText == "True" ? true : false;
  984. break;
  985. default:
  986. break;
  987. }
  988. break;
  989. }
  990. }
  991. //关闭reader
  992. reader.Close();
  993. if (o == null)
  994. return "";
  995. else
  996. return o;
  997. }
  998. catch (Exception e)
  999. {
  1000. LogManager.DoLog(e.Message);
  1001. return "";
  1002. }
  1003. }
  1004. public static void SetCacheData(string ParamName, object Value)
  1005. {
  1006. try
  1007. {
  1008. //根据地址读取xml文件
  1009. XmlDocument doc = new XmlDocument();
  1010. XmlReaderSettings settings = new XmlReaderSettings();
  1011. //忽略文档里面的注释
  1012. settings.IgnoreComments = true;
  1013. XmlReader reader = XmlReader.Create(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml", settings);
  1014. doc.Load(reader);
  1015. //先得到根节点
  1016. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1017. //再由根节点去找制定的节点
  1018. XmlNodeList nodeList = rootNode.ChildNodes;
  1019. bool flag = false;
  1020. foreach (XmlNode node in nodeList)
  1021. {
  1022. //找到了这个节点名字
  1023. if (node.Name == ParamName)
  1024. {
  1025. //就直接赋值
  1026. node.InnerText = Value.ToString();
  1027. flag = true;
  1028. }
  1029. }
  1030. //如果没有该节点,就创建节点保存结果
  1031. if (!flag)
  1032. {
  1033. //创建节点
  1034. XmlElement newNode = doc.CreateElement(ParamName);
  1035. XmlAttribute attr = doc.CreateAttribute("Type");
  1036. attr.InnerText = Value.GetType().ToString();
  1037. newNode.InnerText = Value.ToString();
  1038. newNode.SetAttributeNode(attr);
  1039. //讲新建的节点挂到根节点上
  1040. rootNode.AppendChild(newNode);
  1041. }
  1042. //关闭Reader
  1043. reader.Close();
  1044. doc.Save(Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\Log\cacheInfo\cacheInfo.xml");
  1045. }
  1046. catch (Exception e)
  1047. {
  1048. LogManager.DoLog(e.Message);
  1049. }
  1050. }
  1051. public static void ClosePrint(ApplicationClass lbl)
  1052. {
  1053. lblpro = lbl;
  1054. Thread close = new Thread(ClosePrintProcess);
  1055. close.Start();
  1056. }
  1057. static ApplicationClass lblpro;
  1058. private static void ClosePrintProcess()
  1059. {
  1060. try
  1061. {
  1062. lblpro.Quit();
  1063. }
  1064. catch (Exception)
  1065. {
  1066. }
  1067. }
  1068. public static bool connectState(string path)
  1069. {
  1070. return connectState(path, "", "");
  1071. }
  1072. /// <summary>
  1073. /// 连接远程共享文件夹
  1074. /// </summary>
  1075. /// <param name="path">远程共享文件夹的路径</param>
  1076. /// <param name="userName">用户名</param>
  1077. /// <param name="passWord">密码</param>
  1078. /// <returns></returns>
  1079. public static bool connectState(string path, string userName, string passWord)
  1080. {
  1081. bool Flag = false;
  1082. Process proc = new Process();
  1083. try
  1084. {
  1085. proc.StartInfo.FileName = "cmd.exe";
  1086. proc.StartInfo.UseShellExecute = false;
  1087. proc.StartInfo.RedirectStandardInput = true;
  1088. proc.StartInfo.RedirectStandardOutput = true;
  1089. proc.StartInfo.RedirectStandardError = true;
  1090. proc.StartInfo.CreateNoWindow = true;
  1091. proc.Start();
  1092. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  1093. proc.StandardInput.WriteLine(dosLine);
  1094. proc.StandardInput.WriteLine("exit");
  1095. while (!proc.HasExited)
  1096. {
  1097. proc.WaitForExit(1);
  1098. }
  1099. string errormsg = proc.StandardError.ReadToEnd();
  1100. proc.StandardError.Close();
  1101. if (string.IsNullOrEmpty(errormsg))
  1102. {
  1103. Flag = true;
  1104. }
  1105. else
  1106. {
  1107. throw new Exception(errormsg);
  1108. }
  1109. }
  1110. catch (Exception ex)
  1111. {
  1112. throw new Exception(ex.Message);
  1113. }
  1114. finally
  1115. {
  1116. proc.Close();
  1117. proc.Dispose();
  1118. }
  1119. return Flag;
  1120. }
  1121. /// <summary>
  1122. /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  1123. /// </summary>
  1124. /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  1125. /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  1126. /// <param name="fileName">保存文件的名称以及扩展名</param>
  1127. public static void Transport(string src, string dst, string fileName)
  1128. {
  1129. FileStream inFileStream = new FileStream(src, FileMode.Open);
  1130. if (!Directory.Exists(dst))
  1131. {
  1132. Directory.CreateDirectory(dst);
  1133. }
  1134. dst = dst + fileName;
  1135. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  1136. byte[] buf = new byte[inFileStream.Length];
  1137. int byteCount;
  1138. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  1139. {
  1140. outFileStream.Write(buf, 0, byteCount);
  1141. }
  1142. inFileStream.Flush();
  1143. inFileStream.Close();
  1144. outFileStream.Flush();
  1145. outFileStream.Close();
  1146. }
  1147. /// <summary>
  1148. ///
  1149. /// </summary>
  1150. /// <param name="form"></param>
  1151. /// <param name="InOrOut">True表示打开窗体,False表示关闭窗体</param>
  1152. public static void FormStepInOrOut(Form form, bool InOrOut)
  1153. {
  1154. if (InOrOut)
  1155. {
  1156. for (int iNum = 0; iNum <= 10; iNum++)
  1157. {
  1158. //变更窗体的不透明度
  1159. form.Opacity = 0.1 * iNum;
  1160. //暂停
  1161. System.Threading.Thread.Sleep(20);
  1162. }
  1163. }
  1164. else
  1165. {
  1166. for (int iNum = 10; iNum >= 0; iNum--)
  1167. {
  1168. //变更窗体的不透明度
  1169. form.Opacity = 0.1 * iNum;
  1170. //暂停
  1171. System.Threading.Thread.Sleep(20);
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }