using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Microsoft.Win32; namespace Common { public partial class ComboBoxEx : ComboBox { public string regSubKey; public int maxHistoryCount; public ComboBoxEx() { InitializeComponent(); regSubKey = Name; maxHistoryCount = 20; } public void UpdateHistory() { if (Text.Trim() != "") { BeginUpdate(); string previousText = Text; int index = Items.IndexOf(Text); if (index >= 0) Items.RemoveAt(index); Text = previousText; Items.Insert(0, previousText); while (Items.Count > maxHistoryCount) Items.RemoveAt(Items.Count - 1); EndUpdate(); } } public void LoadFromRegistry() { RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey); if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey); int count = regKey.GetValueInt("Count", 0); BeginUpdate(); while (Items.Count > count) Items.RemoveAt(Items.Count - 1); while (Items.Count < count) Items.Add(""); for (int i = 0; i < count; i++) { Items[i] = (string)regKey.GetValue(i.ToString(), ""); } EndUpdate(); } public void SaveToRegistry() { RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true); if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey); int i = 0; regKey.SetValue("Count", Items.Count); foreach (var item in Items) { regKey.SetValue(i.ToString(), item); i++; } while (true) { List names = regKey.GetValueNames().ToList(); if (names.IndexOf(i.ToString()) != -1) { regKey.DeleteValue(i.ToString()); } else break; i++; } } } }