如何在没有表单的情况下使用 Visual Studio 设计器 [英] How can I use Visual Studio designer without form

查看:34
本文介绍了如何在没有表单的情况下使用 Visual Studio 设计器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 创建一个 Windows 窗体项目.

I'm using Visual Studio to create a Windows Form project.

解决方案资源管理器中没有用于我的项目的表单类 - 我即时构建所有表单.所以我没有机会在 Visual Studio 或工具箱中打开设计器.

There are no Form classes in the Solution Explorer for my project - I build all my forms on the fly. So I have no opportunity to open the Designer in Visual Studio, or the Toolbox.

但我想创建一个 ContextMenuStrip 并在设计器中对其进行编辑.我通常通过将 ContextMenuStrip 从 ToolBox 拖到 Designer 上来完成此操作.但是,如果不先创建表单或用户控件,我就无法(据我所知)打开设计器.

But I want to create a ContextMenuStrip and edit it in the Designer. I normally do this by dragging a ContextMenuStrip from the ToolBox onto the Designer. However, I don't have any way (that I can tell) to open the Designer without first creating a form, or user control.

如何在设计器中编辑 ContextMenuStrip 而不先将其与表单或用户控件相关联?

How can I edit a ContextMenuStrip in the designer without first associating it with a form or user control?

要清楚 - 我知道如何在代码中即时生成 ContextMenuStrip.那不是我想要的.我想使用设计器来创建和编辑 ContextMenuStrip,而不必先将 ContextMenuStrip 与我不打算包含在我的项目中的表单或用户控件相关联.

To be clear - I know how to generate a ContextMenuStrip on the fly, in code. That is not what I want. I want to use the Designer to create and edit a ContextMenuStrip, without having to first associate the ContextMenuStrip with a form or user control that I'm not going to include in my project.

(Visual Studio 2013 Pro,如果重要的话)

(Visual Studio 2013 Pro, if that matters)

推荐答案

您可以将 ContextMenuStrip 添加到表单,然后使用代码修改表单,以便您使用单例类型属性访问它:

You could add your ContextMenuStrip to a Form, then modify the form with code so that you access it with a singleton type property:

public partial class frmContextMenuHost : Form
{

    private frmContextMenuHost()
    {
        InitializeComponent();
    }

    private static frmContextMenuHost _frmContextMenu = null;

    public static ContextMenuStrip GetContextMenu
    {
        get
        {
            if (_frmContextMenu == null)
            {
                _frmContextMenu = new frmContextMenuHost();
            }
            return _frmContextMenu.contextMenuStrip1;
        }
    }

}

您打算如何使用 ContextMenuStrip 的事件?

How are you planning on using the events for the ContextMenuStrip?

这篇关于如何在没有表单的情况下使用 Visual Studio 设计器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆