ucStorageWeightKg.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Infragistics.Win.UltraWinEditors;
  11. using Common;
  12. namespace MeterModuleLibrary
  13. {
  14. public partial class ucStorageWeightKg : UserControl
  15. {
  16. public ucStorageWeightKg()
  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 isGreen = true;
  78. /// <summary>
  79. /// 重量稳定
  80. /// </summary>
  81. /// <param name="bGreen"></param>
  82. public void setStable(bool bGreen)
  83. {
  84. //if (!PbCache.isLockWgt)
  85. {
  86. isGreen = bGreen;
  87. string sValue = isGreen ? "green" : "red";
  88. picStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), picStable, new UpdateUIArgs(sValue));
  89. btnStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), btnStable, new UpdateUIArgs(sValue));
  90. }
  91. }
  92. public double getWgt()
  93. {
  94. if (txtWeight.Text.Trim() != "")
  95. {
  96. return Math.Round(Convert.ToDouble(txtWeight.Text.Trim()), 1);
  97. }
  98. else
  99. {
  100. return 0;
  101. }
  102. }
  103. /// <summary>
  104. /// 实时写入重量
  105. /// PbCache.isLockWgt 重量未锁定则可写入,否则调用了也不会写入
  106. /// </summary>
  107. /// <param name="db">重量信息</param>
  108. public void setWgt(double db)
  109. {
  110. //if (!PbCache.isLockWgt)
  111. {
  112. txtWeight.Invoke(new UpdateUIEventHander(UpdateUI_Method), txtWeight, new UpdateUIArgs(Math.Round(db, 1) + ""));
  113. }
  114. }
  115. #endregion
  116. }
  117. }