Вопрос

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?

Это было полезно?

Решение 2

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.

Другие советы

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top