ComboBoxEx.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using SAGA.DotNetUtils.Configration;
  5. namespace SAGA.DotNetUtils.WinForms
  6. {
  7. public class ComboBoxEx : ComboBox
  8. {
  9. private bool m_AllowFireSelectedIndexChanged;
  10. private Form m_CacheOwnerForm = null;
  11. private List<string> m_HistoryItems;
  12. private const int MaxHistoryCount = 10;
  13. public ComboBoxEx()
  14. {
  15. this.HistoryDataCacheName = null;
  16. this.NumberOnly = false;
  17. this.m_AllowFireSelectedIndexChanged = true;
  18. }
  19. public void BeginLoad()
  20. {
  21. this.m_AllowFireSelectedIndexChanged = false;
  22. }
  23. private void CacheHistoryData()
  24. {
  25. if ((base.DropDownStyle != ComboBoxStyle.DropDownList) && this.AllowCacheHistoryData)
  26. {
  27. string item = string.Empty;
  28. if (this.NumberOnly && this.Value.HasValue)
  29. {
  30. item = this.Value.Value.ToString();
  31. }
  32. else
  33. {
  34. item = this.Text.Trim();
  35. }
  36. if (((item.Length > 0) && (this.StringItems.IndexOf(item) < 0)) && (this.HistoryItems.IndexOf(item) < 0))
  37. {
  38. if (this.HistoryItems.Count >= 10)
  39. {
  40. this.HistoryItems.RemoveAt(0);
  41. }
  42. this.HistoryItems.Add(item);
  43. }
  44. }
  45. }
  46. public void EndLoad()
  47. {
  48. this.m_AllowFireSelectedIndexChanged = true;
  49. }
  50. private void LoadHistoryData()
  51. {
  52. if ((base.DropDownStyle != ComboBoxStyle.DropDownList) && this.AllowCacheHistoryData)
  53. {
  54. Dictionary<string, object> dictionary = this.ConfigProvide.LoadConfigFrom(this.HistoryConfigName);
  55. if ((dictionary != null) && dictionary.ContainsKey("HistoryItems"))
  56. {
  57. string[] collection = dictionary["HistoryItems"] as string[];
  58. if (collection != null)
  59. {
  60. this.HistoryItems.Clear();
  61. this.HistoryItems.AddRange(collection);
  62. base.Items.AddRange(collection);
  63. }
  64. }
  65. }
  66. }
  67. protected override void OnKeyPress(KeyPressEventArgs e)
  68. {
  69. if (!this.NumberOnly)
  70. {
  71. base.OnKeyPress(e);
  72. }
  73. else
  74. {
  75. NumbericValidate.OnKeyPress(this, e, this.MinValue, this.MaxValue);
  76. base.OnKeyPress(e);
  77. }
  78. }
  79. protected override void OnLostFocus(EventArgs e)
  80. {
  81. base.OnLostFocus(e);
  82. this.ValidateMaxMinValue();
  83. this.CacheHistoryData();
  84. }
  85. protected override void OnSelectedIndexChanged(EventArgs e)
  86. {
  87. if (this.m_AllowFireSelectedIndexChanged)
  88. {
  89. base.OnSelectedIndexChanged(e);
  90. }
  91. }
  92. private void SaveHistoryData()
  93. {
  94. if (((base.DropDownStyle != ComboBoxStyle.DropDownList) && this.AllowCacheHistoryData) && (this.HistoryItems.Count > 0))
  95. {
  96. IConfigProvide configProvide = this.ConfigProvide;
  97. Dictionary<string, object> configs = new Dictionary<string, object>();
  98. configs.Add("HistoryItems", this.HistoryItems.ToArray());
  99. configProvide.SaveConfigTo(this.HistoryConfigName, configs);
  100. this.HistoryItems.Clear();
  101. }
  102. }
  103. private void ValidateMaxMinValue()
  104. {
  105. double? nullable = this.Value;
  106. if (!nullable.HasValue)
  107. {
  108. if (this.MinValue.HasValue)
  109. {
  110. this.Text = this.MinValue.Value.ToString();
  111. return;
  112. }
  113. if (this.MaxValue.HasValue)
  114. {
  115. this.Text = this.MaxValue.Value.ToString();
  116. return;
  117. }
  118. }
  119. if (this.MinValue.HasValue && (nullable.Value < this.MinValue.Value))
  120. {
  121. this.Text = this.MinValue.Value.ToString();
  122. }
  123. else if (this.MaxValue.HasValue && (nullable.Value > this.MaxValue.Value))
  124. {
  125. this.Text = this.MaxValue.Value.ToString();
  126. }
  127. }
  128. public bool AllowCacheHistoryData { get; set; }
  129. public bool AllowFireSelectedIndexChanged
  130. {
  131. get
  132. {
  133. return this.m_AllowFireSelectedIndexChanged;
  134. }
  135. set
  136. {
  137. this.m_AllowFireSelectedIndexChanged = value;
  138. }
  139. }
  140. private IConfigProvide ConfigProvide
  141. {
  142. get
  143. {
  144. IConfigProvide provide = base.FindForm() as IConfigProvide;
  145. if (provide == null)
  146. {
  147. provide = BaseConfigProvide.Default;
  148. }
  149. return provide;
  150. }
  151. }
  152. private string HistoryConfigName
  153. {
  154. get
  155. {
  156. Form cacheOwnerForm = base.FindForm();
  157. if (cacheOwnerForm == null)
  158. {
  159. cacheOwnerForm = this.m_CacheOwnerForm;
  160. }
  161. string fullName = cacheOwnerForm.GetType().FullName;
  162. string historyDataCacheName = this.HistoryDataCacheName;
  163. if ((historyDataCacheName == null) || (historyDataCacheName.Trim().Length <= 0))
  164. {
  165. historyDataCacheName = base.Name + "_History";
  166. }
  167. else
  168. {
  169. historyDataCacheName = historyDataCacheName + "_History";
  170. }
  171. return (fullName + "_" + historyDataCacheName);
  172. }
  173. }
  174. public string HistoryDataCacheName { get; set; }
  175. private List<string> HistoryItems
  176. {
  177. get
  178. {
  179. if (this.m_HistoryItems == null)
  180. {
  181. this.m_HistoryItems = new List<string>();
  182. }
  183. return this.m_HistoryItems;
  184. }
  185. }
  186. public double? MaxValue { get; set; }
  187. public double? MinValue { get; set; }
  188. public bool NumberOnly { get; set; }
  189. private List<string> StringItems
  190. {
  191. get
  192. {
  193. List<string> list = new List<string>();
  194. foreach (object obj2 in base.Items)
  195. {
  196. list.Add(obj2.ToString());
  197. }
  198. return list;
  199. }
  200. }
  201. public double? Value
  202. {
  203. get
  204. {
  205. double num;
  206. if (double.TryParse(this.Text.Trim(), out num))
  207. {
  208. return new double?(num);
  209. }
  210. return null;
  211. }
  212. set
  213. {
  214. this.Text = value.ToString();
  215. }
  216. }
  217. }
  218. }