views:

84

answers:

1

Hi Guys,

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 anyway 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 undestand these stuffs...

KEY_CURRENT_USER\Software\Microsoft\VCExpress\9.0\FileMRUList,

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

+1  A: 

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.

Timwi
Hi Timwi,I would like to check with you if is it possible to like not use the generation of c# language that you are using right now?? Because I'm not very familiar with the version, or if im wrong, I've not used "var" for declaring a variable, neither have i used anything with a underscore, "_", in front of a declared field, so far i guess...I'm really on the basics of C#.
Jonathan Ong
Well, if you don’t want to use `var`, just replace it with the type. For example, `var filepath = (string) menuItem.Tag;` is exactly the same as `string filepath = (string) menuItem.Tag;`. If you are unsure about the type of a variable, just hover the mouse over it. Secondly, the underscore is just part of the name. If you don’t want it, just remove it. I only use it to distinguish fields from local variables, but you don’t have to.
Timwi
Hi, I've read through the guidelines you gave me and i realized that,_mru.InsertAt(0, fullFilePath);should it be mru.Insert(0, fullFilePath);instead...am i right to say so?? Because it don't give me any problem if i used Insert. As for the OpenRecentFile(), do i use the open method like an openFileDialog?
Jonathan Ong
@Jonathan Ong: Frankly, I am not your customer service representative. I believe that I have answered this question, so I would be grateful if you could [accept my answer](http://meta.stackoverflow.com/questions/5234/). If you have further questions, please feel free to post a separate question. I would recommend to you, though, that you *think* a bit about your problems before asking questions: Programming is mostly a thinking exercise. This website doesn’t exist to take the need for thinking away from you.
Timwi
@TimwiI sincerely apologize for all the trouble and thank you for the guidelines. cheers.
Jonathan Ong