ComboBoxMemory.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. namespace TSZ.DotNetDll.WinForms
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. public class ComboBoxMemory : ComboBox
  11. {
  12. private string m_AssemblyPath;
  13. private string m_ItemsFileSaveName;
  14. private bool m_ItemsHadLoad;
  15. private ComboBoxMemoryTextType m_Tsz_TextType;
  16. private string m_TszTextRegexPattern;
  17. private string m_TszTextRegexReplace;
  18. private string m_TszTextValidatePattern;
  19. private string m_TszTextValidateReplace;
  20. private string m_TszValueRegexPattern;
  21. private string m_TszValueRegexReplace;
  22. public ComboBoxMemory()
  23. {
  24. this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
  25. this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
  26. this.m_AssemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(base.GetType()).Location);
  27. this.m_AssemblyPath = this.m_AssemblyPath + @"\ComboBoxMemory\";
  28. this.TszItemsMaxCount = 60;
  29. this.Tsz_TextType = ComboBoxMemoryTextType.任意字符;
  30. }
  31. protected override void Dispose(bool disposing)
  32. {
  33. if (this.m_ItemsHadLoad)
  34. {
  35. string path = this.m_AssemblyPath + this.TszItemsFileSaveName;
  36. string fullName = Directory.GetParent(path).FullName;
  37. if (!Directory.Exists(fullName))
  38. {
  39. Directory.CreateDirectory(fullName);
  40. }
  41. this.ItemsSaveToFile(path);
  42. }
  43. base.Dispose(disposing);
  44. }
  45. private void ItemsListAdd(string text)
  46. {
  47. if ((text != null) && ("" != text))
  48. {
  49. this.Items.Remove(text);
  50. this.Items.Insert(0, text);
  51. this.Text = text;
  52. if (this.Items.Count > this.TszItemsMaxCount)
  53. {
  54. this.Items.RemoveAt(this.Items.Count - 1);
  55. }
  56. }
  57. }
  58. private void ItemsLoadFromFile(string fileFullName)
  59. {
  60. if (File.Exists(fileFullName))
  61. {
  62. this.Items.Clear();
  63. StreamReader reader = new StreamReader(fileFullName);
  64. while (reader.Peek() > -1)
  65. {
  66. this.Items.Add(reader.ReadLine());
  67. }
  68. reader.Close();
  69. }
  70. }
  71. private void ItemsSaveToFile(string fileFullName)
  72. {
  73. StreamWriter writer = new StreamWriter(fileFullName);
  74. for (int i = 0; i < this.Items.Count; i++)
  75. {
  76. writer.WriteLine(this.Items[i].ToString());
  77. }
  78. writer.Close();
  79. }
  80. private string NumbericRegion(string value)
  81. {
  82. double num;
  83. if (!double.TryParse(value.Trim(), out num))
  84. {
  85. num = 0.0;
  86. }
  87. if (this.TszMaxValue.HasValue)
  88. {
  89. num = Math.Min(num, this.TszMaxValue.Value);
  90. }
  91. if (this.TszMinValue.HasValue)
  92. {
  93. num = Math.Max(num, this.TszMinValue.Value);
  94. }
  95. return num.ToString();
  96. }
  97. protected override void OnEnter(EventArgs e)
  98. {
  99. base.OnEnter(e);
  100. if (!this.m_ItemsHadLoad)
  101. {
  102. this.ItemsLoadFromFile(this.m_AssemblyPath + this.TszItemsFileSaveName);
  103. this.m_ItemsHadLoad = true;
  104. }
  105. }
  106. protected override void OnLostFocus(EventArgs e)
  107. {
  108. base.OnLostFocus(e);
  109. this.ItemsListAdd(this.TextDeal(this.Text));
  110. }
  111. private string TextDeal(string value)
  112. {
  113. if (this.TszTextValidatePattern != string.Empty)
  114. {
  115. value = Regex.Replace(value, this.TszTextValidatePattern, this.TszTextValidateReplace);
  116. }
  117. if (this.TszTextRegexPattern != string.Empty)
  118. {
  119. value = Regex.Replace(value, this.TszTextRegexPattern, this.TszTextRegexReplace);
  120. }
  121. if (this.TszIsNumberic)
  122. {
  123. value = this.NumbericRegion(value);
  124. }
  125. return value;
  126. }
  127. [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  128. public AutoCompleteStringCollection AutoCompleteCustomSource
  129. {
  130. get
  131. {
  132. return base.AutoCompleteCustomSource;
  133. }
  134. set
  135. {
  136. base.AutoCompleteCustomSource = value;
  137. }
  138. }
  139. [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  140. public System.Windows.Forms.AutoCompleteMode AutoCompleteMode
  141. {
  142. get
  143. {
  144. return base.AutoCompleteMode;
  145. }
  146. set
  147. {
  148. base.AutoCompleteMode = value;
  149. }
  150. }
  151. [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  152. public System.Windows.Forms.AutoCompleteSource AutoCompleteSource
  153. {
  154. get
  155. {
  156. return base.AutoCompleteSource;
  157. }
  158. set
  159. {
  160. base.AutoCompleteSource = value;
  161. }
  162. }
  163. [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  164. public ComboBox.ObjectCollection Items
  165. {
  166. get
  167. {
  168. return base.Items;
  169. }
  170. }
  171. public override string Text
  172. {
  173. get
  174. {
  175. return base.Text;
  176. }
  177. set
  178. {
  179. value = this.TextDeal(value);
  180. base.Text = value;
  181. }
  182. }
  183. public ComboBoxMemoryTextType Tsz_TextType
  184. {
  185. get
  186. {
  187. return this.m_Tsz_TextType;
  188. }
  189. set
  190. {
  191. RegexAttribute attribute = RegexAttribute.Get(value);
  192. if ((attribute != null) && (value != ComboBoxMemoryTextType.自定义))
  193. {
  194. this.m_TszTextValidatePattern = attribute.ValidatePattern;
  195. this.m_TszTextValidateReplace = attribute.ValidateReplace;
  196. this.m_TszTextRegexPattern = attribute.TextPattern;
  197. this.m_TszTextRegexReplace = attribute.TextReplace;
  198. this.m_TszValueRegexPattern = attribute.ValuePattern;
  199. this.m_TszValueRegexReplace = attribute.ValueReplace;
  200. }
  201. this.m_Tsz_TextType = value;
  202. }
  203. }
  204. public bool TszIsNumberic { get; set; }
  205. public string TszItemsFileSaveName
  206. {
  207. get
  208. {
  209. try
  210. {
  211. return (((this.m_ItemsFileSaveName == null) || ("" == this.m_ItemsFileSaveName)) ? (base.Parent.Name + @"\" + base.Name) : this.m_ItemsFileSaveName);
  212. }
  213. catch
  214. {
  215. return base.Name;
  216. }
  217. }
  218. set
  219. {
  220. this.m_ItemsFileSaveName = value;
  221. }
  222. }
  223. public uint TszItemsMaxCount { get; set; }
  224. public double? TszMaxValue { get; set; }
  225. public double? TszMinValue { get; set; }
  226. public string TszTextRegexPattern
  227. {
  228. get
  229. {
  230. return this.m_TszTextRegexPattern;
  231. }
  232. set
  233. {
  234. if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
  235. {
  236. this.m_TszTextRegexPattern = value;
  237. }
  238. }
  239. }
  240. public string TszTextRegexReplace
  241. {
  242. get
  243. {
  244. return this.m_TszTextRegexReplace;
  245. }
  246. set
  247. {
  248. if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
  249. {
  250. this.m_TszTextRegexReplace = value;
  251. }
  252. }
  253. }
  254. public string TszTextValidatePattern
  255. {
  256. get
  257. {
  258. return this.m_TszTextValidatePattern;
  259. }
  260. set
  261. {
  262. if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
  263. {
  264. this.m_TszTextValidatePattern = value;
  265. }
  266. }
  267. }
  268. public string TszTextValidateReplace
  269. {
  270. get
  271. {
  272. return this.m_TszTextValidateReplace;
  273. }
  274. set
  275. {
  276. if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
  277. {
  278. this.m_TszTextValidateReplace = value;
  279. }
  280. }
  281. }
  282. public string TszValue
  283. {
  284. get
  285. {
  286. return Regex.Replace(this.Text, this.TszValueRegexPattern, this.TszValueRegexReplace);
  287. }
  288. }
  289. public string TszValueRegexPattern
  290. {
  291. get
  292. {
  293. return this.m_TszValueRegexPattern;
  294. }
  295. set
  296. {
  297. if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
  298. {
  299. this.m_TszValueRegexPattern = value;
  300. }
  301. }
  302. }
  303. public string TszValueRegexReplace
  304. {
  305. get
  306. {
  307. return this.m_TszValueRegexReplace;
  308. }
  309. set
  310. {
  311. if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
  312. {
  313. this.m_TszValueRegexReplace = value;
  314. }
  315. }
  316. }
  317. }
  318. }