无法获取菜单条上轨迹栏的值

发布于 2024-12-12 08:55:50 字数 1371 浏览 6 评论 0原文

我手动向菜单条添加了一个轨迹栏,因为 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”接受 “goruntuIsleme2.Form1.TrackBarMenuItem”类型的第一个参数可以 被发现(您是否缺少 using 指令或程序集引用?)

有什么想法吗?

I've added a trackbar to menu strip manually because vs 2008 doesn't allow me to do.
However, i can't get the value of trackbar.

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);

and this is how i need to use it:

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

     }

but i get this error:

Error 1 'goruntuIsleme2.Form1.TrackBarMenuItem' does not contain a
definition for 'Value' and no extension method 'Value' accepting a
first argument of type 'goruntuIsleme2.Form1.TrackBarMenuItem' could
be found (are you missing a using directive or an assembly reference?)

any ideas?

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

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

发布评论

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

评论(3

月下客 2024-12-19 08:55:50

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

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

Expose the value of the internal Trackbar object as a property on your new TrackBarMenuItem class:

Value { get { return trackBar.Value; } set { trackBar.Value = value; } }
银河中√捞星星 2024-12-19 08:55:50

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

[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);
            }
        }

    }

如果您将其添加到代码中,您将能够通过设计器将轨迹栏添加为 ToolStripMenuItem。

i am adding the solution i found. someone might need it:

[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);
            }
        }

    }

if you add this to your code, you will be able to add trackbars as ToolStripMenuItem via Designer.

っ〆星空下的拥抱 2024-12-19 08:55:50

您的类 TrackBarMenuItem 是否有一个名为 Value 的属性?如果没有,您必须添加它。

Does your class TrackBarMenuItem has a property called Value? If not, you have to add it.

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