CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Create a Timer / Stopwatch that records to SQL ?

    When you start on a project like this, I'd recommend that you work out the control states and logic first before adding the more complex features like the timer and database functionality.

    What I like to do is create an enum that tells me what mode the form is in (start, stop, reset, etc.).

    I then create button handlers to buttons and set the mode appropriate to the button. To make things easier, I set the control states (i.e whether a button or control is enabled/disabled) in one method and also call this method in each of the button handlers.

    Using this approach you can very quickly set up what buttons and controls are enabled based on which button you click. You can also debug this very quickly because the all the enabling/disabling of controls is done in one method (and the method is simple).

    Code:
    using System;
    using System.Windows.Forms;
    
    namespace CG.Timer
    {
        public partial class Form1 : Form
        {
            /// <summary>
            /// Enum to track states
            /// </summary>
            private enum TimerMode
            {
                /// <summary>
                /// Initial state when form opens
                /// </summary>
                Initial,
                /// <summary>
                /// State after reset button has been pressed
                /// </summary>
                Reset,
                /// <summary>
                /// State after start button has been pressed
                /// </summary>
                Start,
                /// <summary>
                /// State after stop button has been pressed
                /// </summary>
                Stop
            } 
    
            public Form1( )
            {
                InitializeComponent( );
    
                SetControlStates( );
            }
    
            #region Event Handlers
    
            private void _start_Click( object sender, EventArgs e )
            {
                SetTimerMode( TimerMode.Start );
            }
    
            private void _stop_Click( object sender, EventArgs e )
            {
                SetTimerMode( TimerMode.Stop );
            }
    
            private void _reset_Click( object sender, EventArgs e )
            {
                SetTimerMode( TimerMode.Reset );
            }
    
            private void _cancel_Click( object sender, EventArgs e )
            {
                Close( );
            }
    
            private void _tbUserName_TextChanged( object sender, EventArgs e )
            {
                SetControlStates( );
    
                _tbUserName.Focus( );
            }
    
            #endregion Event Handlers
    
            #region Private Methods
    
            /// <summary>
            /// Sets the enabled/disabled control states based on the current timer mode.
            /// </summary>
            private void SetControlStates( )
            {
                _start.Enabled = false;
                _stop.Enabled = false;
                _reset.Enabled = false;
                _tbUserName.Enabled = false;
    
                switch ( _currentMode )
                {
                case TimerMode.Initial:
                    _start.Enabled = _tbUserName.TextLength > 0;
                    _tbUserName.Enabled = true;
                    break;
                case TimerMode.Reset:
                    _start.Enabled = _tbUserName.TextLength > 0;
                    _tbUserName.Enabled = true;
                    break;
                case TimerMode.Start:
                    _stop.Enabled = true;
                    break;
                case TimerMode.Stop:
                    _reset.Enabled = true;
                    break;
                }
            }
    
            private void SetTimerMode( TimerMode mode )
            {
                _currentMode = mode;
    
                SetControlStates( );
            }
            
            #endregion Private Methods
            
            private TimerMode _currentMode = TimerMode.Initial;
        }
    }
    See the attachment for the complete sln.

    Tomorrow, I'll add the timer functionality.
    Attached Files Attached Files

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Creiate a Timer / Stopwatch that records to SQL ?

    In this version, I've added a timer and wired it up to the start/stop buttons as well as updated the status field on each timer click. I just format a string and have a _timerCount value that gets set to the status text.

    To complete your project, I believe all you'll need to do is add your database saving code to the timer click (which is fine for a simple school project, but not how you would do it in the real world).

    Code with timer added and updated Status field
    Code:
    using System;
    using System.Windows.Forms;
    
    namespace CG.Timer
    {
        public partial class Form1 : Form
        {
            /// <summary>
            /// Enum to track states
            /// </summary>
            private enum TimerMode
            {
                /// <summary>
                /// Initial state when form opens
                /// </summary>
                Initial,
                /// <summary>
                /// State after reset button has been pressed
                /// </summary>
                Reset,
                /// <summary>
                /// State after start button has been pressed
                /// </summary>
                Start,
                /// <summary>
                /// State after stop button has been pressed
                /// </summary>
                Stop
            } 
    
            public Form1( )
            {
                InitializeComponent( );
    
                SetControlStates( );
            }
    
            #region Event Handlers
    
            private void _start_Click( object sender, EventArgs e )
            {
                SetTimerMode( TimerMode.Start );
            }
    
            private void _stop_Click( object sender, EventArgs e )
            {
                SetTimerMode( TimerMode.Stop );
            }
    
            private void _reset_Click( object sender, EventArgs e )
            {
                SetTimerMode( TimerMode.Reset );
            }
    
            private void _cancel_Click( object sender, EventArgs e )
            {
                Close( );
            }
    
            private void _tbUserName_TextChanged( object sender, EventArgs e )
            {
                SetControlStates( );
    
                _tbUserName.Focus( );
            }
    
            private void _timer_Tick( object sender, EventArgs e )
            {
                _tbStatus.Text = String.Format("Running: {0}", _timerCount++);
    
                // Add Database Call here.
            }
    
            #endregion Event Handlers
    
            #region Private Methods
    
            /// <summary>
            /// Sets the enabled/disabled control states based on the current timer mode.
            /// </summary>
            private void SetControlStates( )
            {
                _start.Enabled = false;
                _stop.Enabled = false;
                _reset.Enabled = false;
                _tbUserName.Enabled = false;
                
    
                switch ( _currentMode )
                {
                case TimerMode.Initial:
                    _start.Enabled = _tbUserName.TextLength > 0;
                    _tbUserName.Enabled = true;
                    break;
                case TimerMode.Reset:
                    _timerCount = 0;
                    _start.Enabled = _tbUserName.TextLength > 0;
                    _tbUserName.Enabled = true;
                    _tbStatus.Text = String.Empty;
                    break;
                case TimerMode.Start:
                    _timerCount = 0;
                    _stop.Enabled = true;
                    _timer.Start();
                    break;
                case TimerMode.Stop:
                    _reset.Enabled = true;
                    _timer.Stop( );
                    _tbStatus.Text = String.Format( "Stopped: {0}", _timerCount++ );
                    break;
                }
            }
    
            private void SetTimerMode( TimerMode mode )
            {
                _currentMode = mode;
    
                SetControlStates( );
            }
            
            #endregion Private Methods
            
            private TimerMode _currentMode = TimerMode.Initial;
            private long _timerCount;
        }
    }
    See the attachment for the latest source.
    Attached Files Attached Files

  3. #18
    Join Date
    Dec 2010
    Posts
    11

    Re: Creiate a Timer / Stopwatch that records to SQL ?

    yes I know how to zip. I can do that from here on out. I"ll check out your example.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured