ucStorageWeightT.cs 3.8 KB

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