将 TrackBar 控件添加到 ContextMenu

发布于 2024-10-05 07:21:06 字数 74 浏览 0 评论 0原文

是否可以将 TrackBar 控件添加到 ContextMenu?因此,当我右键单击时,我的上下文菜单将下拉并且轨迹栏将显示为菜单项?

Is it possible to add a TrackBar control to a ContextMenu? So when I right click, my ContextMenu will drop down and a TrackBar will appear as a menu item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烛影斜 2024-10-12 07:21:06

如果您的上下文菜单是 ContexMenuStrip,您可以通过以下方式创建项目:

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip | 
                                   ToolStripItemDesignerAvailability.ContextMenuStrip)]
public class TrackBarMenuItem : ToolStripControlHost
{
    private TrackBar trackBar;

    public TrackBarMenuItem():base(new TrackBar())
    {
        this.trackBar = this.Control as TrackBar;
    }

    // Add properties, events etc. you want to expose...
}

借助 ToolStripItemDesignerAvailability 属性,您甚至可以在表单设计器中看到该项目,如图所示如下图中:

alt text

PS
此解决方案来自此 MSDN 示例

If your context menu is a ContexMenuStrip, you can create an item in this way:

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip | 
                                   ToolStripItemDesignerAvailability.ContextMenuStrip)]
public class TrackBarMenuItem : ToolStripControlHost
{
    private TrackBar trackBar;

    public TrackBarMenuItem():base(new TrackBar())
    {
        this.trackBar = this.Control as TrackBar;
    }

    // Add properties, events etc. you want to expose...
}

Thanks to the ToolStripItemDesignerAvailability attribute, you can even see the item in the Forms Designer, as shown in the image below:

alt text

P.S.
This solution comes from this MSDN example

孤单情人 2024-10-12 07:21:06

是的,您需要将上下文菜单设置为用户绘制并自己绘制菜单项。您必须创建一个自定义 MenuItem 来实现 TrackBar

Yes, you need to set the context menu to user draw and draw the menu items yourself. You will have to create a custom MenuItem the implementes a TrackBar

狂之美人 2024-10-12 07:21:06

对于任何偶然发现此问题的人来说,这是值得的:

未找到类型“System.Windows.Forms.ToolStripControlHost”的构造函数。

我让它工作的唯一方法是将派生控件放入它自己的文件中。当它与另一个控件位于同一文件中时,它会让设计者感到困惑。

For what it's worth for anyone who stumbles across this having problems with:

Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found.

The only way I got it to work was by putting the derived control in it's own file. When it is in the same file as another control it confuses the designer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文