123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using SAGA.DotNetUtils.Configration;
- namespace SAGA.DotNetUtils.WinForms
- {
- public class ComboBoxEx : ComboBox
- {
- private bool m_AllowFireSelectedIndexChanged;
- private Form m_CacheOwnerForm = null;
- private List<string> m_HistoryItems;
- private const int MaxHistoryCount = 10;
- public ComboBoxEx()
- {
- this.HistoryDataCacheName = null;
- this.NumberOnly = false;
- this.m_AllowFireSelectedIndexChanged = true;
- }
- public void BeginLoad()
- {
- this.m_AllowFireSelectedIndexChanged = false;
- }
- private void CacheHistoryData()
- {
- if ((base.DropDownStyle != ComboBoxStyle.DropDownList) && this.AllowCacheHistoryData)
- {
- string item = string.Empty;
- if (this.NumberOnly && this.Value.HasValue)
- {
- item = this.Value.Value.ToString();
- }
- else
- {
- item = this.Text.Trim();
- }
- if (((item.Length > 0) && (this.StringItems.IndexOf(item) < 0)) && (this.HistoryItems.IndexOf(item) < 0))
- {
- if (this.HistoryItems.Count >= 10)
- {
- this.HistoryItems.RemoveAt(0);
- }
- this.HistoryItems.Add(item);
- }
- }
- }
- public void EndLoad()
- {
- this.m_AllowFireSelectedIndexChanged = true;
- }
- private void LoadHistoryData()
- {
- if ((base.DropDownStyle != ComboBoxStyle.DropDownList) && this.AllowCacheHistoryData)
- {
- Dictionary<string, object> dictionary = this.ConfigProvide.LoadConfigFrom(this.HistoryConfigName);
- if ((dictionary != null) && dictionary.ContainsKey("HistoryItems"))
- {
- string[] collection = dictionary["HistoryItems"] as string[];
- if (collection != null)
- {
- this.HistoryItems.Clear();
- this.HistoryItems.AddRange(collection);
- base.Items.AddRange(collection);
- }
- }
- }
- }
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- if (!this.NumberOnly)
- {
- base.OnKeyPress(e);
- }
- else
- {
- NumbericValidate.OnKeyPress(this, e, this.MinValue, this.MaxValue);
- base.OnKeyPress(e);
- }
- }
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- this.ValidateMaxMinValue();
- this.CacheHistoryData();
- }
- protected override void OnSelectedIndexChanged(EventArgs e)
- {
- if (this.m_AllowFireSelectedIndexChanged)
- {
- base.OnSelectedIndexChanged(e);
- }
- }
- private void SaveHistoryData()
- {
- if (((base.DropDownStyle != ComboBoxStyle.DropDownList) && this.AllowCacheHistoryData) && (this.HistoryItems.Count > 0))
- {
- IConfigProvide configProvide = this.ConfigProvide;
- Dictionary<string, object> configs = new Dictionary<string, object>();
- configs.Add("HistoryItems", this.HistoryItems.ToArray());
- configProvide.SaveConfigTo(this.HistoryConfigName, configs);
- this.HistoryItems.Clear();
- }
- }
- private void ValidateMaxMinValue()
- {
- double? nullable = this.Value;
- if (!nullable.HasValue)
- {
- if (this.MinValue.HasValue)
- {
- this.Text = this.MinValue.Value.ToString();
- return;
- }
- if (this.MaxValue.HasValue)
- {
- this.Text = this.MaxValue.Value.ToString();
- return;
- }
- }
- if (this.MinValue.HasValue && (nullable.Value < this.MinValue.Value))
- {
- this.Text = this.MinValue.Value.ToString();
- }
- else if (this.MaxValue.HasValue && (nullable.Value > this.MaxValue.Value))
- {
- this.Text = this.MaxValue.Value.ToString();
- }
- }
- public bool AllowCacheHistoryData { get; set; }
- public bool AllowFireSelectedIndexChanged
- {
- get
- {
- return this.m_AllowFireSelectedIndexChanged;
- }
- set
- {
- this.m_AllowFireSelectedIndexChanged = value;
- }
- }
- private IConfigProvide ConfigProvide
- {
- get
- {
- IConfigProvide provide = base.FindForm() as IConfigProvide;
- if (provide == null)
- {
- provide = BaseConfigProvide.Default;
- }
- return provide;
- }
- }
- private string HistoryConfigName
- {
- get
- {
- Form cacheOwnerForm = base.FindForm();
- if (cacheOwnerForm == null)
- {
- cacheOwnerForm = this.m_CacheOwnerForm;
- }
- string fullName = cacheOwnerForm.GetType().FullName;
- string historyDataCacheName = this.HistoryDataCacheName;
- if ((historyDataCacheName == null) || (historyDataCacheName.Trim().Length <= 0))
- {
- historyDataCacheName = base.Name + "_History";
- }
- else
- {
- historyDataCacheName = historyDataCacheName + "_History";
- }
- return (fullName + "_" + historyDataCacheName);
- }
- }
- public string HistoryDataCacheName { get; set; }
- private List<string> HistoryItems
- {
- get
- {
- if (this.m_HistoryItems == null)
- {
- this.m_HistoryItems = new List<string>();
- }
- return this.m_HistoryItems;
- }
- }
- public double? MaxValue { get; set; }
- public double? MinValue { get; set; }
- public bool NumberOnly { get; set; }
- private List<string> StringItems
- {
- get
- {
- List<string> list = new List<string>();
- foreach (object obj2 in base.Items)
- {
- list.Add(obj2.ToString());
- }
- return list;
- }
- }
- public double? Value
- {
- get
- {
- double num;
- if (double.TryParse(this.Text.Trim(), out num))
- {
- return new double?(num);
- }
- return null;
- }
- set
- {
- this.Text = value.ToString();
- }
- }
- }
- }
|