ComHelper.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. using Core.Mes.Client.Comm.Server;
  2. using CoreFS.CA06;
  3. using Infragistics.Win;
  4. using Infragistics.Win.UltraWinEditors;
  5. using Infragistics.Win.UltraWinGrid;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Windows.Forms;
  15. using System.Xml.Linq;
  16. namespace Core.Mes.Client.Comm
  17. {
  18. public static class ComHelper
  19. {
  20. /// <summary>
  21. /// 不能为空
  22. /// </summary>
  23. public static string ERROR_EMPTY = "不能为空";
  24. /// <summary>
  25. /// 未选择
  26. /// </summary>
  27. public static string ERROR_CHOOSE = "未选择";
  28. /// <summary>
  29. /// 日期错误
  30. /// </summary>
  31. public static string ERROR_DATE = "日期错误";
  32. /// <summary>
  33. /// 应该输入整数
  34. /// </summary>
  35. public static string ERROR_INT = "应该输入整数";
  36. /// <summary>
  37. /// 应该输入非整数或小数
  38. /// </summary>
  39. public static string ERROR_DOUBLE = "应该输入整数或小数";
  40. /// <summary>
  41. /// 读取xml文件返回连接地址
  42. /// </summary>
  43. /// <param name="name">Config节点的name属性值</param>
  44. /// <returns></returns>
  45. public static string GetServiceUrl(string name)
  46. {
  47. string url = "";
  48. var xml = XDocument.Load(Application.StartupPath + "/data/comm.core");
  49. var items = xml.Element("CommunicationConfig").Elements("Config");
  50. for (int i = 0; i < items.Count(); i++)
  51. {
  52. try
  53. {
  54. if (items.ElementAt(i).Attribute("name").Value == name)
  55. {
  56. url = items.ElementAt(i).Element("url").Value.Replace("remoting/HessianRemoting", "service/");
  57. break;
  58. }
  59. }
  60. catch { }
  61. }
  62. return url;
  63. }
  64. /// <summary>
  65. /// 禁用 grid 列头排序
  66. /// </summary>
  67. /// <param name="grid"></param>
  68. public static void GridDisableSortIndicator(UltraGrid grid)
  69. {
  70. foreach (var band in grid.DisplayLayout.Bands)
  71. {
  72. foreach (var col in band.Columns)
  73. {
  74. col.SortIndicator = SortIndicator.Disabled;
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// 获取List数据
  80. /// </summary>
  81. /// <param name="url">接口反问地址</param>
  82. /// <typeparam name="TEntity">实体类</typeparam>
  83. /// <param name="className">服务端接口全名称</param>
  84. /// <param name="methodName">方法名</param>
  85. /// <param name="param">参数</param>
  86. /// <returns></returns>
  87. public static List<TEntity> GetListEntity<TEntity>(string url, string className, string methodName, object[] param) where TEntity : class, new()
  88. {
  89. CoreJsonService service = new CoreJsonService(url);
  90. List<TEntity> list = service.execute<List<TEntity>>(className, methodName, param);
  91. return list;
  92. }
  93. /// <summary>
  94. /// 获取List数据
  95. /// </summary>
  96. /// <param name="url">接口反问地址</param>
  97. /// <typeparam name="TEntity">实体类</typeparam>
  98. /// <param name="className">服务端接口全名称</param>
  99. /// <param name="methodName">方法名</param>
  100. /// <param name="param">参数</param>
  101. /// <returns></returns>
  102. public static TEntity GetEntity<TEntity>(string url, string className, string methodName, object[] param)
  103. {
  104. CoreJsonService service = new CoreJsonService(url);
  105. TEntity entity = service.execute<TEntity>(className, methodName, param);
  106. return entity;
  107. }
  108. /// <summary>
  109. /// 无返回数据
  110. /// </summary>
  111. /// <param name="url">接口访问地址</param>
  112. /// <param name="className">服务端接口全名称</param>
  113. /// <param name="methodName">方法名</param>
  114. /// <param name="param">参数</param>
  115. /// <returns></returns>
  116. public static void RequestServer(string url, string className, string methodName, object[] param)
  117. {
  118. CoreJsonService service = new CoreJsonService(url);
  119. service.execute(className, methodName, param);
  120. }
  121. /// <summary>
  122. /// 获取Text值
  123. /// </summary>
  124. /// <param name="textBox"></param>
  125. /// <returns></returns>
  126. public static string GetText(this TextBox textBox)
  127. {
  128. return string.IsNullOrWhiteSpace(textBox.Text) ? null : textBox.Text.Trim();
  129. }
  130. /// <summary>
  131. /// 判定是否为空
  132. /// </summary>
  133. /// <param name="textBox"></param>
  134. /// <returns></returns>
  135. public static bool IsEmpty(this UltraTextEditor textBox)
  136. {
  137. return string.IsNullOrWhiteSpace(textBox.Text);
  138. }
  139. /// <summary>
  140. /// 获取value
  141. /// </summary>
  142. /// <param name="editor"></param>
  143. /// <returns></returns>
  144. public static DateTime? GetValue(this UltraDateTimeEditor editor)
  145. {
  146. if (editor.Value == null)
  147. return null;
  148. else
  149. return Convert.ToDateTime(editor.Value);
  150. }
  151. /// <summary>
  152. /// 获取 DateTime 值
  153. /// </summary>
  154. /// <param name="editor"></param>
  155. /// <param name="date"></param>
  156. /// <returns></returns>
  157. public static DateTime GetDate(this UltraDateTimeEditor editor)
  158. {
  159. return Convert.ToDateTime(editor.Value);
  160. }
  161. /// <summary>
  162. /// 获取短日期
  163. /// </summary>
  164. /// <param name="date"></param>
  165. /// <returns></returns>
  166. public static DateTime GetDate(this DateTime date)
  167. {
  168. DateTime dateTime = Convert.ToDateTime(date.ToShortDateString());
  169. return dateTime;
  170. }
  171. /// <summary>
  172. /// string 转为int?
  173. /// </summary>
  174. /// <param name="text"></param>
  175. /// <returns></returns>
  176. public static int? ParseInt(this string text)
  177. {
  178. if (string.IsNullOrWhiteSpace(text))
  179. return null;
  180. return Convert.ToInt32(text);
  181. }
  182. /// <summary>
  183. /// int? 转 string
  184. /// </summary>
  185. /// <param name="value"></param>
  186. /// <returns></returns>
  187. public static string ParseStr(this int? value)
  188. {
  189. if (value == null)
  190. return null;
  191. return value.ToString();
  192. }
  193. public static double? ParseDouble(this object value)
  194. {
  195. if (value == null || value is DBNull)
  196. {
  197. return null;
  198. }
  199. try
  200. {
  201. return Convert.ToDouble(value);
  202. }
  203. catch
  204. {
  205. return null;
  206. }
  207. }
  208. public static int? ParseInt(this object value)
  209. {
  210. if (value == null)
  211. {
  212. return null;
  213. }
  214. return Convert.ToInt32(value);
  215. }
  216. /// <summary>
  217. /// double? 转 string
  218. /// </summary>
  219. /// <param name="value"></param>
  220. /// <returns></returns>
  221. public static string ParseStr(this double? value)
  222. {
  223. if (value == null)
  224. return null;
  225. return value.ToString();
  226. }
  227. /// <summary>
  228. /// string 转 double?
  229. /// </summary>
  230. /// <param name="value"></param>
  231. /// <returns></returns>
  232. public static double? ParseDouble(this string value)
  233. {
  234. if (value == null)
  235. return null;
  236. return Convert.ToDouble(value);
  237. }
  238. /// <summary>
  239. /// string 转 float
  240. /// </summary>
  241. /// <param name="value"></param>
  242. /// <returns></returns>
  243. public static float ParseFloat(this string value)
  244. {
  245. return Convert.ToSingle(value);
  246. }
  247. /// <summary>
  248. /// string 转 double
  249. /// </summary>
  250. /// <param name="value"></param>
  251. /// <returns></returns>
  252. public static double ConvertDouble(this string value)
  253. {
  254. if (value == null)
  255. return 0;
  256. return Convert.ToDouble(value);
  257. }
  258. /// <summary>
  259. /// DateTime? 转为 DateTime
  260. /// </summary>
  261. /// <param name="value"></param>
  262. /// <returns></returns>
  263. public static DateTime ParseDate(this DateTime? value)
  264. {
  265. if (value == null)
  266. return DateTime.Now;
  267. return Convert.ToDateTime(value);
  268. }
  269. /// <summary>
  270. /// 尝试转换为日期
  271. /// </summary>
  272. /// <param name="value"></param>
  273. /// <returns></returns>
  274. public static bool TryDate(this string value)
  275. {
  276. if (string.IsNullOrWhiteSpace(value))
  277. return false;
  278. try
  279. {
  280. Convert.ToDateTime(value);
  281. return true;
  282. }
  283. catch { return false; }
  284. }
  285. /// <summary>
  286. /// 尝试转换为小数形式
  287. /// </summary>
  288. /// <param name="value"></param>
  289. /// <returns></returns>
  290. public static bool TryDouble(this string value)
  291. {
  292. if (string.IsNullOrWhiteSpace(value))
  293. return false;
  294. try
  295. {
  296. Convert.ToDouble(value);
  297. return true;
  298. }
  299. catch { return false; }
  300. }
  301. /// <summary>
  302. /// 尝试转换为小数形式
  303. /// </summary>
  304. /// <param name="value"></param>
  305. /// <returns></returns>
  306. public static bool TryDouble(this object value)
  307. {
  308. if (value == null)
  309. return false;
  310. try
  311. {
  312. Convert.ToDouble(value);
  313. return true;
  314. }
  315. catch { return false; }
  316. }
  317. /// <summary>
  318. /// 尝试转换为整数
  319. /// </summary>
  320. /// <param name="value"></param>
  321. /// <returns></returns>
  322. public static bool TryInt(this string value)
  323. {
  324. if (string.IsNullOrWhiteSpace(value))
  325. return false;
  326. try
  327. {
  328. Convert.ToInt32(value);
  329. return true;
  330. }
  331. catch { return false; }
  332. }
  333. /// <summary>
  334. /// 给数字补小数位
  335. /// </summary>
  336. /// <param name="value"></param>
  337. /// <param name="digits"></param>
  338. /// <returns></returns>
  339. public static string AddTail(string value, int digits)
  340. {
  341. if (string.IsNullOrWhiteSpace(value))
  342. {
  343. return value;
  344. }
  345. value = value.Trim(' ');
  346. string[] items = value.Split('.');
  347. string format = ".";
  348. if (items.Length > 2)
  349. {
  350. return value;
  351. }
  352. if (items.Length < 2)
  353. { //刚好为整数
  354. for (int i = 0; i < digits; i++)
  355. {
  356. format += "0";
  357. }
  358. return items[0] + format;
  359. }
  360. if (items[1].Length == digits)
  361. { //刚好够位数
  362. return value;
  363. }
  364. int count = items[1].Length - digits;
  365. if (count < 0)
  366. { //位数不够
  367. format += items[1];
  368. for (int i = 0; i < Math.Abs(count); i++)
  369. {
  370. format += "0";
  371. }
  372. }
  373. else if (count > 0)
  374. { //位数大于需要的位数
  375. format += items[1].Substring(0, digits);
  376. }
  377. return items[0] + format;
  378. }
  379. /// <summary>
  380. /// 获取Text值
  381. /// </summary>
  382. /// <param name="obj"></param>
  383. /// <returns></returns>
  384. public static string GetText(this object obj)
  385. {
  386. return obj == null ? null : obj.ToString().Trim();
  387. }
  388. /// <summary>
  389. /// 获取Text值
  390. /// </summary>
  391. /// <param name="textBox"></param>
  392. /// <returns></returns>
  393. public static string GetText(this UltraTextEditor textBox)
  394. {
  395. return string.IsNullOrWhiteSpace(textBox.Text) ? null : textBox.Text.Trim();
  396. }
  397. /// <summary>
  398. /// 获取value值
  399. /// </summary>
  400. /// <param name="combEditor"></param>
  401. /// <returns></returns>
  402. public static string GetSelectValue(this UltraComboEditor comb)
  403. {
  404. return comb.SelectedIndex < 0 ? null : comb.Value.ToString();
  405. }
  406. /// <summary>
  407. /// 设置选中项
  408. /// </summary>
  409. /// <param name="combEditor"></param>
  410. /// <param name="Value"></param>
  411. /// <returns></returns>
  412. public static void SetSelectValue(this UltraComboEditor comb, object value)
  413. {
  414. if (value == null)
  415. {
  416. comb.SelectedIndex = -1;
  417. return;
  418. }
  419. int index = 0;
  420. foreach (var item in comb.Items)
  421. {
  422. if (item.DataValue.ToString() == value.ToString())
  423. {
  424. comb.SelectedIndex = index;
  425. break;
  426. }
  427. index++;
  428. }
  429. }
  430. /// <summary>
  431. ///为空的时候返回null 不为空返回 toString
  432. /// </summary>
  433. /// <param name="value"></param>
  434. /// <returns></returns>
  435. public static string GetString(this object value)
  436. {
  437. if (value == null || value is DBNull || string.IsNullOrWhiteSpace(value.ToString()))
  438. {
  439. return null;
  440. }
  441. return value.ToString();
  442. }
  443. #region 获取 相关 ValueList 数据
  444. /// <summary>
  445. ///班次
  446. /// </summary>
  447. /// <returns></returns>
  448. public static ValueList List_GetWorkGroup()
  449. {
  450. ValueListItem[] items = new ValueListItem[] {
  451. new ValueListItem("1", "夜班"),
  452. new ValueListItem("2", "白班"),
  453. new ValueListItem("3", "中班")
  454. };
  455. ValueList valueList = new ValueList();
  456. valueList.ValueListItems.AddRange(items);
  457. return valueList;
  458. }
  459. /// <summary>
  460. /// DataTable 转换为valueList数据
  461. /// </summary>
  462. /// <param name="dt">数据表</param>
  463. /// <param name="Value">隐藏值</param>
  464. /// <param name="Text"> 显示值</param>
  465. /// <param name="ob">OB对象</param>
  466. public static ValueList GetValueList(DataTable dt, string value, string text)
  467. {
  468. ValueListItem[] items = new ValueListItem[dt.Rows.Count];
  469. for (int i = 0; i < dt.Rows.Count; i++)
  470. {
  471. items[i] = new ValueListItem(dt.Rows[i][value].ToString(), dt.Rows[i][text].ToString());
  472. }
  473. ValueList list = new ValueList();
  474. list.ValueListItems.AddRange(items);
  475. return list;
  476. }
  477. /// <summary>
  478. /// 获取valueList数据
  479. /// </summary>
  480. /// <param name="methodID">方法名</param>
  481. /// <param name="ob">OB对象</param>
  482. public static DataTable GetDataTable(string methodID, OpeBase ob)
  483. {
  484. DataTable dt = ServerHelper.GetData(methodID, new Object[] { }, ob);
  485. return dt;
  486. }
  487. /// <summary>
  488. /// 获取valueList数据
  489. /// </summary>
  490. /// <param name="methodID">方法名</param>
  491. /// <param name="Text">显示值</param>
  492. /// <param name="Value">隐藏值</param>
  493. /// <param name="ob">OB对象</param>
  494. public static ValueList GetValueList(string methodID, string text, string value, OpeBase ob)
  495. {
  496. DataTable dt = ServerHelper.GetData(methodID, new Object[] { }, ob);
  497. ValueListItem[] items = new ValueListItem[dt.Rows.Count];
  498. for (int i = 0; i < dt.Rows.Count; i++)
  499. {
  500. items[i] = new ValueListItem(dt.Rows[i][value].ToString(), dt.Rows[i][text].ToString());
  501. }
  502. ValueList list = new ValueList();
  503. list.ValueListItems.AddRange(items);
  504. return list;
  505. }
  506. /// <summary>
  507. /// 检验补充项目类型
  508. /// </summary>
  509. /// <returns></returns>
  510. public static DataTable List_MaterialOther()
  511. {
  512. DataTable dt = new DataTable();
  513. dt.Columns.AddRange(new DataColumn[] { new DataColumn("NAME"), new DataColumn("CODE") });
  514. dt.Rows.Add(new object[] { "试验方向", "A" });
  515. dt.Rows.Add(new object[] { "试验温度", "B" });
  516. dt.Rows.Add(new object[] { "试样尺寸", "C" });
  517. dt.Rows.Add(new object[] { "试验位置", "D" });
  518. return dt;
  519. }
  520. #endregion 获取 相关 ValueList 数据
  521. /// <summary>
  522. /// 获取grid 所有勾选的行
  523. /// </summary>
  524. /// <param name="grid"></param>
  525. /// <param name="columnName">复选框所在列名</param>
  526. /// <returns></returns>
  527. public static List<UltraGridRow> UltraGridGetChooseRows(UltraGrid grid, string columnName = "Choose")
  528. {
  529. grid.UpdateData();
  530. List<UltraGridRow> list = new List<UltraGridRow>();
  531. RowsCollection rows = grid.Rows;
  532. foreach (var item in rows)
  533. {
  534. try
  535. {
  536. if (Convert.ToBoolean(item.Cells[columnName].Value) == true && item.Hidden == false && item.IsFilteredOut == false)
  537. {
  538. list.Add(item);
  539. }
  540. }
  541. catch { }
  542. }
  543. return list;
  544. }
  545. /// <summary>
  546. /// 获取grid 所有勾选的行
  547. /// </summary>
  548. /// <param name="grid"></param>
  549. /// <param name="columnName">复选框所在列名</param>
  550. /// <returns></returns>
  551. public static List<UltraGridRow> UltraGridGetCHCRows(UltraGrid grid, string columnName = "CHC")
  552. {
  553. grid.UpdateData();
  554. List<UltraGridRow> list = new List<UltraGridRow>();
  555. RowsCollection rows = grid.Rows;
  556. foreach (var item in rows)
  557. {
  558. try
  559. {
  560. if (Convert.ToBoolean(item.Cells[columnName].Value) == true && item.Hidden == false && item.IsFilteredOut == false)
  561. {
  562. list.Add(item);
  563. }
  564. }
  565. catch { }
  566. }
  567. return list;
  568. }
  569. /// <summary>
  570. /// 获取grid 所有勾选的行
  571. /// </summary>
  572. /// <param name="grid"></param>
  573. /// <param name="columnName">复选框所在列名</param>
  574. /// <returns></returns>
  575. public static List<UltraGridRow> UltraGridGetOtherRows(UltraGrid grid, string columnName)
  576. {
  577. grid.UpdateData();
  578. List<UltraGridRow> list = new List<UltraGridRow>();
  579. RowsCollection rows = grid.Rows;
  580. foreach (var item in rows)
  581. {
  582. try
  583. {
  584. if (Convert.ToBoolean(item.Cells[columnName].Value) == true)
  585. {
  586. list.Add(item);
  587. }
  588. }
  589. catch { }
  590. }
  591. return list;
  592. }
  593. /// <summary>
  594. /// 获取grid 所有勾选的行
  595. /// </summary>
  596. /// <param name="grid"></param>
  597. /// <param name="columnName">复选框所在列名</param>
  598. /// <returns></returns>
  599. public static List<UltraGridRow> UltraGridGetOtherRowsNoHidden(UltraGrid grid, string columnName)
  600. {
  601. grid.UpdateData();
  602. List<UltraGridRow> list = new List<UltraGridRow>();
  603. RowsCollection rows = grid.Rows;
  604. foreach (var item in rows)
  605. {
  606. try
  607. {
  608. if (Convert.ToBoolean(item.Cells[columnName].Value) == true && item.Hidden == false && item.IsFilteredOut == false)
  609. {
  610. list.Add(item);
  611. }
  612. }
  613. catch { }
  614. }
  615. return list;
  616. }
  617. /// <summary>
  618. /// 设置列隐藏(全部)
  619. /// </summary>
  620. /// <param name="grid"></param>
  621. public static void UltraGridColunmsHidden(UltraGrid grid)
  622. {
  623. ColumnsCollection band = grid.DisplayLayout.Bands[0].Columns;
  624. foreach (var bandColunm in band)
  625. {
  626. bandColunm.Hidden = true;
  627. }
  628. }
  629. /// <summary>
  630. /// 清空grid 行
  631. /// </summary>
  632. /// <param name="grid"></param>
  633. public static void UltraGridRowsClear(UltraGrid grid)
  634. {
  635. int count = grid.Rows.Count - 1;
  636. while (count != -1)
  637. {
  638. grid.Rows[count].Delete();
  639. count--;
  640. }
  641. }
  642. /// <summary>
  643. /// 设置Grid 全部列 CheckBox 状态
  644. /// </summary>
  645. /// <param name="grid"></param>
  646. /// <param name="colName">checkBox 列名</param>
  647. /// <param name="isChecked">是否选中</param>
  648. public static void UltraGridSetChecked(UltraGrid grid, string colName, bool isChecked)
  649. {
  650. int count = grid.Rows.Count - 1;
  651. while (count != -1)
  652. {
  653. grid.Rows[count].Cells[colName].Value = isChecked;
  654. count--;
  655. }
  656. }
  657. /// <summary>
  658. /// 设置Grid 单选 适用于单表
  659. /// </summary>
  660. /// <param name="grid"></param>
  661. /// <param name="colName">checkBox 列名</param>
  662. public static void UltraGridSingleChecked(UltraGrid grid, string colName = "Choose")
  663. {
  664. int count = grid.Rows.Count - 1;
  665. while (count != -1)
  666. {
  667. try
  668. {
  669. if (!grid.ActiveRow.Equals(grid.Rows[count]))
  670. {
  671. grid.Rows[count].Cells[colName].Value = false;
  672. }
  673. else
  674. {
  675. grid.Rows[count].Cells[colName].Value = true;
  676. }
  677. }
  678. catch { }
  679. count--;
  680. }
  681. }
  682. /// <summary>
  683. /// 设置Grid 单选
  684. /// </summary>
  685. /// <param name="grid"></param>
  686. /// <param name="row">选中的行</param>
  687. /// <param name="colName">checkBox 列名</param>
  688. public static void UltraGridSingleChecked(UltraGrid grid, UltraGridRow row, string colName = "Choose")
  689. {
  690. int count = grid.Rows.Count - 1;
  691. while (count != -1)
  692. {
  693. try
  694. {
  695. if (!row.Equals(grid.Rows[count]))
  696. {
  697. grid.Rows[count].Cells[colName].Value = false;
  698. }
  699. else
  700. {
  701. grid.Rows[count].Cells[colName].Value = true;
  702. }
  703. }
  704. catch { }
  705. count--;
  706. }
  707. }
  708. /// <summary>
  709. /// 设置单元格内容水平对齐方式
  710. /// </summary>
  711. /// <param name="band"></param>
  712. /// <param name="align"></param>
  713. public static void UltraGirdCellTextHAgin(UltraGridBand band, HAlign align)
  714. {
  715. ColumnsCollection columns = band.Columns;
  716. foreach (var item in columns)
  717. {
  718. item.CellAppearance.TextHAlign = align;
  719. }
  720. }
  721. /// <summary>
  722. ///设置水平对齐方式
  723. /// </summary>
  724. /// <param name="col"></param>
  725. /// <param name="align"></param>
  726. public static void UltraGridColunmHAgin(UltraGridColumn[] col, HAlign align)
  727. {
  728. foreach (var item in col)
  729. {
  730. item.CellAppearance.TextHAlign = align;
  731. }
  732. }
  733. /// <summary>
  734. /// 设置焦点行 背景色、字体颜色
  735. /// </summary>
  736. /// <param name="list"></param>
  737. /// <param name="backColor"></param>
  738. /// <param name="fontColor"></param>
  739. public static void UltraGridSetActiveRowColor(List<UltraGrid> list, Color? backColor, Color? fontColor)
  740. {
  741. foreach (UltraGrid grid in list)
  742. {
  743. if (backColor == null)
  744. grid.DisplayLayout.Override.ActiveRowCellAppearance.BackColor = Color.DeepSkyBlue;
  745. else
  746. grid.DisplayLayout.Override.ActiveRowCellAppearance.BackColor = backColor.Value;
  747. if (fontColor == null)
  748. grid.DisplayLayout.Override.ActiveRowCellAppearance.ForeColor = Color.White;
  749. else
  750. grid.DisplayLayout.Override.ActiveRowCellAppearance.ForeColor = fontColor.Value;
  751. }
  752. }
  753. public static bool getUserRight(String userId, OpeBase ob, String deptId, params string[] remark)
  754. {
  755. DataTable userRemaek = ServerHelper.GetData(
  756. "com.steering.pss.sale.order.CoreUpdateInfo.GetUserRight", new Object[] { userId }, ob);
  757. for (int i = 0; i < remark.Length; i++)
  758. {
  759. if (userRemaek.Rows[0]["remark"].ToString().Contains(remark[i]))
  760. return true;
  761. }
  762. if (deptId == "001")
  763. return true;
  764. else
  765. return false;
  766. }
  767. /// <summary>
  768. /// 反射调用模拟平台打开界面方法
  769. /// </summary>
  770. /// <param name="ParentForm">平台窗体对象</param>
  771. /// <param name="formkey">"命名空间+窗体类名 如:[iCore.Omp.Ai.C_Mes.frmAnneal]"</param>
  772. /// <param name="formName">"界面名称 如:[1#退火炉作业]"</param>
  773. /// <param name="formPath">界面菜单路径 如:[\\板带Mes\\板带二车间\\1#退火炉作业] 可不传,但是如果打开了错误的界面,可用此参数</param>
  774. public static void OpenFromWithToolInfo(Form ParentForm, string formkey, string formName, string formPath = null)
  775. {
  776. Infragistics.Win.UltraWinToolbars.UltraToolbarsManager toolManager = ParentForm.GetType().GetField("uMainMenu", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ParentForm) as Infragistics.Win.UltraWinToolbars.UltraToolbarsManager;
  777. if (toolManager == null) return;
  778. MethodInfo mi = ParentForm.GetType().GetMethod("MainMenu_ToolClick", BindingFlags.NonPublic | BindingFlags.Instance);
  779. if (mi == null) return;
  780. Infragistics.Win.UltraWinToolbars.ToolBase bar = null;
  781. foreach (var item in toolManager.Tools)
  782. {
  783. CoreMenuTag mTag = item.Tag as CoreMenuTag;
  784. if (mTag != null)
  785. {
  786. if (mTag.Key == formkey && mTag.WinCaption == formName && (string.IsNullOrEmpty(formPath) || !string.IsNullOrEmpty(formPath) && mTag.WinPath == formPath))
  787. {
  788. bar = item;
  789. }
  790. }
  791. }
  792. if (bar == null) return;
  793. mi.Invoke(ParentForm, new object[] { toolManager, new Infragistics.Win.UltraWinToolbars.ToolClickEventArgs(bar, null) });
  794. }
  795. /// <summary>
  796. /// 设置过滤框是否显示
  797. /// </summary>
  798. /// <param name="grid"></param>
  799. /// <param name="flag"></param>
  800. public static void setAllowRowFolter(UltraGrid grid, bool flag)
  801. {
  802. if (flag)
  803. {
  804. grid.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
  805. }
  806. else
  807. {
  808. int bNum = grid.DisplayLayout.Bands.Count;
  809. if (bNum > 0)
  810. {
  811. for (int j = 0; j < bNum; j++)
  812. {
  813. grid.DisplayLayout.Bands[j].ColumnFilters.ClearAllFilters();
  814. }
  815. }
  816. grid.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.False;
  817. }
  818. }
  819. public static void ToolBarItemAdd(Form ParentForm, List<structButton> Toolbar, List<structButton> newButtonList, int[] index)
  820. {
  821. bool isRun = false;
  822. if (Toolbar == null) return;
  823. for (int i = 0; i < newButtonList.Count; i++)
  824. {
  825. if (Toolbar.FindIndex(t => t.Key.ToLower() == newButtonList[i].Key.ToLower()) < 0)
  826. {
  827. int insertIndex = index[i] >= 0 ? index[i] : Toolbar.Count;
  828. Toolbar.Insert(insertIndex, newButtonList[i]);
  829. isRun = true;
  830. }
  831. else
  832. continue;
  833. }
  834. if (isRun)
  835. {
  836. //Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea
  837. FrmBase frmBase = ParentForm.GetType().GetField("thisform", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ParentForm) as FrmBase;
  838. if (frmBase != null)
  839. {
  840. foreach (System.Windows.Forms.Control item in frmBase.Controls)
  841. {
  842. if (item.Name == "_FrmBase_Toolbars_Dock_Area_Top")
  843. {
  844. item.Tag = "Remove";
  845. }
  846. }
  847. MethodInfo mi = ParentForm.GetType().GetMethod("GenerateUltraToolBar", BindingFlags.Public | BindingFlags.Instance);
  848. if (mi == null) return;
  849. mi.Invoke(ParentForm, new object[] { frmBase, Toolbar });
  850. //frmBase.Controls["_FrmBase_Toolbars_Dock_Area_Top"].Hide();
  851. foreach (System.Windows.Forms.Control item in frmBase.Controls)
  852. {
  853. if (item.Tag != null && item.Tag.ToString() == "Remove")
  854. {
  855. frmBase.Controls.Remove(item);
  856. }
  857. }
  858. }
  859. }
  860. }
  861. #region UltraComboEditor 下拉框绑定数据
  862. /// <summary>
  863. /// 用于通过服务端方法获取绑定下拉框的数据源
  864. /// </summary>
  865. /// <typeparam name="T">实体类名</typeparam>
  866. /// <param name="bs">所属界面 defaultValue(this)</param>
  867. /// <param name="service">调用服务端方法的路径</param>
  868. /// <param name="method">调用服务端方法的方法名称</param>
  869. /// <param name="obj">调用服务端方法的参数</param>
  870. /// <param name="valueMeber">下拉框value绑定对象的属性名</param>
  871. /// <param name="displayMeber">下拉框text绑定对象的属性名</param>
  872. /// <param name="bandEmpty">是否绑定一个空值</param>
  873. /// <returns></returns>
  874. public static DataTable getDTForAll<T>(FrmBase bs, String service, String method, object[] obj, String valueMeber, String displayMeber, Boolean bandEmpty = true)
  875. {
  876. List<T> info = bs.GetJsonService().execute<List<T>>(service, method, obj);
  877. DataTable dt = new DataTable();
  878. dt.Columns.Add("BASECODE");
  879. dt.Columns.Add("BASENAME");
  880. if (bandEmpty) //如果为TRUE,绑定空值
  881. dt.Rows.Add(null, "");
  882. foreach (T t in info)
  883. dt.Rows.Add(t.GetType().GetProperty(valueMeber).GetValue(t, null), t.GetType().GetProperty(displayMeber).GetValue(t, null));
  884. return dt;
  885. }
  886. /// <summary>
  887. /// 第三方控件UltraComboEditor绑定数据源,实现自动补全
  888. /// </summary>
  889. /// <param name="source"></param>
  890. /// <param name="cbb"></param>
  891. public static void valueToUltraComboEditor(DataTable source, UltraComboEditor cbb)
  892. {
  893. cbb.DataSource = source;
  894. cbb.DisplayMember = "BASENAME";
  895. cbb.ValueMember = "BASECODE";
  896. cbb.SelectedIndex = -1;
  897. //cbb.AutoCompleteMode = Infragistics.Win.AutoCompleteMode.SuggestAppend;
  898. SetComboItemHeight(cbb);
  899. }
  900. /// <summary>
  901. /// 设置UltraComboEditor中的中文和非中文统一高度。
  902. /// </summary>
  903. /// <param name="cmb"></param>
  904. public static void SetComboItemHeight(UltraComboEditor cmb)
  905. {
  906. cmb.Update();
  907. foreach (ValueListItem item in cmb.Items)
  908. {
  909. if (Regex.IsMatch(item.DisplayText.ToString(), @"[\u4e00-\u9fa5]+"))
  910. {
  911. item.Appearance.FontData.SizeInPoints = 9.0F;
  912. }
  913. else
  914. {
  915. item.Appearance.FontData.SizeInPoints = 10.5F;
  916. }
  917. }
  918. }
  919. #endregion
  920. #region 打开其他模块界面
  921. public static void MethodInvoke(FrmBase frmBase, string methodName, object[] obj = null)
  922. {
  923. try
  924. {
  925. MethodInfo mi = frmBase.GetType().GetMethod(methodName);
  926. if (mi != null)
  927. {
  928. mi.Invoke(frmBase, obj);
  929. }
  930. }
  931. catch { }
  932. }
  933. /// <summary>
  934. /// 反射调用模拟平台打开界面方法
  935. /// </summary>
  936. /// <param name="ParentForm">平台窗体对象</param>
  937. /// <param name="formkey">"命名空间+窗体类名 如:[iCore.Omp.Ai.C_Mes.frmAnneal]"</param>
  938. /// <param name="formName">"界面名称 如:[1#退火炉作业]"</param>
  939. /// <param name="formPath">界面菜单路径 如:[\\板带Mes\\板带二车间\\1#退火炉作业] 可不传,但是如果打开了错误的界面,可用此参数</param>
  940. public static FrmBase OpenFromWithToolInfo(Form ParentForm, string formkey, string formName, string formPath = null, string customInfo = "")
  941. {
  942. Infragistics.Win.UltraWinToolbars.UltraToolbarsManager toolManager = ParentForm.GetType().GetField("uMainMenu", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ParentForm) as Infragistics.Win.UltraWinToolbars.UltraToolbarsManager;
  943. if (toolManager == null) return null;
  944. MethodInfo mi = ParentForm.GetType().GetMethod("MainMenu_ToolClick", BindingFlags.NonPublic | BindingFlags.Instance);
  945. if (mi == null) return null;
  946. Infragistics.Win.UltraWinToolbars.ToolBase bar = null;
  947. foreach (var item in toolManager.Tools)
  948. {
  949. CoreMenuTag mTag = item.Tag as CoreMenuTag;
  950. if (mTag != null)
  951. {
  952. if (mTag.Key == formkey && mTag.WinCaption == formName && (string.IsNullOrEmpty(formPath) || !string.IsNullOrEmpty(formPath) && mTag.WinPath == formPath))
  953. {
  954. mTag.CustomInfo = customInfo;
  955. bar = item;
  956. }
  957. }
  958. }
  959. if (bar == null) return null;
  960. mi.Invoke(ParentForm, new object[] { toolManager, new Infragistics.Win.UltraWinToolbars.ToolClickEventArgs(bar, null) });
  961. return ParentForm.ActiveMdiChild as FrmBase;
  962. }
  963. #endregion
  964. /// <summary>
  965. /// 增加按钮
  966. /// </summary>
  967. /// <param name="frmBaseThis"></param>
  968. /// <param name="ParentForm"></param>
  969. /// <param name="Toolbar"></param>
  970. /// <param name="newButtonList"></param>
  971. /// <param name="index"></param>
  972. public static void ToolBarItemAdd(FrmBase frmBaseThis, Form ParentForm, List<structButton> Toolbar, List<structButton> newButtonList, int[] index)
  973. {
  974. bool isRun = false;
  975. if (Toolbar == null) return;
  976. for (int i = 0; i < newButtonList.Count; i++)
  977. {
  978. if (Toolbar.FindIndex(t => t.Key.ToLower() == newButtonList[i].Key.ToLower()) < 0)
  979. {
  980. int insertIndex = index[i] >= 0 ? index[i] : Toolbar.Count;
  981. Toolbar.Insert(insertIndex, newButtonList[i]);
  982. isRun = true;
  983. }
  984. else
  985. continue;
  986. }
  987. if (isRun)
  988. {
  989. //Infragistics.Win.UltraWinToolbars.UltraToolbarsDockArea
  990. FrmBase frmBase = ParentForm.GetType().GetField("thisform", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ParentForm) as FrmBase;
  991. if (frmBase != null && frmBase.winPath == frmBaseThis.winPath)
  992. {
  993. foreach (System.Windows.Forms.Control item in frmBase.Controls)
  994. {
  995. if (item.Name == "_FrmBase_Toolbars_Dock_Area_Top")
  996. {
  997. item.Tag = "Remove";
  998. }
  999. }
  1000. MethodInfo mi = ParentForm.GetType().GetMethod("GenerateUltraToolBar", BindingFlags.Public | BindingFlags.Instance);
  1001. if (mi == null) return;
  1002. mi.Invoke(ParentForm, new object[] { frmBase, Toolbar });
  1003. //frmBase.Controls["_FrmBase_Toolbars_Dock_Area_Top"].Hide();
  1004. foreach (System.Windows.Forms.Control item in frmBase.Controls)
  1005. {
  1006. if (item.Tag != null && item.Tag.ToString() == "Remove")
  1007. {
  1008. frmBase.Controls.Remove(item);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. /// <summary>
  1015. /// 获取 BaseInfo ValueList
  1016. /// </summary>
  1017. /// <param name="list">BaseInfo集</param>
  1018. /// <returns></returns>
  1019. public static ValueList List_GetBaseInfo(DataTable dt)
  1020. {
  1021. ValueListItem[] items = new ValueListItem[dt.Rows.Count];
  1022. for (int i = 0; i < dt.Rows.Count; i++)
  1023. {
  1024. ValueListItem item = new ValueListItem();
  1025. item.DataValue = dt.Rows[i]["BASECODE"].ToString();
  1026. item.DisplayText = dt.Rows[i]["BASENAME"].ToString();
  1027. items[i] = item;
  1028. }
  1029. ValueList valueList = new ValueList();
  1030. valueList.ValueListItems.AddRange(items);
  1031. return valueList;
  1032. }
  1033. }
  1034. }