Click here to Skip to main content
Click here to Skip to main content

Tagged as

Usage of a TrackBar as a ToolStripMenuItem

, 30 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Usage of a TrackBar as a ToolStripMenuItem
I needed to add a trackbar to my menu as a ToolStripMenuItem but Visual Studio doesn't allow that by default. I tried to add it manually on Designer.cs and I managed to do it. However, it wasn't functional. Because TrackBar was locked up inside the toolstrip item without exposing any of its properties and events.
 
So I found this code and added it to my form:
 
               [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;
                // 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);
                }
            }
        }
 
Now I can add TrackBar from designer just like other regular items.
 
Example usage:
 
        private void ToolStripMenuItem1_ValueChanged(object sender, EventArgs e)
        {
            int valueB = ToolStripMenuItem1.Value;
            pictureBox2.Image = ChangeB(new Bitmap(pictureBox1.Image), valueB);
        }
 
P.S.: This is my first post here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

A. Duman

Turkey Turkey
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 30 Oct 2011
Article Copyright 2011 by A. Duman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid