顶级控件无法添加到控件请告诉我,如何在PropertyGrid中显示ContextMenuStrip?为此我使用此代码:
public class SaveFileNameEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService FileEditor =
provider.GetService(typeof(IWindowsFormsEditorService)) as
IWindowsFormsEditorService;
ContextMenuStrip MenuList = null;
ToolStripMenuItem MenuItem = null;
if (FileEditor != null)
{
MenuList = new ContextMenuStrip ();
MenuItem = new ToolStripMenuItem("Add File...");
MenuList.Items.Add(MenuItem);
MenuItem = new ToolStripMenuItem("Save File ...");
MenuList.Items.Add(MenuItem);
MenuItem = new ToolStripMenuItem("Delete File ...");
MenuList.Items.Add(MenuItem);
FileEditor.DropDownControl(MenuList);
MenuList.Show();
}
return value;
}
}
但是我在行
上收到错误消息“顶级控件无法添加到控件”FileEditor.DropDownControl(MenuList);
答案 0 :(得分:0)
我找到了解决方案。而不是
FileEditor.DropDownControl(MenuList);
把
Point MenuCoords = (System.Windows.Forms.Control.MousePosition);
MenuList.Show(MenuCoords);