Updating Edit Every Second

This is a discussion on Updating Edit Every Second within the Windows Programming forums, part of the Platform Specific Boards category; I have something that looks like this: Code: while(time >= 0) { time -= 1; SetDlgItemInt(hwnd, IDC_TIME, time, false); Sleep(1000); ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Updating Edit Every Second

    I have something that looks like this:

    Code:
    while(time >= 0)
    {
         time -= 1;
         SetDlgItemInt(hwnd, IDC_TIME, time, false);
         Sleep(1000);
    }
    And, it changes the variable every second, but it wont update the edit box until time is 0, does anyone know why or how i can fix this?
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Sleep() actually halts the program. why dont you try using timers?

    have a look at SetTimer() and KillTimer() in the windows API, and do something like this:

    Code:
    SetTimer(hwnd, TIMER_ID, 1000 /* 1 second */, NULL);
    and in your WndProc:

    Code:
    switch(uMsg)
    {
       ...
       case WM_TIMER:
       {
          if(wParam == TIMER_ID)
          {
             time--;
             SetDlgItemInt(hwnd, IDC_TIME, time, false);
             if(time == 0)
             {
                // game over!
                KillTimer(hwnd, TIMER_ID);
             }
          }
          break;
       }
       ...
    }
    hope this helps
    U.

    edit: i didn't compile this code... it's just here for ideas
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Unregistered
    Guest
    You can try UpdateData(TRUE);
    or
    UpdateData(FALSE);

    I never remeber wich one it is. One is to read from and the other one is to read to.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    i have this:

    Code:
    	case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    			case IDC_START:
    				{
    					time = GetDlgItemInt(hwnd, IDC_TIME, NULL, false);
    					
    					EnableWindow(hTime, false);
    					EnableWindow(hLogoff, false);
    					EnableWindow(hShutdown, false);
    					EnableWindow(hRestart, false);
    					EnableWindow(hStart, false);
    
    					SetTimer(hwnd, IDC_TIMER, 1000, NULL);
    
    				}
    				break;
    
    			case WM_TIMER:
    				{
    					switch(wParam)
    					{
    					case IDC_TIMER:
    						{
    							time--;
    							SetDlgItemInt(hwnd, IDC_TIME, time, false);
    							
    							if(time == 0)
    							{
    								EnableWindow(hTime, true);
    								EnableWindow(hLogoff, true);
    								EnableWindow(hShutdown, true);
    								EnableWindow(hRestart, true);
    								EnableWindow(hStart, true);
    
    								KillTimer(hwnd, IDC_TIMER);
    							}
    						}
    						break;
    					}
    				}
    				break;
    It compiles fine, but it doesnt update the edit box
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Err nevermind i found my problem, i put the WM_TIMER in the WM_COMMAND, WOOPS

    Err now i have another problem, the number in the textbox updates, but goes to a really big number...

    Code:
    	case WM_TIMER:
    		{
    			switch(wParam)
    			{
    			case IDC_TIMER:
    				{
    					time--;
    					SetDlgItemInt(hwnd, IDC_TIME, time, false);
    					
    					if(time == 0)
    					{
    						EnableWindow(hTime, true);
    						EnableWindow(hLogoff, true);
    						EnableWindow(hShutdown, true);
    						EnableWindow(hRestart, true);
    						EnableWindow(hStart, true);
    						KillTimer(hwnd, IDC_TIMER);
    					}
    				}
    				break;
    			}
    		}	
    		break;
    Last edited by Okiesmokie; 05-13-2002 at 06:21 PM.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    alli can think of it kill the timer before you disable/enable all those windows (not that it'll make a difference).... and make sure you initialise the value for time. coz if it's zero, then the moment you do time-- it'll become really big!

    so set time to something other than zero first....

    other than that... if you have problems still, post a link to the code and i'll see what i can do for ya.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 12:56 PM
  2. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 07:58 AM
  3. Replies: 3
    Last Post: 07-23-2005, 09:00 AM
  4. Access database not updating correctly
    By MPSoutine in forum Windows Programming
    Replies: 1
    Last Post: 12-15-2003, 11:38 PM
  5. Edit Box not updating in MFC App
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 01-29-2002, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21