【问题标题】:ContextMenuStrip in PropertyGridPropertyGrid 中的 ContextMenuStrip
【发布时间】:2013-10-02 05:34:11
【问题描述】:

无法将顶级控件添加到控件中请告诉我,如何在 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);

【问题讨论】:

  • 无法重现,而且似乎很难相信,因为 FileEditor 或 MenuList 都不是 null。请展示一些真实的代码和完整的堆栈框架。
  • 对不起,GetEditStype方法最好这样写:
  • 公共覆盖 UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; }
  • 没有相同的错误,它说“无法将顶级控件添加到控件中。”这是预期的。 PS:更新你的问题。

标签: propertygrid uitypeeditor


【解决方案1】:

我找到了解决方案。而不是

FileEditor.DropDownControl(MenuList);

Point MenuCoords = (System.Windows.Forms.Control.MousePosition);
MenuList.Show(MenuCoords);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    相关资源
    最近更新 更多