Click here to Skip to main content
15,896,118 members
Home / Discussions / C#
   

C#

 
QuestionTrackBar Pin
Nicolás Marzoni14-Jun-10 17:32
Nicolás Marzoni14-Jun-10 17:32 
AnswerRe: TrackBar Pin
Calla14-Jun-10 19:32
Calla14-Jun-10 19:32 
GeneralRe: TrackBar Pin
Nicolás Marzoni15-Jun-10 1:28
Nicolás Marzoni15-Jun-10 1:28 
GeneralRe: TrackBar Pin
DaveyM6915-Jun-10 1:31
professionalDaveyM6915-Jun-10 1:31 
GeneralRe: TrackBar Pin
Nicolás Marzoni15-Jun-10 1:36
Nicolás Marzoni15-Jun-10 1:36 
GeneralRe: TrackBar Pin
DaveyM6915-Jun-10 2:39
professionalDaveyM6915-Jun-10 2:39 
GeneralRe: TrackBar Pin
DaveyM6915-Jun-10 2:40
professionalDaveyM6915-Jun-10 2:40 
GeneralRe: TrackBar - Long post alert! Pin
DaveyM6915-Jun-10 10:17
professionalDaveyM6915-Jun-10 10:17 
Hi, I've had a look through my files and I never completed it as it was no longer required. You are welcome to use what I did and finish it off if you'd like. The main things that need doing are marked // ToDo: - unfortunately that includes the painting and mouse handling - but the Range struct is pretty much complete and the main code for the control is in place.
C#
// Range.cs
using System;

namespace DaveyM69
{
    [Serializable]
    public struct Range : IEquatable<Range>
    {
        // ToDo: Make struct designer friendly!

        public static readonly Range Empty = new Range();
        public static readonly Range MaxRange = new Range(int.MinValue, int.MaxValue);
        public static readonly Range Percentage = new Range(0, 100);

        private int lower;
        private int upper;

        public Range(int lower, int upper)
        {
            if (lower > upper)
            {
                int newUpper = lower;
                lower = upper;
                upper = newUpper;
            }
            this.lower = lower;
            this.upper = upper;
        }

        public static bool operator ==(Range rangeA, Range rangeB)
        {
            return (rangeA.lower == rangeB.lower) && (rangeA.upper == rangeB.upper);
        }
        public static bool operator !=(Range rangeA, Range rangeB)
        {
            return !(rangeA == rangeB);
        }

        public int Lower
        {
            get { return lower; }
        }
        public int Upper
        {
            get { return upper; }
        }

        public Range Adjust(int offset)
        {
            return new Range(lower + offset, upper + offset);
        }
        public int Contain(int value)
        {
            return Contain(value, false);
        }
        public int Contain(int value, bool excludeExtremities)
        {
            Range testRange = excludeExtremities ? this.Expand(-1) : this;
            if (value < testRange.lower)
                return testRange.lower;
            if (value > testRange.upper)
                return testRange.upper;
            return value;
        }
        public Range Contain(Range range)
        {
            return Contain(range, false);
        }
        public Range Contain(Range range, bool excludeExtremities)
        {
            if (Contains(range, excludeExtremities))
                return range;
            int newLower = range.lower;
            int newUpper = range.upper;
            if (!Contains(range.lower, excludeExtremities))
            {
                newLower = lower;
                if (excludeExtremities)
                    newLower++;
            }
            if (!Contains(range.upper, excludeExtremities))
            {
                newUpper = upper;
                if (excludeExtremities)
                    newUpper--;
            }
            Range result = new Range(newLower, newUpper);
            if (!Contains(result, excludeExtremities))
                throw new InvalidOperationException("The span of this range is too small to contain another range");
            return result;
        }
        public bool Contains(Range range)
        {
            return Contains(range, false);
        }
        public bool Contains(Range range, bool excludeExtremities)
        {
            return Contains(range.lower, excludeExtremities) && Contains(range.upper, excludeExtremities);
        }
        public bool Contains(int value)
        {
            return Contains(value, false);
        }
        public bool Contains(int value, bool excludeExtremities)
        {
            Range testRange = excludeExtremities ? this.Expand(-1) : this;
            if (testRange.lower < lower)
                throw new InvalidOperationException("The span of the specified range is too small to contain another range");
            return (value >= testRange.lower) && (value <= testRange.upper);
        }
        public Range Expand()
        {
            return Expand(1);
        }
        public Range Expand(int value)
        {
            return new Range(lower - value, upper + value);
        }
        public override bool Equals(object obj)
        {
            return obj is Range && this == (Range)obj;
        }
        public bool Equals(Range other)
        {
            return this == other;
        }
        public override int GetHashCode()
        {
            return lower ^ upper;
        }
        public override string ToString()
        {
            return string.Format("{0}, {1}", lower, upper);
        }
    }
}

C#
// RangeControl.cs
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DaveyM69.Windows.Forms
{
    public class RangeControl : Control
    {
        private static readonly Size Default_Size = new Size(200, 50);

        [Category("Range")]
        public event EventHandler ExcludeExtremitiesChanged;
        [Category("Range")]
        public event EventHandler InnerRangeChanged;
        [Category("Range")]
        public event EventHandler OuterRangeChanged;
        [Category("Range")]
        public event EventHandler ValueChanged;
        
        private bool excludeExtremities;
        private Range innerRange;
        private Range outerRange;
        private int value;

        public RangeControl()
        {
            SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.Selectable |
                ControlStyles.SupportsTransparentBackColor |
                ControlStyles.UserPaint, true);
            excludeExtremities = false;
            innerRange = new Range(25, 75);
            outerRange = Range.Percentage;
            value = 50;
        }

        protected override Size DefaultSize
        {
            get { return Default_Size; }
        }
        [Category("Range"),
        DefaultValue(false)]
        public bool ExcludeExtremities
        {
            get { return excludeExtremities; }
            set
            {
                if (excludeExtremities != value)
                {
                    excludeExtremities = value;
                    OnExcludeExtremitiesChanged(EventArgs.Empty);
                    InnerRange = innerRange; // Force refresh
                    Value = this.value; // Force refresh. This needs refactoring as if InnerRange changes this will have already been done!
                }
            }
        }
        [Category("Range"),
        DefaultValue(typeof(Range), "25, 75")]
        public Range InnerRange
        {
            get { return innerRange; }
            set
            {
                value = outerRange.Contain(value, excludeExtremities);
                if (innerRange != value)
                {
                    innerRange = value;
                    Invalidate();
                    OnInnerRangeChanged(EventArgs.Empty);
                    Value = this.value; // Force refresh
                }
            }
        }
        [Category("Range"),
        DefaultValue(typeof(Range), "0, 100")]
        public Range OuterRange
        {
            get { return outerRange; }
            set
            {
                if (outerRange != value)
                {
                    outerRange = value;
                    OnOuterRangeChanged(EventArgs.Empty);
                    InnerRange = innerRange; // Force refresh
                }
            }
        }
        [Category("Range"),
        DefaultValue(50)]
        public int Value
        {
            get { return value; }
            set
            {
                value = innerRange.Contain(value, excludeExtremities);
                if (this.value != value)
                {
                    this.value = value;
                    Invalidate();
                    OnValueChanged(EventArgs.Empty);
                }
            }
        }

        protected virtual void OnExcludeExtremitiesChanged(EventArgs e)
        {
            EventHandler eh = ExcludeExtremitiesChanged;
            if (eh != null)
                eh(this, e);
        }
        protected virtual void OnInnerRangeChanged(EventArgs e)
        {
            EventHandler eh = InnerRangeChanged;
            if (eh != null)
                eh(this, e);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            // ToDo: MouseDown
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            // ToDo: MouseUp
            base.OnMouseUp(e);
        }
        protected virtual void OnOuterRangeChanged(EventArgs e)
        {
            EventHandler eh = OuterRangeChanged;
            if (eh != null)
                eh(this, e);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            // ToDo: Paint
            base.OnPaint(e);
        }
        protected virtual void OnValueChanged(EventArgs e)
        {
            EventHandler eh = ValueChanged;
            if (eh != null)
                eh(this, e);
        }
    }
}

Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

QuestionAsp.Net MVC2 in VS 2010 Pin
Darrall14-Jun-10 12:03
Darrall14-Jun-10 12:03 
AnswerRe: Asp.Net MVC2 in VS 2010 Pin
Al Beback14-Jun-10 17:15
Al Beback14-Jun-10 17:15 
GeneralRe: Asp.Net MVC2 in VS 2010 Pin
Darrall15-Jun-10 3:37
Darrall15-Jun-10 3:37 
Questioncreating an unsafe array in C# Pin
Keith Vitali14-Jun-10 10:45
Keith Vitali14-Jun-10 10:45 
AnswerRe: creating an unsafe array in C# Pin
Luc Pattyn14-Jun-10 10:56
sitebuilderLuc Pattyn14-Jun-10 10:56 
GeneralRe: creating an unsafe array in C# Pin
Keith Vitali15-Jun-10 3:40
Keith Vitali15-Jun-10 3:40 
GeneralRe: creating an unsafe array in C# Pin
Luc Pattyn15-Jun-10 3:45
sitebuilderLuc Pattyn15-Jun-10 3:45 
AnswerRe: creating an unsafe array in C# Pin
DaveyM6914-Jun-10 11:10
professionalDaveyM6914-Jun-10 11:10 
GeneralRe: creating an unsafe array in C# Pin
Luc Pattyn14-Jun-10 11:17
sitebuilderLuc Pattyn14-Jun-10 11:17 
GeneralRe: creating an unsafe array in C# Pin
DaveyM6914-Jun-10 11:49
professionalDaveyM6914-Jun-10 11:49 
GeneralRe: creating an unsafe array in C# Pin
Luc Pattyn14-Jun-10 11:52
sitebuilderLuc Pattyn14-Jun-10 11:52 
Questionusing statement equivalent Pin
Al Beback14-Jun-10 5:06
Al Beback14-Jun-10 5:06 
AnswerRe: using statement equivalent Pin
Eddy Vluggen14-Jun-10 5:16
professionalEddy Vluggen14-Jun-10 5:16 
GeneralRe: using statement equivalent Pin
Al Beback14-Jun-10 5:24
Al Beback14-Jun-10 5:24 
GeneralRe: using statement equivalent Pin
Eddy Vluggen14-Jun-10 5:55
professionalEddy Vluggen14-Jun-10 5:55 
AnswerRe: using statement equivalent Pin
J4amieC14-Jun-10 5:22
J4amieC14-Jun-10 5:22 
GeneralRe: using statement equivalent Pin
Al Beback14-Jun-10 5:51
Al Beback14-Jun-10 5:51 

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.