1
+ using ComponentFactory . Krypton . Toolkit ;
2
+ using Microsoft . Win32 ;
3
+ using System ;
4
+ using System . Windows . Forms ;
5
+
6
+ namespace PaletteDesigner . Classes
7
+ {
8
+ /// <summary>
9
+ /// Deals with the back-end logic of a most recently used file <see cref="ToolStripMenuItem"/>.
10
+ /// Adapted from (https://www.codeproject.com/Articles/407513/Add-Most-Recently-Used-Files-MRU-List-to-Windows).
11
+ /// </summary>
12
+ public class MostRecentlyUsedDocumentsManager
13
+ {
14
+ #region Private members
15
+ private bool UseConfirmClearListDialogue ;
16
+
17
+ private string NameOfProgram ;
18
+
19
+ private string SubKeyName ;
20
+
21
+ private string filePath ;
22
+
23
+ private ToolStripMenuItem ParentMenuItem ;
24
+
25
+ private ToolStripMenuItem ClearListItem ;
26
+
27
+ private Action < object , EventArgs > OnRecentFileClick ;
28
+
29
+ private Action < object , EventArgs > OnClearRecentFilesClick ;
30
+
31
+ /// <summary>
32
+ /// Gets or sets the file path.
33
+ /// </summary>
34
+ /// <value>
35
+ /// The file path.
36
+ /// </value>
37
+ public string FilePath
38
+ {
39
+ get
40
+ {
41
+ return filePath ;
42
+ }
43
+
44
+ set
45
+ {
46
+ filePath = value ;
47
+ }
48
+ }
49
+
50
+ /// <summary>
51
+ /// Called when [clear recent files click].
52
+ /// </summary>
53
+ /// <param name="obj">The object.</param>
54
+ /// <param name="evt">The <see cref="EventArgs"/> instance containing the event data.</param>
55
+ private void OnClearRecentFiles_Click ( object obj , EventArgs evt )
56
+ {
57
+ try
58
+ {
59
+ DialogResult result = KryptonMessageBox . Show ( "You are about to clear your recent files list. Do you want to continue?" , "Clear Recent Files" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) ;
60
+
61
+ if ( result == DialogResult . Yes )
62
+ {
63
+ ClearRecentFiles ( ) ;
64
+ }
65
+ }
66
+ catch ( Exception ex )
67
+ {
68
+ Console . WriteLine ( ex . ToString ( ) ) ;
69
+ }
70
+
71
+ if ( OnClearRecentFilesClick != null )
72
+ {
73
+ OnClearRecentFilesClick ( obj , evt ) ;
74
+ }
75
+ }
76
+
77
+ /// <summary>
78
+ /// Clears the recent files.
79
+ /// </summary>
80
+ private void ClearRecentFiles ( )
81
+ {
82
+ try
83
+ {
84
+ RegistryKey rK = Registry . CurrentUser . OpenSubKey ( SubKeyName , true ) ;
85
+
86
+ if ( rK == null )
87
+ {
88
+ return ;
89
+ }
90
+
91
+ string [ ] values = rK . GetValueNames ( ) ;
92
+
93
+ foreach ( string valueName in values )
94
+ {
95
+ rK . DeleteValue ( valueName , true ) ;
96
+ }
97
+
98
+ rK . Close ( ) ;
99
+
100
+ ParentMenuItem . DropDownItems . Clear ( ) ;
101
+
102
+ ParentMenuItem . Enabled = false ;
103
+ }
104
+ catch ( Exception ex )
105
+ {
106
+ KryptonMessageBox . Show ( $ "Error: { ex . Message } ", "Unexpected Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
107
+ }
108
+ }
109
+
110
+ /// <summary>
111
+ /// Refreshes the recent files menu.
112
+ /// </summary>
113
+ private void RefreshRecentFilesMenu ( )
114
+ {
115
+ RegistryKey rK ;
116
+
117
+ string s ;
118
+
119
+ ToolStripItem tSI ;
120
+
121
+ try
122
+ {
123
+ rK = Registry . CurrentUser . OpenSubKey ( this . SubKeyName , false ) ;
124
+
125
+ if ( rK == null )
126
+ {
127
+ ParentMenuItem . Enabled = false ;
128
+
129
+ return ;
130
+ }
131
+ }
132
+ catch ( Exception ex )
133
+ {
134
+ KryptonMessageBox . Show ( $ "Error: { ex . Message } ", "Unexpected Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
135
+
136
+ return ;
137
+ }
138
+
139
+ ParentMenuItem . DropDownItems . Clear ( ) ;
140
+
141
+ string [ ] valueNames = rK . GetValueNames ( ) ;
142
+
143
+ foreach ( string valueName in valueNames )
144
+ {
145
+ s = rK . GetValue ( valueName , null ) as string ;
146
+
147
+ if ( s == null )
148
+ {
149
+ continue ;
150
+ }
151
+
152
+ tSI = ParentMenuItem . DropDownItems . Add ( s ) ;
153
+
154
+ tSI . Click += new EventHandler ( OnRecentFileClick ) ;
155
+ }
156
+
157
+ if ( ParentMenuItem . DropDownItems . Count == 0 )
158
+ {
159
+ ParentMenuItem . Enabled = false ;
160
+
161
+ return ;
162
+ }
163
+
164
+ ParentMenuItem . DropDownItems . Add ( "-" ) ;
165
+
166
+ tSI = ParentMenuItem . DropDownItems . Add ( "&Clear list" ) ;
167
+
168
+ tSI . Click += new EventHandler ( OnClearRecentFiles_Click ) ;
169
+
170
+ ParentMenuItem . Enabled = true ;
171
+ }
172
+ #endregion
173
+
174
+ #region Public members
175
+ /// <summary>
176
+ /// Adds the recent file.
177
+ /// </summary>
178
+ /// <param name="fileNameWithFullPath">The file name with full path.</param>
179
+ public void AddRecentFile ( string fileNameWithFullPath )
180
+ {
181
+ string s ;
182
+
183
+ try
184
+ {
185
+ RegistryKey rK = Registry . CurrentUser . CreateSubKey ( SubKeyName , RegistryKeyPermissionCheck . ReadWriteSubTree ) ;
186
+
187
+ for ( int i = 0 ; true ; i ++ )
188
+ {
189
+ s = rK . GetValue ( i . ToString ( ) , null ) as string ;
190
+
191
+ if ( s == null )
192
+ {
193
+ rK . SetValue ( i . ToString ( ) , fileNameWithFullPath ) ;
194
+
195
+ rK . Close ( ) ;
196
+
197
+ break ;
198
+ }
199
+ else if ( s == fileNameWithFullPath )
200
+ {
201
+ rK . Close ( ) ;
202
+
203
+ break ;
204
+ }
205
+ }
206
+ }
207
+ catch ( Exception ex )
208
+ {
209
+ KryptonMessageBox . Show ( $ "Error: { ex . Message } ", "Unexpected Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
210
+ }
211
+
212
+ RefreshRecentFilesMenu ( ) ;
213
+ }
214
+
215
+
216
+ /// <summary>
217
+ /// Removes the recent file.
218
+ /// </summary>
219
+ /// <param name="fileNameWithFullPath">The file name with full path.</param>
220
+ public void RemoveRecentFile ( string fileNameWithFullPath )
221
+ {
222
+ try
223
+ {
224
+ RegistryKey rK = Registry . CurrentUser . OpenSubKey ( SubKeyName , true ) ;
225
+
226
+ string [ ] valuesNames = rK . GetValueNames ( ) ;
227
+
228
+ foreach ( string valueName in valuesNames )
229
+ {
230
+ if ( ( rK . GetValue ( valueName , null ) as string ) == fileNameWithFullPath )
231
+ {
232
+ rK . DeleteValue ( valueName , true ) ;
233
+
234
+ RefreshRecentFilesMenu ( ) ;
235
+
236
+ break ;
237
+ }
238
+ }
239
+ }
240
+ catch ( Exception ex )
241
+ {
242
+ Console . WriteLine ( ex . ToString ( ) ) ;
243
+ }
244
+
245
+ RefreshRecentFilesMenu ( ) ;
246
+ }
247
+ #endregion
248
+
249
+ #region Constructor
250
+ /// <summary>
251
+ /// Initializes a new instance of the <see cref="MostRecentlyUsedDocumentsManager"/> class.
252
+ /// </summary>
253
+ /// <param name="parentMenuItem">The parent menu item.</param>
254
+ /// <param name="nameOfProgram">The name of program.</param>
255
+ /// <param name="onRecentFileClick">The on recent file click.</param>
256
+ /// <param name="onClearRecentFilesClick">The on clear recent files click.</param>
257
+ /// <param name="useConfirmClearListDialogue">if set to <c>true</c> [use confirm clear list dialogue].</param>
258
+ /// <exception cref="ArgumentException">Bad argument.</exception>
259
+ public MostRecentlyUsedDocumentsManager ( ToolStripMenuItem parentMenuItem , string nameOfProgram , Action < object , EventArgs > onRecentFileClick , Action < object , EventArgs > onClearRecentFilesClick = null , bool useConfirmClearListDialogue = true )
260
+ {
261
+ if ( parentMenuItem == null || onRecentFileClick == null || nameOfProgram == null || nameOfProgram . Length == 0 || nameOfProgram . Contains ( "\\ " ) ) throw new ArgumentException ( "Bad argument." ) ;
262
+
263
+ ParentMenuItem = parentMenuItem ;
264
+
265
+ NameOfProgram = nameOfProgram ;
266
+
267
+ OnRecentFileClick = onRecentFileClick ;
268
+
269
+ OnClearRecentFilesClick = onClearRecentFilesClick ;
270
+
271
+ SubKeyName = string . Format ( $ "Software\\ { NameOfProgram } \\ MRU") ;
272
+
273
+ UseConfirmClearListDialogue = useConfirmClearListDialogue ;
274
+
275
+ RefreshRecentFilesMenu ( ) ;
276
+ }
277
+ #endregion
278
+ }
279
+ }
0 commit comments