|
using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using Microsoft.Win32;
namespace Common
{
/// <summary>
/// Summary description for RecentFileList.
/// </summary>
public class RecentFileList
{
public string RegistryKey = "";
public MenuItem FileMenu = null;
public int FileMenuPosition = 0;
public int MaxNumberOfFiles = 16;
public delegate void MenuClickEventHandler( object sender, MenuClickEventArgs e );
public event MenuClickEventHandler OnMenuClick;
private MenuItem Separator = null;
private ArrayList RecentFiles = null;
public RecentFileList()
{
}
public void SetMenuItems()
{
RemoveMenuItems();
LoadRecentFiles();
InsertMenuItems();
}
private void RemoveMenuItems()
{
if ( Separator != null ) FileMenu.MenuItems.Remove( Separator );
if ( RecentFiles != null )
foreach ( RecentFile r in RecentFiles )
if ( r.MenuItem != null )
FileMenu.MenuItems.Remove( r.MenuItem );
Separator = null;
RecentFiles = null;
}
private void InsertMenuItems()
{
if ( RecentFiles == null ) return;
if ( RecentFiles.Count == 0 ) return;
int i = FileMenuPosition;
foreach ( RecentFile r in RecentFiles )
{
string s = "";
if ( r.Number < 9 ) s += "&";
s += ( r.Number + 1 ).ToString();
s += " ";
s += Path.GetDirectoryName( r.Filename );
s += "\\";
s += Path.GetFileNameWithoutExtension( r.Filename );
r.MenuItem = new MenuItem( s, new EventHandler( RaiseOnMenuClick ) );
FileMenu.MenuItems.Add( i++, r.MenuItem );
}
Separator = new MenuItem( "-" );
FileMenu.MenuItems.Add( i, Separator );
}
private void LoadRecentFiles()
{
RegistryKey k = Registry.CurrentUser.OpenSubKey( RegistryKey );
if ( k == null ) k = Registry.CurrentUser.CreateSubKey( RegistryKey );
RecentFiles = new ArrayList( 16 );
for ( int i = 0 ; i < MaxNumberOfFiles ; i++ )
{
string Filename = (string) k.GetValue( i.ToString( "00" ) );
if ( Filename == null ) break;
if ( Filename.Length == 0 ) break;
RecentFiles.Add( new RecentFile( i, Filename ) );
}
}
public void RemoveFile( string Filename )
{
RegistryKey k = Registry.CurrentUser.OpenSubKey( RegistryKey );
if ( k == null ) return;
for ( int i = 0 ; i < MaxNumberOfFiles ; i++ )
{
again:
string s = (string) k.GetValue( i.ToString( "00" ) );
if ( s == Filename )
{
RemoveFile( i );
goto again;
}
}
}
private void RemoveFile( int Number )
{
RegistryKey k = Registry.CurrentUser.OpenSubKey( RegistryKey, true );
if ( k == null ) return;
k.DeleteValue( Number.ToString( "00" ), false );
for ( int i = Number ; i < MaxNumberOfFiles - 1 ; i++ )
{
int iNext = i + 1;
if ( k.GetValue( iNext.ToString( "00" ) ) == null ) break;
k.SetValue( i.ToString( "00" ), k.GetValue( iNext.ToString( "00" ) ) );
k.DeleteValue( iNext.ToString( "00" ) );
}
}
public void InsertFile( string Filename )
{
RegistryKey k = Registry.CurrentUser.OpenSubKey( RegistryKey );
if ( k == null ) Registry.CurrentUser.CreateSubKey( RegistryKey );
k = Registry.CurrentUser.OpenSubKey( RegistryKey, true );
RemoveFile( Filename );
for ( int i = MaxNumberOfFiles - 2 ; i >= 0 ; i-- )
{
if ( k.GetValue( i.ToString( "00" ) ) == null ) continue;
int iNext = i + 1;
k.SetValue( iNext.ToString( "00" ), k.GetValue( i.ToString( "00" ) ) );
}
k.SetValue( "00", Filename );
}
private class RecentFile
{
public int Number = 0;
public string Filename = "";
public MenuItem MenuItem = null;
public RecentFile( int Number, string Filename )
{
this.Number = Number;
this.Filename = Filename;
}
}
public class MenuClickEventArgs : EventArgs
{
public string Filename = "";
public MenuClickEventArgs( string Filename )
{
this.Filename = Filename;
}
}
private void RaiseOnMenuClick( object sender, EventArgs e )
{
MenuItem m = sender as MenuItem;
string Filename = GetFilename( m );
if ( OnMenuClick != null ) OnMenuClick( sender, new MenuClickEventArgs( Filename ) );
}
private string GetFilename( MenuItem m )
{
foreach ( RecentFile r in RecentFiles )
if ( r.MenuItem == m )
return r.Filename;
return "";
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since
In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.
I now live near idyllic Bournemouth in England.
I can work 'virtually' anywhere!