|
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.Reflection;
using System.Security.Permissions;
using System.Collections.ObjectModel;
namespace SundbySoft.Controls.Design
{
/// <summary>
/// Provides a user interface that can edit a <see cref="ContextMenuCollection"/> at design time.
/// </summary>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class ContextMenusEditor : UITypeEditor
{
/// <summary>
/// Initializes a new instance of the <see cref="ContextMenusEditor"/> class.
/// </summary>
public ContextMenusEditor()
{
}
/// <summary>
/// Gets the editor style used by the EditValue method.
/// </summary>
/// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
/// <returns>A UITypeEditorEditStyle value that indicates the style of editor used by the EditValue method.
/// If the UITypeEditor does not support this method, then GetEditStyle will return None.</returns>
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
/// <summary>
/// Edits the specified object's value using the editor style indicated by the GetEditStyle method.
/// </summary>
/// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
/// <param name="provider">An IServiceProvider that this editor can use to obtain services.</param>
/// <param name="value">The object to edit.</param>
/// <returns>The new value of the object. If the value of the object has not changed, this should return the same object it was passed.</returns>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
// Get associated items list
Collection<object> associatedItems = new Collection<object>();
ContextMenuCollection contextMenuCollection = value as ContextMenuCollection;
foreach (object item in contextMenuCollection)
{
associatedItems.Add(item);
}
// Get command switchboard
UISwitchboard UISwitchboard = contextMenuCollection.UISwitchboard;
// Get available items list
Collection<object> availableItems = new Collection<object>();
foreach (object item in UISwitchboard.UIItemAdapter.GetAvailableContextMenus())
{
availableItems.Add(item);
}
// Get editor service
IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService != null)
{
// Show form
FormContextMenus formContextMenus = new FormContextMenus(UISwitchboard, associatedItems, availableItems);
if (editorService.ShowDialog(formContextMenus) == DialogResult.OK)
{
ContextMenuCollection modifiedContextMenuCollection = new ContextMenuCollection(UISwitchboard);
foreach (object item in associatedItems)
{
modifiedContextMenuCollection.Add(item);
}
return modifiedContextMenuCollection;
}
}
return value;
}
}
}
|
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.
Bjørn has developed software since 1984 mainly in C, C++ and C#.
Bjørn lives in Ormåsen, Buskerud in Norway. To contact Bjørn, email him at bjsundby@online.no. He also has a web site at http://www.sundbysoft.com.