source: Common/ComboBoxEx.cs

Last change on this file was 14, checked in by chronos, 21 months ago
  • Modified: Various improvements.
File size: 2.4 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using System.Windows.Forms;
4using Microsoft.Win32;
5
6namespace Common
7{
8 public partial class ComboBoxEx : ComboBox
9 {
10 public string regSubKey;
11 public int maxHistoryCount;
12
13 public ComboBoxEx()
14 {
15 InitializeComponent();
16 regSubKey = Name;
17 maxHistoryCount = 20;
18 }
19
20 public void UpdateHistory()
21 {
22 if (Text.Trim() != "")
23 {
24 BeginUpdate();
25 string previousText = Text;
26 int index = Items.IndexOf(Text);
27 if (index >= 0) Items.RemoveAt(index);
28 Text = previousText;
29 Items.Insert(0, previousText);
30
31 while (Items.Count > maxHistoryCount)
32 Items.RemoveAt(Items.Count - 1);
33 EndUpdate();
34 }
35 }
36
37 public void LoadFromRegistry()
38 {
39 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
40 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
41
42 int count = regKey.GetValueInt("Count", 0);
43 BeginUpdate();
44 while (Items.Count > count) Items.RemoveAt(Items.Count - 1);
45 while (Items.Count < count) Items.Add("");
46 for (int i = 0; i < count; i++)
47 {
48 Items[i] = (string)regKey.GetValue(i.ToString(), "");
49 }
50 EndUpdate();
51 }
52
53 public void SaveToRegistry()
54 {
55 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true);
56 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
57
58 int i = 0;
59 regKey.SetValue("Count", Items.Count);
60 foreach (var item in Items)
61 {
62 regKey.SetValue(i.ToString(), item);
63 i++;
64 }
65
66 while (true)
67 {
68 List<string> names = regKey.GetValueNames().ToList();
69 if (names.IndexOf(i.ToString()) != -1)
70 {
71 regKey.DeleteValue(i.ToString());
72 }
73 else break;
74 i++;
75 }
76 }
77 }
78}
Note: See TracBrowser for help on using the repository browser.