Add the [recently used files] menu item and Winform menu item dynamically in the winform interface menu.

Source: Internet
Author: User

Add the [recently used files] menu item and Winform menu item dynamically in the winform interface menu.

In some of our systems dealing with file processing, we often need to record recently used files, so that users can quickly open the files they have browsed or edited, this is common in many software scenarios. This article mainly introduces how to process the [recently used files] dynamic menu in the Winform interface menu to implement a more common function.

In my previous article on the use of the Text Processing Control TX Text Control, I made a comprehensive understanding of the use of this Control, I found that the function of this part of the case code implementation [recently used files] is quite good, so I sorted it out and recorded the entire idea as an article, hope to help you.

1. Add the menu to [recently used files]

We have seen this feature in many programs. For example, in Visual Studio, we can see the dynamic processing of this list.

In the Word toolbar, the same implementation is available.

The main processing logic of this function is to record and store the corresponding file path when opening, saving, or saving a file as another file. When we open this menu, load them to dynamically generate related menus. Once we select a file, we can load them to the main interface for display or editing.


2. Add menus to [recently used files]. 1) design and processing process

First, we need to add a menu placeholder in the interface, so that we can use this as a reference to add the corresponding dynamic menu, as shown in the following design interface.

The rest is code processing. As we mentioned just now, we need to record the files for opening, saving, and saving, and then store them for use, that is, a file path and a file Title List are stored.

This storage can be implemented through the general processing of the system configuration file. First, find the corresponding Settigns. settings file in the program project solution, and add the record object after opening it, as shown below.

With this, our information storage is the first step, and we need to manage them through code.


2) code implementation process

With the above design process, we have a fixed menu that can be used, and a configuration object and corresponding attributes that can be stored and loaded for processing, the rest is to associate the relationships between them by code, and implement the menu processing of the dynamic file list.

We define a class and add the corresponding file quantity and file list attributes to record and configure the file definition attributes and store the corresponding menu item objects, as shown below.

We need to record the file list when opening, saving, and saving the file. Therefore, we need to add a function, append the most recent file to the top of the list (the most recent file list), and crop more records than the specified number. The specific operations are as follows.

/// <Summary> /// Add the new file path to the top list (open, save, and save as an operation) /// </summary> public void AddRecentFile (string filePath) {_ fileList. insert (0, filePath); // reverse lookup from the last position. If a consistent name is found, the old record for (int I = _ fileList. count-1; I> 0; I --) {for (int j = 0; j <I; j ++) {if (_ fileList [I] = _ fileList [j]) {_ fileList. removeAt (I); break ;}}// at the end, only the specified number of file lists is retained for (int bynd = _ fileList. count-1; bynd> _ nMaxFiles-1; bynd --) {_ fileList. removeAt (bynd);} UpdateMenu ();}

The process of adding menus dynamically is to process the menu items based on the list of these files. First, clear the old records, add new records, and add corresponding event processing.

Add a maintenance operation to clear the list.

Of course, to open a file, we 'd better use a status record file to check whether it has been edited. If it has been edited, we should prompt you whether to save the original file.

/// <Summary> /// menu item of the latest file method /// </summary> void menuItem_Click (object sender, EventArgs e) {if (_ bDocumentDirty) {var result = MessageBox. show ("need to save to" + DocumentFileName +? "," Prompt ", MessageBoxButtons. yesNoCancel); if (result = DialogResult. yes) {FileSave () ;}} ToolStripMenuItem item = (ToolStripMenuItem) sender; int pos = item. getCurrentParent (). items. indexOf (item); if (pos> = 0 & amp; pos <_ fileList. count) {DocumentFileName = item. tag. toString (); FileOpen () ;}/// <summary> /// clear the menu items in the recent menu list /// </summary> void clearListItem_Click (object sender, eventArgs e) {_ fileList. clear (); UpdateMenu ();}

In the menu entry, we should assign the corresponding menu item to the helper class after the main program initialization.

// Specify the menu item of [recently used files] to facilitate the dynamic creation of the document list menu _ fileHandler. RecentFilesMenu = this. menuFile_RecentFiles;

The whole process is basically completed here. Finally, let's take a look at the actual results, as we expected.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.