开发者

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

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 however I found them to be quite a bit too confusing...

So is there anywa开发者_如何学编程y that anyone can create a "Recently Used" section in the toolstripmenuitem without going into those complicated codes??

E.g I will not be able to understand this stuff...

Registry key:

KEY_CURRENT_USER\Software\Microsoft\VCExpress\9.0\FileMRUList

Code:

Application.UserAppDataRegistry.DeleteSubKey("MRU", false);
RegistryKey appKey = Application.UserAppDataRegistry.CreateSubKey("MRU");

dictionary

microsoft.win32

I will only need something as simple as shown in this link below http://www.codeproject.com/KB/menus/MRUHandler.aspx


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.


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

This project does exactly what you want

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜