none
How to update edit box instantly?? RRS feed

  • 質問

  • Hi,

    I have tried to search through the internet for this issue. Is it possible to display the test instantly after doing the command below in visual c++ 6:

    m_sMessage = "Simulation starts.";
    UpdateData(FALSE);

    After I step through UpdateData(FALSE), the test stored in CString m_sMessage(member of my editbox) is not being updated into the editbox immediately. The text will only be added after the application has run through all the codes. Is there any command I can add after the UpdateDATA command to show the text in the edit box immediately? I know this might sound very silly to the expert but it has wasted a lot of my time to search in internet for the solution. Hopefully I can get a reply regarding this issue, sorry for asking such an easy question. :)
    2008年7月10日 11:34

回答

  • nomaikai:

    Did you try SetWindowText()? UpdateData(FALSE) is not a good way to modify the content of a single control, and some say that it is a very poor methodology in general.

    But really, you should not be doing a long calculation in your main thread, because it makes your GUI unresponsive in general. For example if the user covers your window with another application, and then uncovers it, your window will not repaint itself.

    Long calculations should be done in a separate thread.


    David Wilkinson | Visual C++ MVP
    • 回答としてマーク Yan-Fei Wei 2008年7月16日 8:00
    2008年7月10日 11:44

すべての返信

  • Call UpdateWindow() to force the control to repaint itself.
    Hans Passant.
    2008年7月10日 11:42
    モデレータ
  • Call UpdateWindow() to force the control to repaint itself.
    Hans Passant.
    2008年7月10日 11:42
    モデレータ
  • nomaikai:

    Did you try SetWindowText()? UpdateData(FALSE) is not a good way to modify the content of a single control, and some say that it is a very poor methodology in general.

    But really, you should not be doing a long calculation in your main thread, because it makes your GUI unresponsive in general. For example if the user covers your window with another application, and then uncovers it, your window will not repaint itself.

    Long calculations should be done in a separate thread.


    David Wilkinson | Visual C++ MVP
    • 回答としてマーク Yan-Fei Wei 2008年7月16日 8:00
    2008年7月10日 11:44
  • Hi nobugz,

    Thanks for the reply and answer.

    Hi David,

    Thanks for the suggestion. I faced the problem you mentioned. The program is not developed by me. It was designed by another person previously. I just take over the program. I am having problem to redesign the thing as the program is too long and not organized systematically. However, I will try the way you suggest once I have free time and understand flow of the whole program . Another reason is I am not very good in visual c++ and code planning. :P

    2008年7月11日 17:36
  • Hi David,
    I have the same problem but in VC++2008 Here is my code

    void Ctest4Dlg::OnBnClickedButton1()

    {

    for (int i=0;i<20;++i){

    double erro_count=ptr_tb->Get_erro(chann);

    Sleep(5000);

    tring.Format(_T("%d"),erro_count );

    pWnd = GetDlgItem(IDC_EDIT3);

    pWnd->SetWindowText(tring);

    Sleep(5000);

    }}

    it updates until finishing the loop
    any commad to invoke the application ?
    some thing simular like application . event  in C#?

    thanks  

    2008年9月13日 1:31
  • biginner:

    pWnd->UpdateWindow() should make your edit box update inside the loop.

    But, really, using Sleep() is not the right way to go here (it almost never is). While your app is sleeping, it will not be able to repaint itself if another window covers it, and then uncovers it, and the user will not be able to interact with your window in any way.

    The simplest solution here would be a timer. See

    CWnd::SetTimer()
    CWnd::KillTimer()
    CWnd::OnTimer()


    David Wilkinson | Visual C++ MVP
    • 編集済み davewilk 2008年9月13日 9:28 Typo
    2008年9月13日 8:06
  • Call:

    UpdateWindow();
    Sleep(100);

    The purpose of calling Sleep() is to give the control and resources back to the operating system, so other applications are easily used.


    Michael Haephrati

    2019年8月27日 10:10
  • Did you seriously not notice that this question was asked eleven years ago?  The person who asked it is probably long gone.

    And your advice is not good.  As David said in his reply, Sleep is almost never the right answer.  The operating system scheduler runs every 16ms and will give other processes an opportunity to run.

    The RIGHT answer, as David said, is to do the computations in a separate thread and let the UI thread keep things up to date.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    2019年8月27日 17:00