In some of our systems dealing with file processing, we often need to record the most recently used files, so that users can quickly open the browsing or edited files, which is common in many software, this article mainly on the WinForm interface menu to implement the "Recent files" dynamic menu processing, Implement a more common function.
In my previous essay, "Text Processing control TX text control use" in the content, I targeted the use of this control to do a comprehensive understanding, found that the case code total this part of the function to achieve "Recently used files" very good, so it was organized, The whole idea as an essay to record, I hope to be helpful to everyone.
1, the menu dynamic Add "recent Files" Introduction
This feature we have seen in many programs, such as in Visual Studio, we can see the dynamic processing of this list.
And in the Word toolbar, the same implementation
The main processing logic of this function is that when we open the file, save the file, or save it as another file, we store the corresponding file path, and when we open the menu, we load it dynamically to generate the relevant menu, once we select one of the files, We will load them into the main interface for display or editing.
2, the menu dynamically added "Recently Used files" Implementation 1) design process
First of all we need to add a placeholder for the menu in the interface, so that we can use this as a benchmark to add the corresponding dynamic menu, as shown in the following design interface
The rest is the code processing, we just mentioned that we need to record files open, save, Save as a few operations of the file, and then stored to use, that is, to store a file path and file title list.
This storage we can through the general processing of the system configuration file implementation, first in the program project solution to find the corresponding settigns.settings file, open after the addition of record object processing, as shown below.
With this, our information store achieves the first step, and what is needed is to manage them through the code.
2) Code implementation process
With the above design process, we have a fixed menu can be used, with a configuration object and the corresponding properties can be stored and loaded processing, then the rest is through the code to link their relationship, the dynamic file list to implement the menu processing.
We define a class and add the corresponding file number size and file list properties, which are used to record and configure the property contents of the file definition, and to store the corresponding menu item object as follows.
Where we need to do a file list processing when the file is open, save, and save, so we need to add a function to append the most recent file to the top of the list (the most recent file list) and to crop more than a specified number of records, as shown below.
/// <summary>///Add a new file path to the top list (in open, Save, Save as Action)/// </summary> Public voidAddrecentfile (stringFilePath) {_filelist.insert (0, FilePath); //search backwards from the last position if a consistent name is found, remove the old record for(inti = _filelist.count-1; i >0; i--) { for(intj =0; J < I; J + +) { if(_filelist[i] = =_filelist[j]) {_filelist.removeat (i); Break; } } } //Finally, keep only the specified number of file lists for(intBynd = _filelist.count-1; Bynd > _nmaxfiles-1; bynd--) {_filelist.removeat (Bynd); } updatemenu ();}
Dynamic Add menu processing, is based on these file list of menu item processing, first empty the old records, and then add a new record, and add corresponding to the event processing.
A maintenance operation that adds a "clear list".
Of course, the opening of the file, we'd better use a status record whether the file has been edited, if edited should prompt the user whether to save the original file.
/// <summary>///Recent file-method menu items/// </summary>voidMenuitem_click (Objectsender, EventArgs e) { if(_bdocumentdirty) {varresult = MessageBox.Show ("need to be saved to"+ Documentfilename +"it?","Tips", Messageboxbuttons.yesnocancel); if(Result = =dialogresult.yes) {filesave (); }} ToolStripMenuItem Item=(ToolStripMenuItem) sender; intpos =item. Getcurrentparent (). Items.indexof (item); if(Pos >=0&& Pos <_filelist.count) {Documentfilename=item. Tag.tostring (); FileOpen (); }}/// <summary>///clears the menu item for the recent menu list/// </summary>voidClearlistitem_click (Objectsender, EventArgs e) {_filelist.clear (); Updatemenu ();}
In the menu entry, we should assign the corresponding menu item to the auxiliary class after the main program is initialized.
// a menu item that specifies "recent files," which makes it easy to dynamically create a document list menu this. menufile_recentfiles;
The whole process is basically done here, and finally we look at the actual effect that meets our expectations.
Dynamically add recent Files menu items in the WinForm interface menu