123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- namespace TSZ.DotNetDll.WinForms
- {
- using System;
- using System.ComponentModel;
- using System.IO;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- public class ComboBoxMemory : ComboBox
- {
- private string m_AssemblyPath;
- private string m_ItemsFileSaveName;
- private bool m_ItemsHadLoad;
- private ComboBoxMemoryTextType m_Tsz_TextType;
- private string m_TszTextRegexPattern;
- private string m_TszTextRegexReplace;
- private string m_TszTextValidatePattern;
- private string m_TszTextValidateReplace;
- private string m_TszValueRegexPattern;
- private string m_TszValueRegexReplace;
- public ComboBoxMemory()
- {
- this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
- this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
- this.m_AssemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(base.GetType()).Location);
- this.m_AssemblyPath = this.m_AssemblyPath + @"\ComboBoxMemory\";
- this.TszItemsMaxCount = 60;
- this.Tsz_TextType = ComboBoxMemoryTextType.任意字符;
- }
- protected override void Dispose(bool disposing)
- {
- if (this.m_ItemsHadLoad)
- {
- string path = this.m_AssemblyPath + this.TszItemsFileSaveName;
- string fullName = Directory.GetParent(path).FullName;
- if (!Directory.Exists(fullName))
- {
- Directory.CreateDirectory(fullName);
- }
- this.ItemsSaveToFile(path);
- }
- base.Dispose(disposing);
- }
- private void ItemsListAdd(string text)
- {
- if ((text != null) && ("" != text))
- {
- this.Items.Remove(text);
- this.Items.Insert(0, text);
- this.Text = text;
- if (this.Items.Count > this.TszItemsMaxCount)
- {
- this.Items.RemoveAt(this.Items.Count - 1);
- }
- }
- }
- private void ItemsLoadFromFile(string fileFullName)
- {
- if (File.Exists(fileFullName))
- {
- this.Items.Clear();
- StreamReader reader = new StreamReader(fileFullName);
- while (reader.Peek() > -1)
- {
- this.Items.Add(reader.ReadLine());
- }
- reader.Close();
- }
- }
- private void ItemsSaveToFile(string fileFullName)
- {
- StreamWriter writer = new StreamWriter(fileFullName);
- for (int i = 0; i < this.Items.Count; i++)
- {
- writer.WriteLine(this.Items[i].ToString());
- }
- writer.Close();
- }
- private string NumbericRegion(string value)
- {
- double num;
- if (!double.TryParse(value.Trim(), out num))
- {
- num = 0.0;
- }
- if (this.TszMaxValue.HasValue)
- {
- num = Math.Min(num, this.TszMaxValue.Value);
- }
- if (this.TszMinValue.HasValue)
- {
- num = Math.Max(num, this.TszMinValue.Value);
- }
- return num.ToString();
- }
- protected override void OnEnter(EventArgs e)
- {
- base.OnEnter(e);
- if (!this.m_ItemsHadLoad)
- {
- this.ItemsLoadFromFile(this.m_AssemblyPath + this.TszItemsFileSaveName);
- this.m_ItemsHadLoad = true;
- }
- }
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- this.ItemsListAdd(this.TextDeal(this.Text));
- }
- private string TextDeal(string value)
- {
- if (this.TszTextValidatePattern != string.Empty)
- {
- value = Regex.Replace(value, this.TszTextValidatePattern, this.TszTextValidateReplace);
- }
- if (this.TszTextRegexPattern != string.Empty)
- {
- value = Regex.Replace(value, this.TszTextRegexPattern, this.TszTextRegexReplace);
- }
- if (this.TszIsNumberic)
- {
- value = this.NumbericRegion(value);
- }
- return value;
- }
- [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
- public AutoCompleteStringCollection AutoCompleteCustomSource
- {
- get
- {
- return base.AutoCompleteCustomSource;
- }
- set
- {
- base.AutoCompleteCustomSource = value;
- }
- }
- [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
- public System.Windows.Forms.AutoCompleteMode AutoCompleteMode
- {
- get
- {
- return base.AutoCompleteMode;
- }
- set
- {
- base.AutoCompleteMode = value;
- }
- }
- [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
- public System.Windows.Forms.AutoCompleteSource AutoCompleteSource
- {
- get
- {
- return base.AutoCompleteSource;
- }
- set
- {
- base.AutoCompleteSource = value;
- }
- }
- [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
- public ComboBox.ObjectCollection Items
- {
- get
- {
- return base.Items;
- }
- }
- public override string Text
- {
- get
- {
- return base.Text;
- }
- set
- {
- value = this.TextDeal(value);
- base.Text = value;
- }
- }
- public ComboBoxMemoryTextType Tsz_TextType
- {
- get
- {
- return this.m_Tsz_TextType;
- }
- set
- {
- RegexAttribute attribute = RegexAttribute.Get(value);
- if ((attribute != null) && (value != ComboBoxMemoryTextType.自定义))
- {
- this.m_TszTextValidatePattern = attribute.ValidatePattern;
- this.m_TszTextValidateReplace = attribute.ValidateReplace;
- this.m_TszTextRegexPattern = attribute.TextPattern;
- this.m_TszTextRegexReplace = attribute.TextReplace;
- this.m_TszValueRegexPattern = attribute.ValuePattern;
- this.m_TszValueRegexReplace = attribute.ValueReplace;
- }
- this.m_Tsz_TextType = value;
- }
- }
- public bool TszIsNumberic { get; set; }
- public string TszItemsFileSaveName
- {
- get
- {
- try
- {
- return (((this.m_ItemsFileSaveName == null) || ("" == this.m_ItemsFileSaveName)) ? (base.Parent.Name + @"\" + base.Name) : this.m_ItemsFileSaveName);
- }
- catch
- {
- return base.Name;
- }
- }
- set
- {
- this.m_ItemsFileSaveName = value;
- }
- }
- public uint TszItemsMaxCount { get; set; }
- public double? TszMaxValue { get; set; }
- public double? TszMinValue { get; set; }
- public string TszTextRegexPattern
- {
- get
- {
- return this.m_TszTextRegexPattern;
- }
- set
- {
- if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
- {
- this.m_TszTextRegexPattern = value;
- }
- }
- }
- public string TszTextRegexReplace
- {
- get
- {
- return this.m_TszTextRegexReplace;
- }
- set
- {
- if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
- {
- this.m_TszTextRegexReplace = value;
- }
- }
- }
- public string TszTextValidatePattern
- {
- get
- {
- return this.m_TszTextValidatePattern;
- }
- set
- {
- if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
- {
- this.m_TszTextValidatePattern = value;
- }
- }
- }
- public string TszTextValidateReplace
- {
- get
- {
- return this.m_TszTextValidateReplace;
- }
- set
- {
- if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
- {
- this.m_TszTextValidateReplace = value;
- }
- }
- }
- public string TszValue
- {
- get
- {
- return Regex.Replace(this.Text, this.TszValueRegexPattern, this.TszValueRegexReplace);
- }
- }
- public string TszValueRegexPattern
- {
- get
- {
- return this.m_TszValueRegexPattern;
- }
- set
- {
- if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
- {
- this.m_TszValueRegexPattern = value;
- }
- }
- }
- public string TszValueRegexReplace
- {
- get
- {
- return this.m_TszValueRegexReplace;
- }
- set
- {
- if (ComboBoxMemoryTextType.自定义 == this.Tsz_TextType)
- {
- this.m_TszValueRegexReplace = value;
- }
- }
- }
- }
- }
|