- Introduction
- Developer’s Guide
- Samples
- Reference
Item rename support
Using Windows Explorer, the end user can rename Shell Items. It can be done using context menus (if the ‘Rename’ menu item is visible), or by pressing the F2 key while the item is selected. In all cases, the item can only be renamed if the corresponding ShellItem instance has its CanRename property set to true. By default, it’s not.
Renaming is done item by item. A folder can perfectly contain items (including sub folders) that can be renamed and others that cannot.
If an item supports renaming, you must override the OnOperate() method of the parent folder class to implement it:
protected virtual void OnOperate(ShellOperationEventArgs e)
The event argument contains the type of operation and its context (the item being renamed, the new name, etc.). So, for example, here is an implementation of OnOperate:
protected override void OnOperate(ShellOperationEventArgs e)
{
switch (e.Operation)
{
case ShellOperation.SetNameOf:
case ShellOperation.RenameItem:
OnRename(e);
break;
}
}
private void OnRename(ShellOperationEventArgs e)
{
if (e.Item.IsFolder)
{
// TODO: rename a folder
return;
}
// TODO: rename an item
}
Note 1: Explorer has multiple ways of renaming an item, so you should handle both SetNameOf and RenameItem operations.
Note 2: if the ShellItem instance represents a physical file item, the default implementation of OnOperate() will rename the physical file, so the method does not need to be overridden in that case.
Validation
Starting with ShellBoost version 1.5.0.3, a Shell Folder can validate a Shell Item’s name, especially during renaming operations. For example, this is how you can limit characters and name length:
protected override void OnGetNameValidCharactersEvent(object sender, GetNameValidCharactersEventArgs e)
{
e.InvalidCharacters = "xyz";
}
protected override void OnGetNameMaxLengthEvent(object sender, GetNameMaxLengthEventArgs e)
{
e.MaxLength = 30;
}
In this case, this is what the end-user will see when typing an invalid character: