CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Apr 2006
    Location
    BANGALORE,INDIA
    Posts
    75

    Starting Timer In Vc++

    i want to start a timer when the dialog box is loaded. the timer should be displayed in a edit control. timer shud start in this format

    00:00:00

    eg:- 00:00:03

    it shud go on till the dialog box is closed. pls let me know how this can be done

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Starting Timer In Vc++

    Look at ::SetTimer().

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Starting Timer In Vc++

    use function
    SetTimer() and handle the message WM_TIMER in your code.

  4. #4
    Join Date
    Apr 2006
    Location
    BANGALORE,INDIA
    Posts
    75

    Re: Starting Timer In Vc++

    Where Can I Find The Timer Control? Its Not Present In The Toolbar That Is Displayed Normalyy. Should I Add The Timer Control Separately

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Starting Timer In Vc++

    The timer is not a control. . it's a function as humptydumpty said.. .

  6. #6
    Join Date
    Apr 2006
    Location
    BANGALORE,INDIA
    Posts
    75

    Re: Starting Timer In Vc++

    i have put the code as follows
    BOOL CStatusDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
    UpdateData( TRUE );
    m_TIME = SetTimer(1, 2000, 0);
    UpdateData( FALSE );
    return TRUE;
    }
    is this rite? m_TIME is a member variable of a edit control. i gave this but nothing is displayed in edit control. i want the timer to start of as soon as the dialog box is loaded.

  7. #7
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Starting Timer In Vc++

    Take a look at this for reference.

  8. #8
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Starting Timer In Vc++

    Quote Originally Posted by sudeep_br3
    i have put the code as follows
    BOOL CStatusDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
    UpdateData( TRUE );
    m_TIME = SetTimer(1, 2000, 0);
    UpdateData( FALSE );
    return TRUE;
    }
    is this rite? m_TIME is a member variable of a edit control. i gave this but nothing is displayed in edit control. i want the timer to start of as soon as the dialog box is loaded.
    That looks fine, but you also need to handle WM_TIMER, as suggested by HD. You can look into the above link for reference that how you can do that.

  9. #9
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: Starting Timer In Vc++

    Just a side issue, even though your code will work, as Ejaz mentioned, you don't need the calls to UpdateData(...)

    Regards
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Starting Timer In Vc++

    Quote Originally Posted by sudeep_br3
    i have put the code as follows
    BOOL CStatusDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
    UpdateData( TRUE );
    m_TIME = SetTimer(1, 2000, 0);
    UpdateData( FALSE );
    return TRUE;
    }
    is this rite? m_TIME is a member variable of a edit control. i gave this but nothing is displayed in edit control. i want the timer to start of as soon as the dialog box is loaded.
    No. Look at the documentation for the return value of SetTimer. Why do you think that would be what you'd want in the edit control?

    Assuming you're counting elapsed seconds,

    Create a member variable of type CTime in your dialog.
    In OnInitDialog initialize it using CTime::GetCurrentTime;
    In OnInitDialog set a timer to go off every second.
    Handle the WM_TIMER message;
    Create a new CTime object initialized with CTime::GetCurrentTime.
    Subtract the first CTime that's a member of your dialog class from the new one you created in OnTimer. The result is a CTimeSpan.
    Use GetTotalSeconds of whatever methods you feel appropriate from your CTimeSpan object.
    Format them appropriately and use SetWindowText to put the value in your edit control.

  11. #11
    Join Date
    Apr 2006
    Location
    BANGALORE,INDIA
    Posts
    75

    Re: Starting Timer In Vc++

    the code is not working . can some one give me the correct and complete code

  12. #12
    Join Date
    Apr 2006
    Location
    BANGALORE,INDIA
    Posts
    75

    Re: Starting Timer In Vc++

    sir this procedure seems to be correct but can u give me the code?

  13. #13
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: Starting Timer In Vc++

    You have all the steps in this thread to help you on your way. Have you looked at the link golanshahar gave you? I remember msdn giving a few examples... have you also looked on MSDN regarding CTime and CTimeSpan?
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  14. #14
    Join Date
    Apr 2006
    Location
    BANGALORE,INDIA
    Posts
    75

    Re: Starting Timer In Vc++

    that is where i got this code from but its not working i mean nothing is apearing in edit control

  15. #15
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: Starting Timer In Vc++

    Have you done UpdateData()?
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

Page 1 of 2 12 LastLast

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