Adding a TrackBar control to a ContextMenu

南楼画角 提交于 2019-11-29 04:56:55

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:

P.S.
This solution comes from this MSDN example

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

johnb003

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!