如何以最简单的方式在V 2010Express C#中创建MRU? [英] How do I create a MRU in V 2010Express C# in the simplest way?

查看:105
本文介绍了如何以最简单的方式在V 2010Express C#中创建MRU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是C#编程的新手,我需要尽快创建一个简单的MRU。

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 understand this stuff...

注册表项:

KEY_CURRENT_USER\Software\Microsoft\VCExpress\9.0\FileMRUList

代码:

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

dictionary

microsoft.win32

I只需 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:


  • 将最近使用的文件列表存储在某个位置。这可能是注册表,也可能只是一个简单的文本文件,为了简化起见,我现在要做。

  • 了解如何在运行时而不是在设计器中生成菜单项

您可能已经声明了一个私有字段来包含您的MRU,对吧?

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

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

每次有人打开文件时,都将此文件添加到MRU的开头,对吗?

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

_mru.Insert(0, fullFilePath);

现在,当然,当应用程序关闭时,您需要将此MRU保存到文件中。让我们在Form的 FormClosed 事件中进行操作。双击属性中的 FormClosed 事件,然后编写一些类似于以下内容的代码:

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);

现在,我们已将MRU保存在文件中。现在很明显,当应用程序启动时,我们需要再次加载它,因此在表单的 Load 事件中执行以下操作:

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。创建菜单项



现在 _mru 包含我们想要的菜单中的文件路径,我们需要创建每个菜单项都有一个新菜单项。我在这里假设您在 File 菜单中已经有一个菜单项(屏幕快照中的 Most最近使用项),并且它被称为 mnuRecentlyUsed ,我们只需要创建以下子项:

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);
}

现在我们需要的是实际打开文件的方法,我称之为 OpenRecentFile

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.

这篇关于如何以最简单的方式在V 2010Express C#中创建MRU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆