Click here to Skip to main content
12,458,167 members (54,286 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.3K views
Posted

Usage of a TrackBar as a ToolStripMenuItem

, 1 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Create a custom eventargs to make the eventhandling simpler.Any code interested in the change in the tractbar is very likely interested in its value..../// /// Custom event arguments class so we can push the value of the trackbar/// to any interested subscribers./// public class...
Create a custom eventargs to make the eventhandling simpler.
Any code interested in the change in the tractbar is very likely interested in its value....

/// <summary>
/// Custom event arguments class so we can push the value of the trackbar
/// to any interested subscribers.
/// </summary>
public class TrackBarEventArgs: EventArgs
{
   /// <summary>
   /// The new value of the trackbar.
   /// </summary>
   public int NewValue {get;set;}
}

[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;
                // you can ommit the delegate creation:
                trackBar.ValueChanged += 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;
                // you can ommit the delegate creation: new EventHandler();
                trackBar.ValueChanged -= 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)
                {
                    // Use custom event arguments so we can push the value of the trackbar.
                    var arguments = new TrackBarEventArgs();
                    arguments.NewValue = TrackBar.Value;  
                    ValueChanged(sender, e);
                }
            }
            // add an event that is subscribable from the designer.
            // Changed to type of: TrackBarEventArgs
            public event EventHandler<trackbareventargs> ValueChanged;
 
            // set other defaults that are interesting
            protected override Size DefaultSize
            {
                get
                {
                    return new Size(200, 16);
                }
            }
       } </trackbareventargs>


Usage:

 private void ToolStripMenuItem1_ValueChanged(object sender, TrackBarEventArgs e)
{
    //The trackbar value is stored in the event args for easy access.
    int trackbarValue = e.NewValue;
    pictureBox2.Image = ChangeB(new Bitmap(pictureBox1.Image), trackbarValue );
}

License

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

Share

About the Author

Kabwla.Phone
Software Developer (Senior)
Netherlands Netherlands
Doing that 'computer thing' ever since the C64.

Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.

Either being an appliction or a patient.
Oddly enough, more people care about the death of their application, than the massacre op people...

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionCustom background image Pin
Member 1190565126-Aug-15 19:01
memberMember 1190565126-Aug-15 19:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 1 Nov 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid