Update Edit Box text
-
I'm not much of C++ programmer but am just putting together a very simple Dialog in Visual C++ 6.0. It's one button and two Edit Boxes. From the first I get user input and the second I use for user feedback. To the press button message handler I have added a function that is looping and for each loop I'd like to update the message in the second Edit Box. The code looks something like this: void CAppGUIDlg::OnButton1() { CAppGUIDlg::DoSomething(); } void CAppGUIDlg::DoSomething() { int *string; int max; char *tmpStr = ""; int total = 0; max = atoi(m_editStr1); //Get Edit Box 1 value while (total < max ) { //Do something with value from Edit Box 1 sprintf(tmpStr,"User message %i", total/1024); UpdateData(false); m_editStr2 = _T(tmpStr); //feedback the result to Edit Box2 Sleep(200); UpdateData(false); } } What will happen is that Edit Box 2 will not be updated until the loop is finished. Is there a simple way to update it within every loop?
-
I'm not much of C++ programmer but am just putting together a very simple Dialog in Visual C++ 6.0. It's one button and two Edit Boxes. From the first I get user input and the second I use for user feedback. To the press button message handler I have added a function that is looping and for each loop I'd like to update the message in the second Edit Box. The code looks something like this: void CAppGUIDlg::OnButton1() { CAppGUIDlg::DoSomething(); } void CAppGUIDlg::DoSomething() { int *string; int max; char *tmpStr = ""; int total = 0; max = atoi(m_editStr1); //Get Edit Box 1 value while (total < max ) { //Do something with value from Edit Box 1 sprintf(tmpStr,"User message %i", total/1024); UpdateData(false); m_editStr2 = _T(tmpStr); //feedback the result to Edit Box2 Sleep(200); UpdateData(false); } } What will happen is that Edit Box 2 will not be updated until the loop is finished. Is there a simple way to update it within every loop?
-
I'm not much of C++ programmer but am just putting together a very simple Dialog in Visual C++ 6.0. It's one button and two Edit Boxes. From the first I get user input and the second I use for user feedback. To the press button message handler I have added a function that is looping and for each loop I'd like to update the message in the second Edit Box. The code looks something like this: void CAppGUIDlg::OnButton1() { CAppGUIDlg::DoSomething(); } void CAppGUIDlg::DoSomething() { int *string; int max; char *tmpStr = ""; int total = 0; max = atoi(m_editStr1); //Get Edit Box 1 value while (total < max ) { //Do something with value from Edit Box 1 sprintf(tmpStr,"User message %i", total/1024); UpdateData(false); m_editStr2 = _T(tmpStr); //feedback the result to Edit Box2 Sleep(200); UpdateData(false); } } What will happen is that Edit Box 2 will not be updated until the loop is finished. Is there a simple way to update it within every loop?
Either: Do each thing in a different thread. Or (more simply for this case) Pump messages in your loop. Change your loop to: while (total < max ) { //Do something with value from Edit Box 1 sprintf(tmpStr,"User message %i", total/1024); UpdateData(false); if(!PeekAndPump()) break; m_editStr2 = _T(tmpStr); //feedback the result to Edit Box2 Sleep(200); UpdateData(false); } Then add this function to your class: BOOL PeekAndPump() { MSG dlgMsg; if (::PeekMessage (&dlgMsg, NULL, 0, 0, PM_NOREMOVE)) { if (!AfxGetApp ()->PumpMessage ()) { ::AfxPostQuitMessage (0); return FALSE; } return TRUE; } LONG lIdle = 0; while (AfxGetApp ()->OnIdle (lIdle++)); return FALSE; }
-
if you want to send a message to an edit box just use SetDlgItemText function and you have only 2 parameters in MFC VC++. gabby
-
Either: Do each thing in a different thread. Or (more simply for this case) Pump messages in your loop. Change your loop to: while (total < max ) { //Do something with value from Edit Box 1 sprintf(tmpStr,"User message %i", total/1024); UpdateData(false); if(!PeekAndPump()) break; m_editStr2 = _T(tmpStr); //feedback the result to Edit Box2 Sleep(200); UpdateData(false); } Then add this function to your class: BOOL PeekAndPump() { MSG dlgMsg; if (::PeekMessage (&dlgMsg, NULL, 0, 0, PM_NOREMOVE)) { if (!AfxGetApp ()->PumpMessage ()) { ::AfxPostQuitMessage (0); return FALSE; } return TRUE; } LONG lIdle = 0; while (AfxGetApp ()->OnIdle (lIdle++)); return FALSE; }
-
Wont help. If your looping around in that one function, you can do what you want to the other edit box, it wont redraw until you return from that function and contine to pump messages.
-
By updating the edit control (either by using
SetDlgItemText()
orUpdateData()
) every 200 ms, none of the updates will be seen except for the last one after the loop terminates.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
Try it - it doesnt. Yes SetDlgItemText changes the text - though really its the Win32 way of doing things. The way he has done it with UpdateData basically calls SetDlgItemText anyway under the hood - just using the DDX/DDV map to work out what to map to what. But just like UpdateData, SetDlgItemText will set the text after the loop has returned. If you are processing a message spinning in a loop then no other messages will come through when you are doing that (specifically, WM_PAINT's) until you return from that loop. Hence, yes u have changed the text in the edit box with SetDlgItemText but the edit box wont redraw until your function returns.