FOLLOW US
softpcapps Software CODE HELP BLOG

Shareware and free Open Source Windows Software Applications and free Online Tools

FourDots Time Position Custom UpDown Control

FourDots Time Position Custom UpDown Control Screenshot

We are developing a new video cutter application and we needed a control to set the start and end time position of the video cut.

We developed a new control for that for which we provide the source code.

At first you have to set a maximum and minimum timespan for the control. For a video min time is TimeSpan(0) and maximum the timespan that corresponds to the length of the video. For example if the video has a length of 1 hour 10 minutes and 30 seconds and 400 milliseconds, the maximum timespan is TimeSpan(0,1,10,30,400).

After the user for example enters the hours part of the time position the input cursor is automatically transferred to the next textbox control which is for the minutes e.t.c.

Moreover, the user can increase and increase the hours, minutes, seoncds, e.t.c. by clicking on the small arrow buttons that are next to the textboxes.

Also, the user can change the value of the hours, minutes, seconds e.t.c. with the mouse wheel. For that we used the following code :

In the constructor of the control :

			txtH.MouseWheel += txtH_MouseWheel;
            txtM.MouseWheel += txtM_MouseWheel;
            txtS.MouseWheel += txtS_MouseWheel;
            txtMS.MouseWheel += txtMS_MouseWheel;
 
and then the methods

	void txtMS_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                btnMSUpClick();
            }
            else if (e.Delta < 0)
            {
                btnMSDownClick();
            }
        }

        void txtS_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                btnSUpClick();
            }
            else if (e.Delta < 0)
            {
                btnSDownClick();
            }
            
        }

        void txtM_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                btnMUpClick();
            }
            else if (e.Delta < 0)
            {
                btnMDownClick();
            }
        }

        void txtH_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                btnHUpClick();
            }
            else if (e.Delta < 0)
            {
                btnHDownClick();
            }
        }
		
		private void btnHUpClick()
        {
            Hours++;
        }

        private void btnHDownClick()
        {
            if (Hours > 0)
            {
                Hours--;
            }
        }
		
		private void btnMUpClick()
        {
            if (Minutes < 59)
            {
                Minutes++;
            }
            else
            {
                int lasth = Hours;
                Hours++;
                bool success=(lasth != Hours);
               
                if (success)
                {
                    Minutes = 0;
                }
            }
        }

        private void btnMDownClick()
        {
            if (Minutes > 0)
            {
                Minutes--;
            }            
        }

        private void btnSUpClick()
        {            
            if (Seconds < 59)
            {
                Seconds++;
            }
            else
            {
                int lastm = Minutes;
                Minutes++;
                bool success = (lastm != Minutes);

                if (success)
                {
                    Seconds = 0;
                }
            }
        }

        private void btnSDownClick()
        {
            if (Seconds > 0)
            {
                Seconds--;
            }
        }

        private void btnMSUpClick()
        {
            if (MSeconds < 999)
            {
                MSeconds++;
            }
            else
            {
                int lasts = Seconds;
                Seconds++;
                bool success = (lasts != Seconds);

                if (success)
                {
                    MSeconds = 0;
                }
            }
        }

        private void btnMSDownClick()
        {
            if (MSeconds > 0)
            {
                MSeconds--;
            }
        }
 

We had to take extra care so that the value of seconds does not get less than 0 and greater than 59 the same for the minutes and for the milliseconds we had to restrict values between 0 and 999.

Also we wanted that once the user clicks and holds the left mouse button on the up arrow or down arrow button the value will be increase at a steady pace. Thereforew we added a MouseDown event handler that sets the tag of the control to the text "MouseDown" and a MouseUp event handler that sets the tag of the control to "MouseUp". Now when the user clicks for example on the up arrow button the value is increased immediately but we also wait for 700 milliseconds. If we did not wait then the value would be increased too much. We wait for 700 milliseconds with a special method named "WaitNMSeconds" and then we test if the tag of the control is still "MouseDown". If the user does not hold the mouse button anymore above the control the tag of it will be "MouseUp" so the value will not increase again. However, if the tag of the control still is "MouseDown" the value will be increased, because he is still holding the mouse button down on the up arrow control.

In the constructor of the control :

			btnHDown.MouseDown += btnTime_MouseDown;
            btnHDown.MouseUp += btnTime_MouseUp;
            btnHUp.MouseDown += btnTime_MouseDown;
            btnHUp.MouseUp += btnTime_MouseUp;

            btnMDown.MouseDown += btnTime_MouseDown;
            btnMDown.MouseUp += btnTime_MouseUp;
            btnMUp.MouseDown += btnTime_MouseDown;
            btnMUp.MouseUp += btnTime_MouseUp;

            btnSDown.MouseDown += btnTime_MouseDown;
            btnSDown.MouseUp += btnTime_MouseUp;
            btnSUp.MouseDown += btnTime_MouseDown;
            btnSUp.MouseUp += btnTime_MouseUp;

            btnMSDown.MouseDown += btnTime_MouseDown;
            btnMSDown.MouseUp += btnTime_MouseUp;
            btnMSUp.MouseDown += btnTime_MouseDown;
            btnMSUp.MouseUp += btnTime_MouseUp;
and the methods :

		void btnTime_MouseDown(object sender, MouseEventArgs e)
        {
            Button btn = (Button)sender;

            btn.Tag = "MouseDown";

            if (btn == btnHUp)
            {
                btnHUpClick();
            }
            else if (btn == btnHDown)
            {
                btnHDownClick();
            }
            else if (btn == btnMUp)
            {
                btnMUpClick();
            }
            if (btn == btnMDown)
            {
                btnMDownClick();
            }
            if (btn == btnSUp)
            {
                btnSUpClick();
            }
            if (btn == btnSDown)
            {
                btnSDownClick();
            }
            if (btn == btnMSUp)
            {
                btnMSUpClick();
            }
            if (btn == btnMSDown)
            {
                btnMSDownClick();
            }            

            while (btn.Tag.ToString() == "MouseDown")
            {
                WaitNMSeconds(700);

                if (btn.Tag.ToString() == "MouseDown")
                {
                    if (btn == btnHUp)
                    {
                        btnHUpClick();
                    }
                    else if (btn == btnHDown)
                    {
                        btnHDownClick();
                    }
                    else if (btn == btnMUp)
                    {
                        btnMUpClick();
                    }
                    if (btn == btnMDown)
                    {
                        btnMDownClick();
                    }
                    if (btn == btnSUp)
                    {
                        btnSUpClick();
                    }
                    if (btn == btnSDown)
                    {
                        btnSDownClick();
                    }
                    if (btn == btnMSUp)
                    {
                        btnMSUpClick();
                    }
                    if (btn == btnMSDown)
                    {
                        btnMSDownClick();
                    }
                }
            }
        }

        void btnTime_MouseUp(object sender, MouseEventArgs e)
        {
            Button btn = (Button)sender;
            btn.Tag = "MouseUp";
        }
		
		private void WaitNMSeconds(int mseconds)
        {
            if (mseconds < 1) return;
            DateTime _desired = DateTime.Now.AddMilliseconds(mseconds);
            while (DateTime.Now < _desired)
            {
                System.Windows.Forms.Application.DoEvents();
            }
        }

Also, it is worth to note that when we change the hours, minutes, seconds e.t.c. we test if the new value will be between the minumum and maximum timespan that we have set. If not, we do not allow the change. For that we use the following code :


	public int Hours
        {
            get
            {
                return _Hours;
            }
            set
            {
                TimeSpan ts = new TimeSpan(0,value, _Minutes, _Seconds, _MSeconds);

                if (ts.CompareTo(MaxTime) <= 0 && ts.CompareTo(MinTime) >= 0)
                {                    
                    _Hours = value;
                    FillTimeTextBoxes();
                }

            }
        }
		
		public int Hours
        {
            get
            {
                return _Hours;
            }
            set
            {
                TimeSpan ts = new TimeSpan(0,value, _Minutes, _Seconds, _MSeconds);

                if (ts.CompareTo(MaxTime) <= 0 && ts.CompareTo(MinTime) >= 0)
                {                    
                    _Hours = value;
                    FillTimeTextBoxes();
                }

            }
        }

        public int Minutes
        {
            get
            {
                return _Minutes;
            }
            set
            {
                TimeSpan ts = new TimeSpan(0, _Hours,value, Seconds, _MSeconds);

                if (ts.CompareTo(MaxTime) <= 0 && ts.CompareTo(MinTime) >= 0)
                {
                    _Minutes = value;
                    FillTimeTextBoxes();
                }

            }
        }

        public int Seconds
        {
            get
            {
                return _Seconds;
            }
            set
            {
                TimeSpan ts = new TimeSpan(0, _Hours, _Minutes, value, _MSeconds);

                if (ts.CompareTo(MaxTime) <= 0 && ts.CompareTo(MinTime) >= 0)
                {
                    _Seconds = value;
                    FillTimeTextBoxes();
                }

            }
        }

        public int MSeconds
        {
            get
            {
                return _MSeconds;
            }
            set
            {
                TimeSpan ts = new TimeSpan(0, _Hours, _Minutes, _Seconds, value);

                if (ts.CompareTo(MaxTime) <= 0 && ts.CompareTo(MinTime) >= 0)
                {
                    _MSeconds = value;
                    FillTimeTextBoxes();
                }

            }
        }

Download Demo Project

FourDots Time Position Custom UpDown Control Demo Project