source: Common/RecentFiles.cs

Last change on this file was 14, checked in by chronos, 21 months ago
  • Modified: Various improvements.
File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Windows.Forms;
4using Microsoft.Win32;
5
6namespace Common
7{
8 class RecentFiles
9 {
10 public List<string> Items = new List<string>();
11 public int MaxCount = 10;
12 public event EventHandler Change;
13 public EventArgs e = null;
14
15 public void AddItem(string fileName)
16 {
17 int index = Items.IndexOf(fileName);
18 if (index != -1) Items.RemoveAt(index);
19 Items.Insert(0, fileName);
20 LimitMaxCount();
21 DoChange();
22 }
23
24 public void LimitMaxCount()
25 {
26 while (Items.Count > MaxCount)
27 Items.RemoveAt(Items.Count - 1);
28 }
29
30 public string GetFirstFileName()
31 {
32 if (Items.Count > 0) return Items[0];
33 else return "";
34 }
35
36 private void DoChange()
37 {
38 Change?.Invoke(this, new EventArgs());
39 }
40
41 public void LoadFromRegistry(string regSubKey)
42 {
43 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
44 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
45
46 int count = (int)regKey.GetValue("Count", 0);
47 Items.Clear();
48 for (int i = 0; i < count; i++)
49 {
50 Items.Add((string)regKey.GetValue("FileName" + i, ""));
51 }
52 DoChange();
53 }
54
55 public void SaveToRegistry(string regSubKey)
56 {
57 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true);
58 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
59
60 regKey.SetValue("Count", Items.Count);
61 for (int i = 0; i < Items.Count; i++)
62 {
63 regKey.SetValue("FileName" + i, Items[i]);
64 }
65 }
66
67 public void LoadToMenu(ToolStripMenuItem menuItem, EventHandler handler)
68 {
69 while (menuItem.DropDownItems.Count < Items.Count)
70 {
71 ToolStripMenuItem newItem = new ToolStripMenuItem();
72 newItem.Click += handler;
73 Theme.ApplyTheme(newItem);
74 menuItem.DropDownItems.Add(newItem);
75 }
76
77 while (menuItem.DropDownItems.Count > Items.Count) menuItem.DropDownItems.RemoveAt(menuItem.DropDownItems.Count - 1);
78 for (int i = 0; i < Items.Count; i++)
79 {
80 menuItem.DropDownItems[i].Text = Items[i];
81 }
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.