ucWeightT.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Common;
  11. using Infragistics.Win.UltraWinEditors;
  12. namespace MeterModuleLibrary
  13. {
  14. public partial class ucWeightT : UserControl
  15. {
  16. public ucWeightT()
  17. {
  18. InitializeComponent();
  19. }
  20. #region 线程中赋值的控件需使用委托
  21. private delegate void UpdateUIEventHander(object sender, UpdateUIArgs args); //自定义事件用来从线程中更新控件的值
  22. public class UpdateUIArgs : EventArgs
  23. {
  24. public string textValue { get; private set; }
  25. public UpdateUIArgs(string textValue)
  26. {
  27. this.textValue = textValue;
  28. }
  29. }
  30. private void UpdateUI_Method(object sender, UpdateUIArgs args)
  31. {
  32. if(sender is UltraTextEditor)
  33. {
  34. if (args.textValue == "" || args.textValue == null)
  35. {
  36. ((UltraTextEditor)sender).Text = "0";
  37. }
  38. else
  39. {
  40. ((UltraTextEditor)sender).Text = args.textValue;
  41. }
  42. }
  43. else if (sender is Button)
  44. {
  45. if (args.textValue == "red")
  46. {
  47. ((Button)sender).BackColor = Color.Red;
  48. ((Button)sender).ForeColor = Color.White;
  49. }
  50. else if (args.textValue == "green")
  51. {
  52. ((Button)sender).BackColor = Color.White;
  53. ((Button)sender).ForeColor = Color.Black;
  54. }
  55. else
  56. {
  57. ((Label)sender).Text = args.textValue;
  58. }
  59. }
  60. else if (sender is PictureBox)
  61. {
  62. if (args.textValue == "red")
  63. {
  64. ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\red.gif");
  65. }
  66. else
  67. {
  68. ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\green.gif");
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// 记录当前的颜色,在线程中窑调用下面的setControl方法前
  74. /// 可先判断当前的颜色与显示颜色是否一致
  75. /// 若一致则可不调用setControl,不一致再调用
  76. /// </summary>
  77. public bool isGreenWgt = true;
  78. public bool isGrennExceed = true;
  79. /// <summary>
  80. /// 重量稳定
  81. /// </summary>
  82. /// <param name="bGreen"></param>
  83. public void setStable(bool bGreen)
  84. {
  85. if (!PbCache.isLockWgt)
  86. {
  87. if (picStable.IsHandleCreated)
  88. {
  89. isGreenWgt = bGreen;
  90. string sValue = isGreenWgt ? "green" : "red";
  91. picStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), picStable, new UpdateUIArgs(sValue));
  92. btnStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), btnStable, new UpdateUIArgs(sValue));
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 超量程
  98. /// </summary>
  99. /// <param name="bGreen">true为绿色,false红色警告</param>
  100. public void setExceed(bool bGreen)
  101. {
  102. if (!PbCache.isLockWgt)
  103. {
  104. if (picExceed.IsHandleCreated)
  105. {
  106. isGrennExceed = bGreen;
  107. string sValue = isGrennExceed ? "green" : "red";
  108. picExceed.Invoke(new UpdateUIEventHander(UpdateUI_Method), picExceed, new UpdateUIArgs(sValue));
  109. btnExceedRange.Invoke(new UpdateUIEventHander(UpdateUI_Method), btnExceedRange, new UpdateUIArgs(sValue));
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 实时写入重量
  115. /// PbCache.isLockWgt 重量未锁定则可写入,否则调用了也不会写入
  116. /// </summary>
  117. /// <param name="db">重量信息</param>
  118. public void setWgt(double db)
  119. {
  120. if (!PbCache.isLockWgt)
  121. {
  122. if (txtWeight.IsHandleCreated)
  123. {
  124. txtWeight.Invoke(new UpdateUIEventHander(UpdateUI_Method), txtWeight, new UpdateUIArgs(db + ""));
  125. }
  126. }
  127. }
  128. #endregion
  129. }
  130. }