How do I create a MRU in V 2010Express C# in the simplest way?

前端 未结 2 602
醉话见心
醉话见心 2021-01-22 02:29

First of all I am a newbie in C# Programming, and I need to create a simple MRU as fast as i could.

Well the thing is I\'ve tried looking at some online examples but how

相关标签:
2条回答
  • 2021-01-22 02:49

    So you want to create a submenu like in the screenshot? For this, you will have to:

    • Store the list of recently-used files somewhere. This could be the registry, or it could just be a simple textfile, which I’ll do now to keep it simple.
    • Learn how to generate menu items at runtime instead of in the designer.

    1. Store the MRU in a file

    You will probably have already declared a private field to contain your MRU, right?

    private List<string> _mru = new List<string>();
    

    Every time someone opens a file, you add this file to the beginning of the MRU, right?

    _mru.Insert(0, fullFilePath);
    

    Now, of course when the application closes, you need to save this MRU to a file. Let’s do that in the Form’s FormClosed event. Double-click the FormClosed event in the properties and write some code which looks somewhat like this:

    var appDataPath = Application.UserAppDataPath;
    var myAppDataPath = Path.Combine(appDataPath, "MyApplication");
    var mruFilePath = Path.Combine(myAppDataPath, "MRU.txt");
    File.WriteAllLines(mruFilePath, _mru);
    

    Now we have saved the MRU in a file. Now obviously when the application starts, we need to load it again, so do something like this in the form’s Load event:

    var appDataPath = Application.UserAppDataPath;
    var myAppDataPath = Path.Combine(appDataPath, "MyApplication");
    var mruFilePath = Path.Combine(myAppDataPath, "MRU.txt");
    if (File.Exists(mruFilePath))
        _mru.AddRange(File.ReadAllLines(mruFilePath));
    

    2. Create the menu items

    Now that _mru contains the file paths that we want in our menu, we need to create a new menu item for each. I’ll be assuming here that you already have a menu item in the File menu (the item called “Most Recently Used” in your screenshot) and that it is called mnuRecentlyUsed, and that we only need to create sub-items:

    foreach (var path in _mru)
    {
        var item = new ToolStripMenuItem(path);
        item.Tag = path;
        item.Click += OpenRecentFile;
        mnuRecentlyUsed.DropDownItems.Add(item);
    }
    

    Now all we need is the method that actually opens a file, which I called OpenRecentFile:

    void OpenRecentFile(object sender, EventArgs e)
    {
        var menuItem = (ToolStripMenuItem) sender;
        var filepath = (string) menuItem.Tag;
    
        // Proceed to open the file
        // ...
    }
    

    Disclaimer

    Please don’t use any of this code unless you understand it and you are sure that it is written to do what you intended. If it needs to do something slightly different, I’m sure you can make the necessary changes yourself.

    Also, I’m sure you will have noticed that the above doesn’t update the sub-menu while the program is running. If you understand how the above code works, then I’m sure you’ll be able to figure out the rest for yourself.

    0 讨论(0)
  • 2021-01-22 02:51

    http://www.codeproject.com/Tips/680088/Recent-Items-Tool-Strip-Menu-Item

    This project does exactly what you want

    0 讨论(0)
提交回复
热议问题