BaseUtil.cs 46 KB

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