如何以最简单的方式在 V 2010Express C# 中创建 MRU?

发布于 2024-09-15 22:26:33 字数 657 浏览 6 评论 0原文

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

好吧,问题是我尝试查看一些在线示例,但是我发现它们有点太令人困惑了......

那么,无论如何,任何人都可以在toolstripmenuitem 中创建“最近使用的”部分,而无需进入那些复杂的部分代码??

例如,我将无法理解这些东西...

注册表项:

KEY_CURRENT_USER\Software\Microsoft\VCExpress\9.0\FileMRUList

代码:

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

dictionary

microsoft.win32

我只需要像下面的链接所示的简单内容 http://www.codeproject.com/KB/menus/MRUHandler.aspx

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...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兰花执着 2024-09-22 22:26:33

那么您想创建一个像屏幕截图中那样的子菜单吗?为此,您必须:

  • 将最近使用的文件列表存储在某处。这可能是注册表,也可能只是一个简单的文本文件,为了简单起见,我现在将这样做。
  • 了解如何在运行时而不是在设计器中生成菜单项。

1. 将 MRU 存储在文件中

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

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

每次有人打开一个文件时,您都会将此文件添加到 MRU 的开头,对吧?

_mru.Insert(0, fullFilePath);

现在,当然,当应用程序关闭时,您需要将此 MRU 保存到文件中。让我们在表单的 FormClosed 事件中执行此操作。双击属性中的 FormClosed 事件并编写一些代码,如下所示:

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

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

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 包含我们需要为每个文件路径创建一个新的菜单项。我在这里假设您的文件菜单中已经有一个菜单项(屏幕截图中名为“最近使用”的项目),并且它的名称为mnuRecentlyUsed ,并且我们只需要创建子项:

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

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

void OpenRecentFile(object sender, EventArgs e)
{
    var menuItem = (ToolStripMenuItem) sender;
    var filepath = (string) menuItem.Tag;

    // Proceed to open the file
    // ...
}

免责声明

除非您理解,否则请不要使用任何此代码并且您确信它是为了实现您的目的而编写的。如果需要做一些稍微不同的事情,我相信您可以自己进行必要的更改。

另外,我相信您会注意到,上面的内容在程序运行时不会更新子菜单。如果您了解上面的代码是如何工作的,那么我相信您将能够自己弄清楚剩下的部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文