BaseUtil.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  946. StreamWriter sw = new StreamWriter(fs);
  947. sw.WriteLine(str, Encoding.UTF8);
  948. sw.Close();
  949. fs.Close();
  950. }
  951. public static Object GetCacheData(string ParamName)
  952. {
  953. try
  954. {
  955. Object o = null;
  956. //根据地址读取xml文件
  957. XmlDocument doc = new XmlDocument();
  958. XmlReaderSettings settings = new XmlReaderSettings();
  959. //忽略文档里面的注释
  960. settings.IgnoreComments = true;
  961. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  962. doc.Load(reader);
  963. //先得到根节点
  964. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  965. //再由根节点去找制定的节点
  966. XmlNodeList nodeList = rootNode.ChildNodes;
  967. foreach (XmlNode node in nodeList)
  968. {
  969. //找到了这个节点名字
  970. if (node.Name == ParamName)
  971. {
  972. //返回节点的内容
  973. switch (((XmlElement)node).GetAttribute("Type"))
  974. {
  975. case "System.String":
  976. o = node.InnerText;
  977. break;
  978. case "System.Int32":
  979. o = int.Parse(node.InnerText);
  980. break;
  981. case "System.Boolean":
  982. o = node.InnerText == "True" ? true : false;
  983. break;
  984. default:
  985. break;
  986. }
  987. break;
  988. }
  989. }
  990. //关闭reader
  991. reader.Close();
  992. if (o == null)
  993. return "";
  994. else
  995. return o;
  996. }
  997. catch (Exception e)
  998. {
  999. LogManager.DoLog(e.Message);
  1000. return "";
  1001. }
  1002. }
  1003. public static void SetCacheData(string ParamName, object Value)
  1004. {
  1005. try
  1006. {
  1007. //根据地址读取xml文件
  1008. XmlDocument doc = new XmlDocument();
  1009. XmlReaderSettings settings = new XmlReaderSettings();
  1010. //忽略文档里面的注释
  1011. settings.IgnoreComments = true;
  1012. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1013. doc.Load(reader);
  1014. //先得到根节点
  1015. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1016. //再由根节点去找制定的节点
  1017. XmlNodeList nodeList = rootNode.ChildNodes;
  1018. bool flag = false;
  1019. foreach (XmlNode node in nodeList)
  1020. {
  1021. //找到了这个节点名字
  1022. if (node.Name == ParamName)
  1023. {
  1024. //就直接赋值
  1025. node.InnerText = Value.ToString();
  1026. flag = true;
  1027. }
  1028. }
  1029. //如果没有该节点,就创建节点保存结果
  1030. if (!flag)
  1031. {
  1032. //创建节点
  1033. XmlElement newNode = doc.CreateElement(ParamName);
  1034. XmlAttribute attr = doc.CreateAttribute("Type");
  1035. attr.InnerText = Value.GetType().ToString();
  1036. newNode.InnerText = Value.ToString();
  1037. newNode.SetAttributeNode(attr);
  1038. //讲新建的节点挂到根节点上
  1039. rootNode.AppendChild(newNode);
  1040. }
  1041. //关闭Reader
  1042. reader.Close();
  1043. doc.Save(SystemInf.CacheFilePath);
  1044. }
  1045. catch (Exception e)
  1046. {
  1047. LogManager.DoLog(e.Message);
  1048. }
  1049. }
  1050. public static void ClosePrint(ApplicationClass lbl)
  1051. {
  1052. lblpro = lbl;
  1053. Thread close = new Thread(ClosePrintProcess);
  1054. close.Start();
  1055. }
  1056. static ApplicationClass lblpro;
  1057. private static void ClosePrintProcess()
  1058. {
  1059. try
  1060. {
  1061. lblpro.Quit();
  1062. }
  1063. catch (Exception)
  1064. {
  1065. }
  1066. }
  1067. public static bool connectState(string path)
  1068. {
  1069. return connectState(path, "", "");
  1070. }
  1071. /// <summary>
  1072. /// 连接远程共享文件夹
  1073. /// </summary>
  1074. /// <param name="path">远程共享文件夹的路径</param>
  1075. /// <param name="userName">用户名</param>
  1076. /// <param name="passWord">密码</param>
  1077. /// <returns></returns>
  1078. public static bool connectState(string path, string userName, string passWord)
  1079. {
  1080. bool Flag = false;
  1081. Process proc = new Process();
  1082. try
  1083. {
  1084. proc.StartInfo.FileName = "cmd.exe";
  1085. proc.StartInfo.UseShellExecute = false;
  1086. proc.StartInfo.RedirectStandardInput = true;
  1087. proc.StartInfo.RedirectStandardOutput = true;
  1088. proc.StartInfo.RedirectStandardError = true;
  1089. proc.StartInfo.CreateNoWindow = true;
  1090. proc.Start();
  1091. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  1092. proc.StandardInput.WriteLine(dosLine);
  1093. proc.StandardInput.WriteLine("exit");
  1094. while (!proc.HasExited)
  1095. {
  1096. proc.WaitForExit(1);
  1097. }
  1098. string errormsg = proc.StandardError.ReadToEnd();
  1099. proc.StandardError.Close();
  1100. if (string.IsNullOrEmpty(errormsg))
  1101. {
  1102. Flag = true;
  1103. }
  1104. else
  1105. {
  1106. throw new Exception(errormsg);
  1107. }
  1108. }
  1109. catch (Exception ex)
  1110. {
  1111. LogManager.DoLog(ex.Message);
  1112. }
  1113. finally
  1114. {
  1115. proc.Close();
  1116. proc.Dispose();
  1117. }
  1118. return Flag;
  1119. }
  1120. /// <summary>
  1121. /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  1122. /// </summary>
  1123. /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  1124. /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  1125. /// <param name="fileName">保存文件的名称以及扩展名</param>
  1126. public static void Transport(string src, string dst, string fileName)
  1127. {
  1128. FileStream inFileStream = new FileStream(src, FileMode.Open);
  1129. if (!Directory.Exists(dst))
  1130. {
  1131. Directory.CreateDirectory(dst);
  1132. }
  1133. dst = dst + fileName;
  1134. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  1135. byte[] buf = new byte[inFileStream.Length];
  1136. int byteCount;
  1137. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  1138. {
  1139. outFileStream.Write(buf, 0, byteCount);
  1140. }
  1141. inFileStream.Flush();
  1142. inFileStream.Close();
  1143. outFileStream.Flush();
  1144. outFileStream.Close();
  1145. }
  1146. /// <summary>
  1147. ///
  1148. /// </summary>
  1149. /// <param name="form"></param>
  1150. /// <param name="InOrOut">True表示打开窗体,False表示关闭窗体</param>
  1151. public static void FormStepInOrOut(Form form, bool InOrOut)
  1152. {
  1153. if (InOrOut)
  1154. {
  1155. for (int iNum = 0; iNum <= 10; iNum++)
  1156. {
  1157. //变更窗体的不透明度
  1158. form.Opacity = 0.1 * iNum;
  1159. //暂停
  1160. System.Threading.Thread.Sleep(20);
  1161. }
  1162. }
  1163. else
  1164. {
  1165. for (int iNum = 10; iNum >= 0; iNum--)
  1166. {
  1167. //变更窗体的不透明度
  1168. form.Opacity = 0.1 * iNum;
  1169. //暂停
  1170. System.Threading.Thread.Sleep(20);
  1171. }
  1172. }
  1173. }
  1174. }
  1175. }