Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the Timer in my application. I displayed the current system time and currently running application time using the SetTimer(1,999,NULL). I created one text box and start button.
When I enter the integer value in the text box (ex: 10) and press the start button, the message box should pop-up after 10 seconds. For this I created another SetTimer(2,m_Sec*1000,NULL). Everything was fine but when I execute the application for every sec the text box is refreshed.
I can't enter the value in that text box.

What changes I have to make.

Here is my code.

m_Sec is the UINT variable of the text box.

C++
// TickCounterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TickCounter.h"
#include "TickCounterDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CTickCounterDlg dialog

CTickCounterDlg::CTickCounterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTickCounterDlg::IDD, pParent)
	, m_Comp(_T(""))
	, m_App(_T(""))
	, m_Sec(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTickCounterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMP_TIME, m_CompTime);
	DDX_Control(pDX, IDC_APP_TIME, m_AppTime);
	DDX_Text(pDX, IDC_COMP_TIME, m_Comp);
	DDX_Text(pDX, IDC_APP_TIME, m_App);

	DDX_Text(pDX, IDC_SEC, m_Sec);
}

BEGIN_MESSAGE_MAP(CTickCounterDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
	
	ON_BN_CLICKED(IDC_START, &CTickCounterDlg::OnBnClickedStart)
END_MESSAGE_MAP()


// CTickCounterDlg message handlers

BOOL CTickCounterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	CompTime = CTime::GetTickCount();
SetTimer(1, 999, NULL);
SetTimer(2,10000,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTickCounterDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<wparam>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTickCounterDlg::OnQueryDragIcon()
{
	return static_cast<hcursor>(m_hIcon);
}


void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
	CTime CurTime=CTime::GetTickCount();
	CTimeSpan Difference=CurTime-CompTime;
	unsigned int compHour,compMin,compSec;
	unsigned int curSec,curMin,curHour;
	compHour=Difference.GetHours();
	compMin=Difference.GetMinutes();
	compSec=Difference.GetSeconds();
	curHour=CurTime.GetHour();
	curMin=CurTime.GetMinute();
	curSec=CurTime.GetSecond();
m_Comp.Format(L"This computer current Time is %d hours, %d minutes %d seconds", curHour, curMin, curSec);
m_App.Format(L"This application has been running for %d hours, %d minutes %d seconds", compHour, compMin, compSec);
}
if(nIDEvent==2)
{
MessageBox(L"hai");
KillTimer(2);
}
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}


void CTickCounterDlg::OnBnClickedStart()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	SetTimer(2,m_Sec*1000,NULL);
}
Posted
Updated 5-Dec-12 2:00am
v2
Comments
ThatsAlok 5-Dec-12 9:31am    
will this SetDlgItemText work?

The UpdateData(False) will refresh all controls (and fill them with the content held in the member variable)
You could add UpdateData(true) at the beginning of OnTimer or better, Call SetWindowText on the controls you want to update rather than calling UpdateData!
Hope this helps
 
Share this answer
 
Comments
J.Surjith Kumar 5-Dec-12 8:33am    
I have to enter the value at the run time. To make only that text control not to refresh what i have to do.
J.Surjith Kumar 5-Dec-12 8:43am    
If i add UpdateData(TRUE) at the beginning of OnTimer the message box will popup automatically even the start button was not pressed.
OK you need to do this:

C#
void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
	CTime CurTime=CTime::GetTickCount();
	CTimeSpan Difference=CurTime-CompTime;
	unsigned int compHour,compMin,compSec;
	unsigned int curSec,curMin,curHour;
	compHour=Difference.GetHours();
	compMin=Difference.GetMinutes();
	compSec=Difference.GetSeconds();
	curHour=CurTime.GetHour();
	curMin=CurTime.GetMinute();
	curSec=CurTime.GetSecond();
	CString strComp,strApp;
        strComp.Format(L"This computer current Time is %d hours, %d minutes %d seconds", curHour, curMin, curSec);
	m_CompTime.SetWindowText(strComp);
	strApp.Format(L"This application has been running for %d hours, %d minutes %d seconds", compHour, compMin, compSec);
	m_appTime.SetWindowText(strApp);
}
if(nIDEvent==2)
{
MessageBox(L"hai");
KillTimer(2);
}

CDialog::OnTimer(nIDEvent);
}
 
Share this answer
 
v2
Comments
J.Surjith Kumar 5-Dec-12 9:43am    
ThankYou BadJerry its working :)
Use SetDlgItemText, it's Dialogbox specific function! Link[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900