locked
MRU component RRS feed

  • Question

  • can anyone help me?
    how to create an MRU entry into file menu? line in notepad++.
    i saw the examples over the internet but there are complicated.
    is there any posibility tu add a new toolStripMenuItem like creating new tabs?
    if so, please give me an example
    Thanks!

    Saturday, April 12, 2008 9:48 AM

Answers

  • Talkabout,

     

    To implement this kind of functionality is very simple, depending on the context of the files your application uses, the only real challenge here is to decide how best to persist the MRU data.

     

    Here is a rough example that uses the Registry rather than the Settings/Config file, it consists of a static class Utility called MRU that has some methods to Load, Save and Insert items to the list, one of the methods accepts a ToolStripMenuItem as a parameter so you call populate your Menus easily.

     

    The list will always move the most recent item to the top.

     

    Code Snippet

    using System;

    using System.Collections.ObjectModel;

    using System.IO;

    using System.Windows.Forms;

    using Microsoft.Win32;

     

    namespace MRUExample

    {

    public static class MRU

    {

    private static Collection<string> items;

    public static Collection<string> Items

    {

    get { return items; }

    }

     

    public static void Load()

    {

    items = new Collection<string>();

    RegistryKey appKey = Application.UserAppDataRegistry.OpenSubKey("MRU");

    if (appKey != null)

    {

    String[] values = appKey.GetValueNames();

    foreach (String value in values)

    {

    Items.Add(appKey.GetValue(value).ToString());

    }

    appKey.Close();

    }

    }

     

    public static void Save()

    {

    Application.UserAppDataRegistry.DeleteSubKey("MRU", false);

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

    int i = 0;

    foreach (String mruItem in Items)

    {

    appKey.SetValue(i.ToString(), mruItem);

    i++;

    }

    appKey.Close();

    }

     

    public static void Insert(String fileName)

    {

    if (Items.Contains(fileName))

    {

    Items.Remove(fileName);

    Items.Insert(0, fileName);

    }

    else

    {

    Items.Insert(0, fileName);

    }

    Save();

    }

     

    public static void UpdateMRUMenu(ToolStripMenuItem menuItem)

    {

    Collection<string> ItemsToRemove = new Collection<string>();

    menuItem.DropDownItems.Clear();

    foreach (String mruItem in MRU.Items)

    {

    if (File.Exists(mruItem))

    {

    FileInfo fi = new FileInfo(mruItem);

    ToolStripMenuItem itm = new ToolStripMenuItem();

    itm.Text = fi.Name;

    itm.Tag = mruItem;

    itm.Click += new EventHandler(itm_Click);

    menuItem.DropDownItems.Add(itm);

    }

    else

    {

    ItemsToRemove.Add(mruItem);

    }

    }

    foreach (String itemToRemove in ItemsToRemove)

    {

    Console.WriteLine("Removing item...");

    Items.Remove(itemToRemove);

    }

    }

     

    static void itm_Click(object sender, EventArgs e)

    {

    //Load your file...

    }

    }

    }

     

     

    To use the MRU utility, create a project and modify the Program class to call the MRU.Load() method, you could do this anywhere in your application.

     

    Code Snippet

    [STAThread]

    static void Main()

    {

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    MRU.Load();

    Application.Run(new Form1());

    MRU.Save();

    }

     

     

    Then on your Form, add a MenuStrip and a ToolStripMenuItem that represents your MRU Item List. You can then implement the MRU Utility as shown below:

     

    Code Snippet

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    MRU.UpdateMRUMenu(recentFilesToolStripMenuItem);

    }

     

    private void openToolStripMenuItem_Click(object sender, EventArgs e)

    {

    OpenFileDialog ofd = new OpenFileDialog();

    ofd.Filter = "C#|*.cs";

    if (ofd.ShowDialog()==DialogResult.OK)

    {

    //Add the file to the MRU.Items collection using

    //the MRU.Insert method...

     

    MRU.Insert(ofd.FileName);

    MRU.UpdateMRUMenu(recentFilesToolStripMenuItem);

     

    //...

    }

    }

    }

     

     

    Steve

    Saturday, April 12, 2008 4:20 PM

All replies

  • Talkabout,

     

    To implement this kind of functionality is very simple, depending on the context of the files your application uses, the only real challenge here is to decide how best to persist the MRU data.

     

    Here is a rough example that uses the Registry rather than the Settings/Config file, it consists of a static class Utility called MRU that has some methods to Load, Save and Insert items to the list, one of the methods accepts a ToolStripMenuItem as a parameter so you call populate your Menus easily.

     

    The list will always move the most recent item to the top.

     

    Code Snippet

    using System;

    using System.Collections.ObjectModel;

    using System.IO;

    using System.Windows.Forms;

    using Microsoft.Win32;

     

    namespace MRUExample

    {

    public static class MRU

    {

    private static Collection<string> items;

    public static Collection<string> Items

    {

    get { return items; }

    }

     

    public static void Load()

    {

    items = new Collection<string>();

    RegistryKey appKey = Application.UserAppDataRegistry.OpenSubKey("MRU");

    if (appKey != null)

    {

    String[] values = appKey.GetValueNames();

    foreach (String value in values)

    {

    Items.Add(appKey.GetValue(value).ToString());

    }

    appKey.Close();

    }

    }

     

    public static void Save()

    {

    Application.UserAppDataRegistry.DeleteSubKey("MRU", false);

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

    int i = 0;

    foreach (String mruItem in Items)

    {

    appKey.SetValue(i.ToString(), mruItem);

    i++;

    }

    appKey.Close();

    }

     

    public static void Insert(String fileName)

    {

    if (Items.Contains(fileName))

    {

    Items.Remove(fileName);

    Items.Insert(0, fileName);

    }

    else

    {

    Items.Insert(0, fileName);

    }

    Save();

    }

     

    public static void UpdateMRUMenu(ToolStripMenuItem menuItem)

    {

    Collection<string> ItemsToRemove = new Collection<string>();

    menuItem.DropDownItems.Clear();

    foreach (String mruItem in MRU.Items)

    {

    if (File.Exists(mruItem))

    {

    FileInfo fi = new FileInfo(mruItem);

    ToolStripMenuItem itm = new ToolStripMenuItem();

    itm.Text = fi.Name;

    itm.Tag = mruItem;

    itm.Click += new EventHandler(itm_Click);

    menuItem.DropDownItems.Add(itm);

    }

    else

    {

    ItemsToRemove.Add(mruItem);

    }

    }

    foreach (String itemToRemove in ItemsToRemove)

    {

    Console.WriteLine("Removing item...");

    Items.Remove(itemToRemove);

    }

    }

     

    static void itm_Click(object sender, EventArgs e)

    {

    //Load your file...

    }

    }

    }

     

     

    To use the MRU utility, create a project and modify the Program class to call the MRU.Load() method, you could do this anywhere in your application.

     

    Code Snippet

    [STAThread]

    static void Main()

    {

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    MRU.Load();

    Application.Run(new Form1());

    MRU.Save();

    }

     

     

    Then on your Form, add a MenuStrip and a ToolStripMenuItem that represents your MRU Item List. You can then implement the MRU Utility as shown below:

     

    Code Snippet

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    MRU.UpdateMRUMenu(recentFilesToolStripMenuItem);

    }

     

    private void openToolStripMenuItem_Click(object sender, EventArgs e)

    {

    OpenFileDialog ofd = new OpenFileDialog();

    ofd.Filter = "C#|*.cs";

    if (ofd.ShowDialog()==DialogResult.OK)

    {

    //Add the file to the MRU.Items collection using

    //the MRU.Insert method...

     

    MRU.Insert(ofd.FileName);

    MRU.UpdateMRUMenu(recentFilesToolStripMenuItem);

     

    //...

    }

    }

    }

     

     

    Steve

    Saturday, April 12, 2008 4:20 PM
  • ok, done, thanksSmile
    i have a history file in my Enviromnent.SpecialFolder.ApplicationData named History.txt, where i store my history files.
    how can i, from my code, where i have a textBox which accept only numbers, to set the maximum files(lines, with full path of files) that i can insert into history.txt? so if i type into that textBox 5, i wanna that only 5 lines to be written into history.txt, and if i open the 6th file(in a new tab), the first entry to the file being overwritten by the new file opened, and so on..
    how can i do this?
    Thanks
    Saturday, April 12, 2008 6:05 PM
  • So are you using the MRU example above? if so this can be easily modified to limit the number of MRU entries, let me know and I'll update the example.

     

    Steve

    Saturday, April 12, 2008 6:08 PM
  • Talkabout,

     

    I made a few minor modifications and included a MaxItems property within the MRU utility class, the value for this setting is stored in th registry. To modify the setting just get/set using MRU.MaxItems and call MRU.Save to persist the change.

     

    Code Snippet

    using System;

    using System.Collections.ObjectModel;

    using System.IO;

    using System.Windows.Forms;

    using Microsoft.Win32;

    namespace MRUExample

    {

    public static class MRU

    {

    private static Collection<string> items;

    public static Collection<string> Items

    {

    get { return items; }

    }

     

    private static int maxItems = 5;

    public static int MaxItems

    {

    get { return maxItems; }

    set { maxItems = value; }

    }

     

    public static void Load()

    {

    items = new Collection<string>();

    RegistryKey appKey = Application.UserAppDataRegistry.OpenSubKey("MRU");

    if (appKey != null)

    {

    RegistryKey settings = appKey.OpenSubKey("Settings");

    if (settings != null)

    {

    MaxItems = (int)settings.GetValue("Max");

    }

    String[] values = appKey.GetValueNames();

    foreach (String value in values)

    {

    Items.Add(appKey.GetValue(value).ToString());

    }

    appKey.Close();

    }

    while (Items.Count > MaxItems - 1)

    {

    Items.RemoveAt(Items.Count - 1);

    }

    }

     

    public static void Save()

    {

    RegistryKey appKey = Application.UserAppDataRegistry.OpenSubKey("MRU");

    if (appKey != null)

    {

    appKey.Close();

    Application.UserAppDataRegistry.DeleteSubKeyTree("MRU");

    }

    appKey = Application.UserAppDataRegistry.CreateSubKey("MRU");

    RegistryKey settings = appKey.CreateSubKey("Settings");

    settings.SetValue("Max", MaxItems);

    int i = 0;

    foreach (String mruItem in Items)

    {

    appKey.SetValue(i.ToString(), mruItem);

    i++;

    }

    appKey.Close();

    }

     

    public static void Insert(String fileName)

    {

    while (Items.Count > MaxItems-1)

    {

    Items.RemoveAt(Items.Count - 1);

    }

    if (Items.Contains(fileName))

    {

    Items.Remove(fileName);

    Items.Insert(0, fileName);

    }

    else

    {

    Items.Insert(0, fileName);

    }

    Save();

    }

     

    public static void UpdateMRUMenu(ToolStripMenuItem menuItem)

    {

    Collection<string> ItemsToRemove = new Collection<string>();

    menuItem.DropDownItems.Clear();

    foreach (String mruItem in MRU.Items)

    {

    if (File.Exists(mruItem))

    {

    FileInfo fi = new FileInfo(mruItem);

    ToolStripMenuItem itm = new ToolStripMenuItem();

    itm.Text = fi.Name;

    itm.Tag = mruItem;

    itm.Click += new EventHandler(itm_Click);

    menuItem.DropDownItems.Add(itm);

    }

    else

    {

    ItemsToRemove.Add(mruItem);

    }

    }

    foreach (String itemToRemove in ItemsToRemove)

    {

    Items.Remove(itemToRemove);

    }

    }

     

    static void itm_Click(object sender, EventArgs e)

    {

    //Load your file...

    }

    }

    }

     

     

     

    Saturday, April 12, 2008 6:38 PM
  • right, thanks
    but in File->Recent Files-> .... there are just the name of the files, not the full path. how can obtain the full path of the files?
    (when i open a file, the full path is openFileDialog1.FileName)
    Sunday, April 13, 2008 12:52 PM
  • Code Snippet

    public static void UpdateMRUMenu(ToolStripMenuItem menuItem)

    {

    Collection<string> ItemsToRemove = new Collection<string>();

    menuItem.DropDownItems.Clear();

    foreach (String mruItem in MRU.Items)

    {

    if (File.Exists(mruItem))

    {

    //FileInfo fi = new FileInfo(mruItem);

    ToolStripMenuItem itm = new ToolStripMenuItem();

    //itm.Text = fi.Name;

    itm.Text = mruItem;

    itm.Tag = mruItem;

    itm.Click += new EventHandler(itm_Click);

    menuItem.DropDownItems.Add(itm);

    }

    else

    {

    ItemsToRemove.Add(mruItem);

    }

    }

    foreach (String itemToRemove in ItemsToRemove)

    {

    Items.Remove(itemToRemove);

    }

    }

     

     

    Sunday, April 13, 2008 1:43 PM
  • protected void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
    string FileName = "";
    openFileDialog1.Title = "Open";
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    try
    {
    TabPage newTabPage = new TabPage();
    tabControl1.TabPages.Add(newTabPage);
    nrTabPageuri++;
    newTabPage.ImageKey = "ico501.ico";
    tabControl1.SelectedTab = newTabPage;
    TextBox newTextBox = new TextBox();
    newTextBox.AcceptsReturn = true;
    newTextBox.AcceptsTab = true;
    newTextBox.Multiline = true;
    newTextBox.Dock = DockStyle.Fill;
    newTabPage.Controls.Add(newTextBox);
    FileName = openFileDialog1.FileName;
    newTabPage.Text = (new FileInfo(FileName)).Name;
    tabControl1.SelectedTab.ToolTipText = FileName;
    MRUExample.MRU.Insert(openFileDialog1.FileName);
    MRUExample.MRU.UpdateMRUMenu(menuRecentFile);
    StreamReader sr = File.OpenText(FileName);
    string strLine;
    while ((strLine = sr.ReadLine()) != null)
    {
    newTextBox.Text += strLine + Environment.NewLine;
    }
    newTextBox.TextChanged += new EventHandler(textBox2_TextChanged_1);
    newTextBox.SelectionStart = 0;
    newTextBox.SelectionLength = 0;
    newTextBox.Focus();
    newTextBox.ScrollBars = ScrollBars.Both;
    newTextBox.Font = new Font("Courier New", 10f);
    str = newTextBox.Text;
    sr.Close();
    String s = tabControl1.SelectedTab.ToolTipText;
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    using (StreamWriter stw = File.CreateText("C:\\Documents and settings\\Sorin\\Application Data\\Notepad++\\History.txt"))
    for (int i = 0; i < tabControl1.TabPages.Count; i++)
    {
    if (tabControl1.TabPagesIdea.Text.Contains(".txt"))
    stw.WriteLine(tabControl1.TabPagesIdea.ToolTipText);
    }
    stw.Close();
    }
    }

    catch (Exception ex)
    {
    MessageBox.Show("Exception occured: " + ex.ToString());
    }
    }
    }

    if this is a openFile method, which open the files, how can i implement the item_Click method? if i click on a item from History(item being a file path), i wanna that file to be opened in a new tab

    itm.Click += new EventHandler(itm_Click);

    //itm_Click?

    how to generate this method?
    Thanks!

    Sunday, April 13, 2008 2:25 PM