using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.IO;
using
System.Linq;
using
System.Windows.Forms;
using
SharpConfig;
using
Telerik.WinControls.UI;
namespace
Recent
{
public
class
RecentFolders
{
readonly
RadSplitButtonElement _param_Button;
readonly
string
_param_FileName;
readonly
string
_param_SectionName;
readonly
int
_param_MaxDisplayItems;
public
readonly
FolderBrowserDialog Dialog;
List<
string
> _items;
public
event
EventHandler<RecentFolderChangingEventArgs> RecentFolderChanging;
public
event
EventHandler<RecentFolderChangedEventArgs> RecentFolderChanged;
/// <summary>
///
/// </summary>
/// <param name="form"></param>
/// <param name="button">For RadSplitButton, use DropDownButtonElement</param>
/// <param name="fileName"></param>
/// <param name="sectionName"></param>
/// <param name="maxDisplayItems"></param>
public
RecentFolders(Form form, RadSplitButtonElement button,
string
fileName,
string
sectionName =
"Recent"
,
int
maxDisplayItems = 10)
{
_param_Button = button;
_param_FileName = fileName;
_param_SectionName = sectionName;
_param_MaxDisplayItems = maxDisplayItems;
form.FormClosed +=
(sender, e) => Save();
var browse =
new
RadMenuItem();
browse.Click +=
(sender, e) => OnBrowseClick();
_param_Button.DefaultItem = browse;
_items =
new
List<
string
>();
Dialog =
new
FolderBrowserDialog();
Dialog.ShowNewFolderButton =
false
;
Load();
}
void
Load()
{
if
(File.Exists(_param_FileName))
{
try
{
Configuration cfg = Configuration.LoadFromFile(_param_FileName);
if
(cfg.Contains(_param_SectionName))
{
foreach
(var setting
in
cfg[_param_SectionName])
_items.Add(setting.StringValue);
if
(_items.Count != 0)
{
Dialog.SelectedPath = _items[0];
}
}
}
catch
{
}
}
UpdateUI();
}
void
Save()
{
Configuration cfg =
null
;
if
(File.Exists(_param_FileName))
{
try
{
cfg = Configuration.LoadFromFile(_param_FileName);
if
(cfg.Contains(_param_SectionName))
cfg.Remove(_param_SectionName);
}
catch
{
}
}
if
(cfg ==
null
)
cfg =
new
Configuration();
var section =
new
Section(_param_SectionName);
for
(
int
i = 0; i < _items.Count; i++)
{
section.Add(
new
Setting((i + 1).ToString(), _items[i]));
}
cfg.Add(section);
cfg.SaveToFile(_param_FileName);
}
void
UpdateUI()
{
_param_Button.Items.Clear();
if
(_items.Count != 0)
{
int
count =
_items.Count > _param_MaxDisplayItems ?
_param_MaxDisplayItems :
_items.Count;
for
(
int
i = 0; i < count; i++)
{
var item =
new
RadMenuItem(_items[i]);
item.Click +=
(sender, e) => OnItemClick(((RadMenuItem)sender).Text);
_param_Button.Items.Add(item);
}
_param_Button.Items.Add(
new
RadMenuSeparatorItem());
var clear =
new
RadMenuItem(
"Clear Recent List"
);
clear.Click +=
(sender, e) => OnClearClick();
_param_Button.Items.Add(clear);
_param_Button.ArrowButton.Enabled =
true
;
}
else
{
// Telerik Issue: If RadSplitButton is placed on RadForm, then this is not working.
_param_Button.ArrowButton.Enabled =
false
;
}
}
void
OnBrowseClick()
{
if
(Continue())
{
if
(Dialog.ShowDialog() == DialogResult.OK)
{
AddToTop(Dialog.SelectedPath);
}
}
}
void
OnItemClick(
string
path)
{
if
(Continue())
{
if
(Directory.Exists(path))
{
AddToTop(path);
Dialog.SelectedPath = path;
}
else
{
string
s =
string
.Format(
"The folder '{0}' cannot be opened.\n\nDo you want to remove the reference to it from the Recent List?"
, path);
if
(MessageBox.Show(s,
""
, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_items.Remove(path);
UpdateUI();
}
if
(_items.Count != 0)
_param_Button.ShowDropDown();
}
}
}
bool
Continue()
{
if
(RecentFolderChanging !=
null
)
{
var args =
new
RecentFolderChangingEventArgs();
RecentFolderChanging.Invoke(
this
, args);
return
!args.Cancel;
}
return
true
;
}
void
OnClearClick()
{
_items =
_items
.Skip(_param_MaxDisplayItems)
.ToList();
UpdateUI();
if
(_items.Count != 0)
_param_Button.ShowDropDown();
}
void
AddToTop(
string
path)
{
int
index = _items.FindIndex(s => s.Equals(path, StringComparison.OrdinalIgnoreCase));
if
(index != 0)
{
// Exist?
if
(index != -1)
_items.RemoveAt(index);
_items.Insert(0, path);
UpdateUI();
}
if
(RecentFolderChanged !=
null
)
{
RecentFolderChanged.Invoke(
this
,
new
RecentFolderChangedEventArgs(path));
}
}
}
public
class
RecentFolderChangingEventArgs : CancelEventArgs
{
public
RecentFolderChangingEventArgs(
bool
cancel =
false
)
:
base
(cancel)
{
}
}
public
class
RecentFolderChangedEventArgs : EventArgs
{
public
string
Path {
get
;
private
set
; }
public
RecentFolderChangedEventArgs(
string
path)
{
Path = path;
}
}
}
To use, Add RadSplitButton or RadSplitButtonElement in RadForm or RadRibbonForm respectively and use following code...
Where...
Please post suggestions or queries.