BaseUtil.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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].Controls.Count > 0)
  573. {
  574. CleanControls(Form.Controls[i].Controls);
  575. }
  576. 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)
  577. Form.Controls[i].Text = "";
  578. if (Form.Controls[i] is DataGridView)
  579. CleanDGVData((DataGridView)Form.Controls[i]);
  580. }
  581. }
  582. public static void CleanControls(ControlCollection collection)
  583. {
  584. for (int i = 0; i < collection.Count; i++)
  585. {
  586. if (collection[i].Controls.Count > 0)
  587. CleanControls(collection[i].Controls);
  588. 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)
  589. collection[i].Text = "";
  590. if (collection[i] is DataGridView)
  591. CleanDGVData((DataGridView)collection[i]);
  592. }
  593. }
  594. public static void CleanControlsText(params Control[] ctl)
  595. {
  596. foreach (Control item in ctl)
  597. item.Text = "";
  598. }
  599. /// <summary>
  600. /// 需要SQL的顺序和DGV的列的顺序一致
  601. /// </summary>
  602. /// <param name="dgv"></param>
  603. /// <param name="dt"></param>
  604. /// <param name="CheckBox"></param>
  605. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  606. {
  607. CleanDGVData(dgv);
  608. if (CheckBox)
  609. {
  610. for (int i = 0; i < dt.Rows.Count; i++)
  611. {
  612. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  613. collapseRow.IsCollapse = true;
  614. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  615. collapseRow.Cells.Add(checkcell);
  616. checkcell.Value = CheckBoxTrue;
  617. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  618. for (int j = 0; j < dt.Columns.Count; j++)
  619. {
  620. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  621. textcell.Value = dt.Rows[i][j].ToString();
  622. collapseRow.Cells.Add(textcell);
  623. textcell.ReadOnly = true;
  624. }
  625. collapseRow.Tag = "MainRow";
  626. dgv.Rows.Add(collapseRow);
  627. }
  628. }
  629. else
  630. {
  631. for (int i = 0; i < dt.Rows.Count; i++)
  632. {
  633. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  634. collapseRow.IsCollapse = true;
  635. for (int j = 1; j <= dt.Columns.Count; j++)
  636. {
  637. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  638. textcell.Value = dt.Rows[i][j - 1].ToString();
  639. collapseRow.Cells.Add(textcell);
  640. }
  641. dgv.Rows.Add(collapseRow);
  642. collapseRow.ReadOnly = true;
  643. }
  644. }
  645. }
  646. /// <summary>
  647. /// 用于给DGV中的Combox列赋静态值
  648. /// </summary>
  649. /// <param name="dgvc"></param>
  650. /// <param name="displayField"></param>
  651. /// <param name="valueField"></param>
  652. /// <param name="Value"></param>
  653. /// <returns></returns>
  654. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  655. {
  656. DataTable dt = new DataTable();
  657. dt.Columns.Add(displayField);
  658. dt.Columns.Add(valueField);
  659. for (int i = 0; i < Value.Length; i++)
  660. {
  661. DataGridViewRow row = new DataGridViewRow();
  662. dt.Rows.Add(row);
  663. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  664. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  665. }
  666. dgvc.DataPropertyName = DataPropertyName;
  667. dgvc.DataSource = dt;
  668. dgvc.DisplayMember = displayField;
  669. dgvc.ValueMember = valueField;
  670. }
  671. /// <summary>
  672. /// 用于给DGV中的ComboxCell赋静态值
  673. /// </summary>
  674. /// <param name="dgvcc"></param>
  675. /// <param name="displayField"></param>
  676. /// <param name="valueField"></param>
  677. /// <param name="Value"></param>
  678. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  679. {
  680. DataTable dt = new DataTable();
  681. dt.Columns.Add(displayField);
  682. dt.Columns.Add(valueField);
  683. for (int i = 0; i < Value.Length; i++)
  684. {
  685. DataRow dr = dt.NewRow();
  686. dr[displayField] = Value[i].Split('#')[0];
  687. dr[valueField] = Value[i].Split('#')[1];
  688. dt.Rows.Add(dr);
  689. }
  690. dgvcc.DisplayMember = displayField;
  691. dgvcc.ValueMember = valueField;
  692. dgvcc.DataSource = dt;
  693. }
  694. /// <summary>
  695. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  696. /// </summary>
  697. /// <param name="SQL"></param>
  698. /// <param name="Condition"></param>
  699. /// <returns></returns>
  700. public static string GetScreenSqlCondition(params Control[] Condition)
  701. {
  702. string condition = "";
  703. //用于统计传入的控件的空值数
  704. int EmptyControlCount = 0;
  705. for (int i = 0; i < Condition.Length; i++)
  706. {
  707. //如果Text不为空再进行条件的拼接
  708. if (Condition[i].Text != "")
  709. {
  710. if (Condition[i] is ComboBox)
  711. {
  712. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  713. }
  714. else
  715. {
  716. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  717. }
  718. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  719. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  720. for (int j = i + 1; j < Condition.Length; j++)
  721. {
  722. if (j < Condition.Length)
  723. {
  724. if (Condition[j].Text != "")
  725. {
  726. condition += " and ";
  727. break;
  728. }
  729. }
  730. }
  731. }
  732. else
  733. {
  734. EmptyControlCount = EmptyControlCount + 1;
  735. }
  736. }
  737. //如果所有的控件传入的都是空值则返回也为空
  738. if (EmptyControlCount == Condition.Length)
  739. return "";
  740. else
  741. condition = " where " + condition;
  742. return condition;
  743. }
  744. public static void CleanDataGridView(DataGridView dgv)
  745. {
  746. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  747. {
  748. dgv.Columns.RemoveAt(i);
  749. }
  750. }
  751. /// <summary>
  752. /// 取出SQL中的参数占位符
  753. /// </summary>
  754. /// <param name="SQL"></param>
  755. /// <returns></returns>
  756. public static string[] GetParamFromSQL(string SQL)
  757. {
  758. string[] par = SQL.Split(':');
  759. //用来存参数的数组
  760. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  761. string[] param = new string[par.Length - 1];
  762. for (int i = 0; i < par.Length - 1; i++)
  763. {
  764. //新建一个char类型的数组用来存储每个字节的变量
  765. char[] c = par[i + 1].ToCharArray();
  766. addpar[i] = new StringBuilder();
  767. for (int j = 0; j < c.Length; j++)
  768. {
  769. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  770. {
  771. addpar[i].Append(c[j]);
  772. }
  773. else
  774. {
  775. break;
  776. }
  777. }
  778. }
  779. for (int i = 0; i < par.Length - 1; i++)
  780. {
  781. param[i] = addpar[i].ToString();
  782. }
  783. return param;
  784. }
  785. public static void SetFormCenter(Form form)
  786. {
  787. form.StartPosition = FormStartPosition.CenterParent;
  788. }
  789. /// <summary>
  790. /// 设置DataGridView的指定列可编辑
  791. /// </summary>
  792. /// <param name="DGV"></param>
  793. /// <param name="EditAbleField"></param>
  794. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  795. {
  796. foreach (DataGridViewColumn dc in DGV.Columns)
  797. {
  798. dc.ReadOnly = true;
  799. foreach (string s in EditAbleField)
  800. {
  801. if (dc.Name.ToLower() == s.ToLower())
  802. {
  803. DGV.Columns[dc.Name].ReadOnly = false;
  804. }
  805. }
  806. }
  807. }
  808. //判断带有CheckBox的DGV是否有项目勾选了
  809. public static DataTable DGVIfChecked(DataGridView dgv)
  810. {
  811. int CheckCount = 0;
  812. DataTable dt = new DataTable();
  813. //第一列是勾选框,排除在循环之外
  814. for (int i = 1; i < dgv.Columns.Count; i++)
  815. {
  816. dt.Columns.Add(dgv.Columns[i].Name);
  817. }
  818. for (int i = 0; i < dgv.RowCount; i++)
  819. {
  820. if (dgv.Rows[i].Cells[0].Value != null)
  821. {
  822. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  823. {
  824. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  825. {
  826. DataRow dr = dt.NewRow();
  827. for (int j = 1; j < dgv.ColumnCount; j++)
  828. {
  829. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  830. }
  831. dt.Rows.Add(dr);
  832. CheckCount++;
  833. }
  834. }
  835. }
  836. }
  837. //判断是否勾选了明细
  838. if (CheckCount == 0)
  839. return null;
  840. return dt;
  841. }
  842. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  843. {
  844. //第一列是勾选框,排除在循环之外
  845. if (dt.Columns.Count == 0)
  846. {
  847. for (int i = 0; i < dgv.Columns.Count; i++)
  848. dt.Columns.Add(dgv.Columns[i].Name);
  849. }
  850. //是展开的子行的数据
  851. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  852. {
  853. DataRow dr = dt.NewRow();
  854. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  855. //判断值是否存在,存在移除重新添加
  856. if (datarow.Length > 0)
  857. {
  858. dt.Rows.Remove(datarow[0]);
  859. }
  860. for (int j = 0; j < dgv.ColumnCount; j++)
  861. {
  862. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  863. }
  864. dt.Rows.Add(dr);
  865. }
  866. }
  867. /// <summary>
  868. /// 设置只允许输入数字
  869. /// </summary>
  870. /// <param name="sender"></param>
  871. /// <param name="e"></param>
  872. public static void NumOnly(object sender, KeyPressEventArgs e)
  873. {
  874. if (e.KeyChar != '\b')//这是允许输入退格键
  875. {
  876. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  877. {
  878. e.Handled = true;
  879. }
  880. }
  881. }
  882. public static string AddField(string[] Fields)
  883. {
  884. string sql = " ";
  885. foreach (string field in Fields)
  886. sql += field + ",";
  887. return sql.Substring(0, sql.Length - 1);
  888. }
  889. /// <summary>
  890. /// 筛选DataTable
  891. /// </summary>
  892. /// <param name="dt"></param>
  893. /// <param name="condition"></param>
  894. /// <returns></returns>
  895. public static DataTable filterDataTable(DataTable dt, String condition)
  896. {
  897. //获取筛选条件中的列名,值
  898. DataRow[] dataRows = dt.Select(condition);
  899. DataTable ndt = dt.Clone();
  900. for (int i = 0; i < dataRows.Length; i++)
  901. {
  902. ndt.Rows.Add(dataRows[i].ItemArray);
  903. }
  904. return ndt;
  905. }
  906. /// <summary>
  907. /// 图表绘制公共方法样本
  908. /// </summary>
  909. /// <param name="chart1"></param>
  910. /// <param name="_dt"></param>
  911. /// <param name="seriesChartType"></param>
  912. /// <param name="_title"></param>
  913. /// <param name="XValueMember"></param>
  914. /// <param name="YValueMembers"></param>
  915. /// <param name="Xname"></param>
  916. /// <param name="Yname"></param>
  917. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  918. {
  919. chart1.Series[0].ChartType = seriesChartType;
  920. chart1.DataSource = _dt;
  921. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  922. chart1.Series[0].MarkerSize = 8;
  923. chart1.Series[0].XValueMember = XValueMember;
  924. chart1.Series[0].YValueMembers = YValueMembers;
  925. chart1.Series[0].Label = "#VAL";
  926. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  927. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  928. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  929. chart1.Series[0].LabelBackColor = Color.Transparent;
  930. chart1.Series[0].LegendText = Yname;
  931. chart1.Legends[0].Title = _title;
  932. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  933. }
  934. /// <summary>
  935. /// 将新增打印进程信息写入静态文件
  936. /// </summary>
  937. /// <param name="lbl"></param>
  938. public static void WriteLbl(ApplicationClass lbl)
  939. {
  940. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  941. Dictionary<string, int> ProInf = new Dictionary<string, int>();
  942. int PID = 0;
  943. for (int i = 0; i < processes.Length; i++)
  944. {
  945. ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  946. }
  947. var temp = ProInf.Keys.Max();
  948. String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  949. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  950. StreamWriter sw = new StreamWriter(fs);
  951. sw.WriteLine(str, Encoding.UTF8);
  952. sw.Close();
  953. fs.Close();
  954. }
  955. public static Object GetCacheData(string ParamName)
  956. {
  957. try
  958. {
  959. Object o = null;
  960. //根据地址读取xml文件
  961. XmlDocument doc = new XmlDocument();
  962. XmlReaderSettings settings = new XmlReaderSettings();
  963. //忽略文档里面的注释
  964. settings.IgnoreComments = true;
  965. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  966. doc.Load(reader);
  967. //先得到根节点
  968. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  969. //再由根节点去找制定的节点
  970. XmlNodeList nodeList = rootNode.ChildNodes;
  971. foreach (XmlNode node in nodeList)
  972. {
  973. //找到了这个节点名字
  974. if (node.Name == ParamName)
  975. {
  976. //返回节点的内容
  977. switch (((XmlElement)node).GetAttribute("Type"))
  978. {
  979. case "System.String":
  980. o = node.InnerText;
  981. break;
  982. case "System.Int32":
  983. o = int.Parse(node.InnerText);
  984. break;
  985. case "System.Boolean":
  986. o = node.InnerText == "True" ? true : false;
  987. break;
  988. default:
  989. break;
  990. }
  991. break;
  992. }
  993. }
  994. //关闭reader
  995. reader.Close();
  996. if (o == null)
  997. return "";
  998. else
  999. return o;
  1000. }
  1001. catch (Exception e)
  1002. {
  1003. LogManager.DoLog(e.Message);
  1004. return "";
  1005. }
  1006. }
  1007. public static void SetCacheData(string ParamName, object Value)
  1008. {
  1009. try
  1010. {
  1011. //根据地址读取xml文件
  1012. XmlDocument doc = new XmlDocument();
  1013. XmlReaderSettings settings = new XmlReaderSettings();
  1014. //忽略文档里面的注释
  1015. settings.IgnoreComments = true;
  1016. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1017. doc.Load(reader);
  1018. //先得到根节点
  1019. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1020. //再由根节点去找制定的节点
  1021. XmlNodeList nodeList = rootNode.ChildNodes;
  1022. bool flag = false;
  1023. foreach (XmlNode node in nodeList)
  1024. {
  1025. //找到了这个节点名字
  1026. if (node.Name == ParamName)
  1027. {
  1028. //就直接赋值
  1029. node.InnerText = Value.ToString();
  1030. flag = true;
  1031. }
  1032. }
  1033. //如果没有该节点,就创建节点保存结果
  1034. if (!flag)
  1035. {
  1036. //创建节点
  1037. XmlElement newNode = doc.CreateElement(ParamName);
  1038. XmlAttribute attr = doc.CreateAttribute("Type");
  1039. attr.InnerText = Value.GetType().ToString();
  1040. newNode.InnerText = Value.ToString();
  1041. newNode.SetAttributeNode(attr);
  1042. //讲新建的节点挂到根节点上
  1043. rootNode.AppendChild(newNode);
  1044. }
  1045. //关闭Reader
  1046. reader.Close();
  1047. doc.Save(SystemInf.CacheFilePath);
  1048. }
  1049. catch (Exception e)
  1050. {
  1051. LogManager.DoLog(e.Message);
  1052. }
  1053. }
  1054. public static void ClosePrint(ApplicationClass lbl)
  1055. {
  1056. lblpro = lbl;
  1057. Thread close = new Thread(ClosePrintProcess);
  1058. close.Start();
  1059. }
  1060. static ApplicationClass lblpro;
  1061. private static void ClosePrintProcess()
  1062. {
  1063. try
  1064. {
  1065. lblpro.Quit();
  1066. }
  1067. catch (Exception)
  1068. {
  1069. }
  1070. }
  1071. public static bool connectState(string path)
  1072. {
  1073. return connectState(path, "", "");
  1074. }
  1075. /// <summary>
  1076. /// 连接远程共享文件夹
  1077. /// </summary>
  1078. /// <param name="path">远程共享文件夹的路径</param>
  1079. /// <param name="userName">用户名</param>
  1080. /// <param name="passWord">密码</param>
  1081. /// <returns></returns>
  1082. public static bool connectState(string path, string userName, string passWord)
  1083. {
  1084. bool Flag = false;
  1085. Process proc = new Process();
  1086. try
  1087. {
  1088. proc.StartInfo.FileName = "cmd.exe";
  1089. proc.StartInfo.UseShellExecute = false;
  1090. proc.StartInfo.RedirectStandardInput = true;
  1091. proc.StartInfo.RedirectStandardOutput = true;
  1092. proc.StartInfo.RedirectStandardError = true;
  1093. proc.StartInfo.CreateNoWindow = true;
  1094. proc.Start();
  1095. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  1096. proc.StandardInput.WriteLine(dosLine);
  1097. proc.StandardInput.WriteLine("exit");
  1098. while (!proc.HasExited)
  1099. {
  1100. proc.WaitForExit(1);
  1101. }
  1102. string errormsg = proc.StandardError.ReadToEnd();
  1103. proc.StandardError.Close();
  1104. if (string.IsNullOrEmpty(errormsg))
  1105. {
  1106. Flag = true;
  1107. }
  1108. else
  1109. {
  1110. throw new Exception(errormsg);
  1111. }
  1112. }
  1113. catch (Exception ex)
  1114. {
  1115. LogManager.DoLog(ex.Message);
  1116. }
  1117. finally
  1118. {
  1119. proc.Close();
  1120. proc.Dispose();
  1121. }
  1122. return Flag;
  1123. }
  1124. /// <summary>
  1125. /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  1126. /// </summary>
  1127. /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  1128. /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  1129. /// <param name="fileName">保存文件的名称以及扩展名</param>
  1130. public static void Transport(string src, string dst, string fileName)
  1131. {
  1132. FileStream inFileStream = new FileStream(src, FileMode.Open);
  1133. if (!Directory.Exists(dst))
  1134. {
  1135. Directory.CreateDirectory(dst);
  1136. }
  1137. dst = dst + fileName;
  1138. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  1139. byte[] buf = new byte[inFileStream.Length];
  1140. int byteCount;
  1141. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  1142. {
  1143. outFileStream.Write(buf, 0, byteCount);
  1144. }
  1145. inFileStream.Flush();
  1146. inFileStream.Close();
  1147. outFileStream.Flush();
  1148. outFileStream.Close();
  1149. }
  1150. /// <summary>
  1151. ///
  1152. /// </summary>
  1153. /// <param name="form"></param>
  1154. /// <param name="InOrOut">True表示打开窗体,False表示关闭窗体</param>
  1155. public static void FormStepInOrOut(Form form, bool InOrOut)
  1156. {
  1157. if (InOrOut)
  1158. {
  1159. for (int iNum = 0; iNum <= 10; iNum++)
  1160. {
  1161. //变更窗体的不透明度
  1162. form.Opacity = 0.1 * iNum;
  1163. //暂停
  1164. System.Threading.Thread.Sleep(20);
  1165. }
  1166. }
  1167. else
  1168. {
  1169. for (int iNum = 10; iNum >= 0; iNum--)
  1170. {
  1171. //变更窗体的不透明度
  1172. form.Opacity = 0.1 * iNum;
  1173. //暂停
  1174. System.Threading.Thread.Sleep(20);
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }