我想在ToolStrip中添加一个TrackBar.我在网上的某个地方发现了这个代码,但我不知道如何使用它,因为它应该编译?
码
    /// <summary>
    /// Adds trackbar to toolstrip stuff
    /// </summary>
    [
    ToolStripItemDesignerAvailability
        (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)
    ]
    public class ToolStripTraceBarItem : ToolStripControlHost
    {
        public ToolStripTraceBarItem(): base(new TrackBar())
        {
        }
    }
任何提示都会受到关注!
我正在经历一些奇怪的行为。让我试着解释一下,我将代码精简到最低限度,但问题仍然存在。所以首先,我使用 VS2013 和 .NET 4.0,我使用的是 Windows 8.1。
所以我有一个自定义UserControl与TextBox多数民众赞成正在通过使用ToolStripControlHost,如果我专注于这个文本框和命中TAB通过控制这个文本框的左侧,它只是个周期。如果我将它聚焦并点击SHIFT+ TAB,它会在它右侧的按钮之间循环。

所以这是我的表格的一个例子。中间的文本框是一个自定义控件。我的代码(尽可能简化)如下所示:
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public class ToolStripTestControl : ToolStripControlHost
{
    public ToolStripTestControl() : this(new TestControl()) { }
    public ToolStripTestControl(Control c) : base(c) { }
}
public class TestControl : UserControl
{
    private TextBox _textBox = new TextBox();
    public TestControl()
    {
        _textBox.Dock = DockStyle.Fill;
        this.Controls.Add(_textBox);
    }
    protected override Size DefaultMinimumSize { get { return new Size(100, 22); } }
}
只需创建一个新的 …
我有一个 ToolStripSplitButton,下拉列表中包含各种元素。其中之一是包含在 ToolStripControlHost 中的 Trackbar,称为 ToolStripTrackbarItem。它的代码(我从 stackoverflow 得到的):
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace Application
{
    [System.ComponentModel.DesignerCategory("code")]
    [System.Windows.Forms.Design.ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip | ToolStripItemDesignerAvailability.MenuStrip)]
    public class ToolStripTrackbarItem : ToolStripControlHost
    {
        public ToolStripTrackbarItem()
            : base(CreateControlInstance())
        {
            this.Size = Control.Size;
        }
        public TrackBar TrackBar
        {
            get { return Control as TrackBar; }
        }
        private static Control CreateControlInstance()
        {
            TrackBar t = new TrackBar();
            t.AutoSize = false;
            return t;
        }
        [DefaultValue(0)]
        public int Value
        {
            get { return TrackBar.Value; }
            set { …我试图模仿这个答案但是虽然这有效:
public class TrackBarMenuItem : ToolStripControlHost
{
    TrackBar trackBar;
    public TrackBarMenuItem()
        : base(new TrackBar())
    {
        trackBar = Control as TrackBar;
    }
}
这不是:
public class PanelMenuItem : ToolStripControlHost
{
    Panel panel;
    public PanelMenuItem()
        : base(new Panel())
    {
        panel = Control as Panel;
        Visible = true;
        Enabled = true;
        panel.AutoSize = false;
        panel.Size = new Size(100, 50);
    }
}
为什么?
我这样叫他们:
contextMenuStrip1.Items.Add(new TrackBarMenuItem());
contextMenuStrip1.Items.Add(new PanelMenuItem());