1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | using Microsoft.Win32;
|
---|
5 |
|
---|
6 | namespace 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 | try
|
---|
26 | {
|
---|
27 | string previousText = Text;
|
---|
28 | int index = Items.IndexOf(Text);
|
---|
29 | if (index >= 0) Items.RemoveAt(index);
|
---|
30 | Text = previousText;
|
---|
31 | Items.Insert(0, previousText);
|
---|
32 |
|
---|
33 | while (Items.Count > MaxHistoryCount)
|
---|
34 | Items.RemoveAt(Items.Count - 1);
|
---|
35 | }
|
---|
36 | finally
|
---|
37 | {
|
---|
38 | EndUpdate();
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void LoadFromRegistry()
|
---|
44 | {
|
---|
45 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey) ??
|
---|
46 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey);
|
---|
47 |
|
---|
48 | int count = regKey.GetValueInt("Count", 0);
|
---|
49 | BeginUpdate();
|
---|
50 | try
|
---|
51 | {
|
---|
52 | while (Items.Count > count) Items.RemoveAt(Items.Count - 1);
|
---|
53 | while (Items.Count < count) Items.Add("");
|
---|
54 | for (int i = 0; i < count; i++)
|
---|
55 | {
|
---|
56 | Items[i] = (string)regKey.GetValue(i.ToString(), "");
|
---|
57 | }
|
---|
58 | }
|
---|
59 | finally
|
---|
60 | {
|
---|
61 | EndUpdate();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void SaveToRegistry()
|
---|
66 | {
|
---|
67 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey, true) ??
|
---|
68 | Application.UserAppDataRegistry.CreateSubKey(RegSubKey);
|
---|
69 |
|
---|
70 | int i = 0;
|
---|
71 | regKey.SetValue("Count", Items.Count);
|
---|
72 | foreach (var item in Items)
|
---|
73 | {
|
---|
74 | regKey.SetValue(i.ToString(), item);
|
---|
75 | i++;
|
---|
76 | }
|
---|
77 |
|
---|
78 | while (true)
|
---|
79 | {
|
---|
80 | List<string> names = regKey.GetValueNames().ToList();
|
---|
81 | if (names.IndexOf(i.ToString()) != -1)
|
---|
82 | {
|
---|
83 | regKey.DeleteValue(i.ToString());
|
---|
84 | }
|
---|
85 | else break;
|
---|
86 | i++;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|