无法获得菜单条上的跟踪栏值

时间:2011-10-26 08:35:08

标签: c# menustrip trackbar

我已手动向菜单条添加了一个跟踪栏,因为vs 2008不允许我这样做。 但是,我无法获得轨迹栏的价值。

Form1.cs中:

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

         public TrackBarMenuItem()
             : base(new TrackBar())
         {
             this.trackBar = this.Control as TrackBar;
             trackBar.TickFrequency = 1;
             trackBar.Maximum = 255;
             trackBar.LargeChange = 5;
             trackBar.SmallChange = 2;
         }
     }

Form1.Designer.cs:

private TrackBarMenuItem trackBar1;
// 
// trackBar1
// 
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(104, 25);

这就是我需要使用它的方式:

         private void trackBar1_Scroll(object sender, System.EventArgs e)
     {
         int valueB = trackBar1.Value;
         pictureBox2.Image = Deneme(new Bitmap(pictureBox1.Image),valueB);

     }

但是我收到了这个错误:

  

错误1'goruntuIsleme2.Form1.TrackBarMenuItem'不包含   'Value'的定义,没有扩展方法'Value'接受a   'goruntuIsleme2.Form1.TrackBarMenuItem'类型的第一个参数可以   找到(你错过了使用指令或汇编引用吗?)

任何想法?

3 个答案:

答案 0 :(得分:1)

将内部Trackbar对象的值公开为新TrackBarMenuItem类的属性:

Value { get { return trackBar.Value; } set { trackBar.Value = value; } }

答案 1 :(得分:1)

我正在添加我找到的解决方案。有人可能需要它:

[System.ComponentModel.DesignerCategory("code")]
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip | ToolStripItemDesignerAvailability.MenuStrip)]
    public partial class ToolStripMenuItem : ToolStripControlHost
    {
        public ToolStripMenuItem()
            : base(CreateControlInstance())
        {

        }

        /// <summary>
        /// Create a strongly typed property called TrackBar - handy to prevent casting everywhere.
        /// </summary>
        public TrackBar TrackBar
        {
            get
            {
                return Control as TrackBar;
            }
        }

        /// <summary>
        /// Create the actual control, note this is static so it can be called from the
        /// constructor.
        ///
        /// </summary>
        /// <returns></returns>
        private static Control CreateControlInstance()
        {
            TrackBar t = new TrackBar();
            t.AutoSize = false;
            t.Height = 16;
            t.Maximum = 255;

            // Add other initialization code here.
            return t;
        }

        [DefaultValue(0)]
        public int Value
        {
            get { return TrackBar.Value; }
            set { TrackBar.Value = value; }
        }


        /// <summary>
        /// Attach to events we want to re-wrap
        /// </summary>
        /// <param name="control"></param>
        protected override void OnSubscribeControlEvents(Control control)
        {
            base.OnSubscribeControlEvents(control);
            TrackBar trackBar = control as TrackBar;
            trackBar.ValueChanged += new EventHandler(trackBar_ValueChanged);
        }

        /// <summary>
        /// Detach from events.
        /// </summary>
        /// <param name="control"></param>
        protected override void OnUnsubscribeControlEvents(Control control)
        {
            base.OnUnsubscribeControlEvents(control);
            TrackBar trackBar = control as TrackBar;
            trackBar.ValueChanged -= new EventHandler(trackBar_ValueChanged);

        }


        /// <summary>
        /// Routing for event
        /// TrackBar.ValueChanged -> ToolStripTrackBar.ValueChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void trackBar_ValueChanged(object sender, EventArgs e)
        {
            // when the trackbar value changes, fire an event.
            if (this.ValueChanged != null)
            {
                ValueChanged(sender, e);
            }
        }

        // add an event that is subscribable from the designer.
        public event EventHandler ValueChanged;


        // set other defaults that are interesting
        protected override Size DefaultSize
        {
            get
            {
                return new Size(200, 16);
            }
        }

    }

如果将其添加到代码中,您将能够通过Designer将跟踪栏添加为ToolStripMenuItem。

答案 2 :(得分:0)

您的班级TrackBarMenuItem是否有名为Value的媒体资源?如果没有,你必须添加它。