Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC++
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.
 
// 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 5-Dec-12 2:57am
Edited 5-Dec-12 3:00am
v2
Comments
ThatsAlok at 5-Dec-12 9:31am
   
will this SetDlgItemText work?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
J.Surjith Kumar at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

OK you need to do this:
 
 
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);
}
  Permalink  
v2
Comments
J.Surjith Kumar at 5-Dec-12 9:43am
   
ThankYou BadJerry its working :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use SetDlgItemText, it's Dialogbox specific function! Link[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 285
1 DamithSL 265
2 CPallini 195
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100