CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Mar 2006
    Posts
    63

    [RESOLVED] Dialog : How to update an Edit Box?

    VC++6.
    I have a simple Dialog app. There is one Edit Box, updated in a function triggered by a Button. In the function, the Edit Box's CString member variable is modified twice, firstly as "a", then with "b". I have added a wait of 1 second between the two settings (to simulate some data collection tasks).
    Whether I use
    SetDlgItemText(IDC_DATA,m_data);
    or
    UpdateData(false) or UpdateData(NULL)
    in conjunction with, or without
    Invalidate();
    only the text "b" appears in the Edit Box.
    If however I insert
    AfxMessageBox("paws");
    after UpdateData(false), the text "a" does indeed appear in the Edit Box, and on quitting the AfxMessageBox, the text "b" appears in the Edit Box.
    Obviously Winclows needs to perceive some or all of the Dialog Window as needing to be repainted - as evinced by the inclusion of the AfxMessageBox, which forces a repaint, but I cannot find a method which does this seamlessly.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Dialog : How to update an Edit Box?

    I didn't understand why you need to modify your editbox
    Quote Originally Posted by crasher
    ...twice, firstly as "a", then with "b"
    however, the simplest way to implement is, IMHO, would be modifying it from within the OnTimer message handler.
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Dialog : How to update an Edit Box?

    Forget about what are you doing, and tell us what you want to achieve. If you don't append to the edit text, then it's normal that you only see the last updated text.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Mar 2006
    Posts
    63

    Re: Dialog : How to update an Edit Box?

    I have an application which communicates with some measurement hardware, and I want to display the readings from that hardware, which consist of several digits. I do not need a sophisticated display - a humble text box will do nicely. I do not want to scroll the output into a document view. Having tried and failed to get the Edit Box to work in my real app, I created a simple test app which illustrates the problem.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Dialog : How to update an Edit Box?

    It may be that if your app is cpu bound, Windows just isn't getting a chance to update the edit control. Either include a message pump (PeekMessage followed by DispatchMessage) or call UpdateWindow on your edit control and see if that helps.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Dialog : How to update an Edit Box?

    1) if you don't need to edit the data, and don't need to be able to copy/paste from the data. Then using a static control is a better solution.

    2) You're only seeing the last value because in between value changes no redraw is being forced.
    Set the text and then force a redraw by calling UpdateWindow()




    It'll work, but it is a "cludge" solution though. The main issue is that your application isn't responsive until you return to the normal message loop.
    For a 'quick and dirty' program you only use yourself, it'll probably make do. For a program you intend to publish it's bad practice and you should reorganise to not multiple 'overwriting' updates to the same control within a single messagehandling.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Dialog : How to update an Edit Box?

    Quote Originally Posted by crasher View Post
    ... I do not want to scroll the output into a document view. Having tried and failed to get the Edit Box to work in my real app...
    Well editbox is not the best choice for logging. Di you consider using List box instead? Have a look at A Logging ListBox Control
    Victor Nijegorodov

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Dialog : How to update an Edit Box?

    Quote Originally Posted by crasher View Post
    I have added a wait of 1 second between the two settings (to simulate some data collection tasks).
    How are you waiting? If you do it in the main thread, the user interface won't be redrawn until the function exits, because the thread is busy.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Dialog : How to update an Edit Box?

    The problem you have is that UpdateData, Invalidate, SetDlgItemText, and other similar calls actually perform what they are supposed to do, but the edit control itself does not repaint (with the new text) until the windows messages are processed. While you are in your function (waiting with a sleep or whatever) those windows messages are not processed.

    However, you can force the window to repaint by using the UpdateWindow function.
    Code:
    SetDlgItemText (IDC_WHATEVER, _T("Text to be displayed."));
    GetDlgItem(IDC_WHATEVER)->UpdateWindow();
    // do your other stuff here - the window has already been updated
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Dialog : How to update an Edit Box?

    Quote Originally Posted by krmed View Post
    The problem you have is that UpdateData, Invalidate, SetDlgItemText, and other similar calls actually perform what they are supposed to do, but the edit control itself does not repaint (with the new text) until the windows messages are processed. While you are in your function (waiting with a sleep or whatever) those windows messages are not processed.

    However, you can force the window to repaint by using the UpdateWindow function.
    Code:
    SetDlgItemText (IDC_WHATEVER, _T("Text to be displayed."));
    GetDlgItem(IDC_WHATEVER)->UpdateWindow();
    // do your other stuff here - the window has already been updated
    Hope that helps.
    Didn't I say that already?

  11. #11
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Dialog : How to update an Edit Box?

    Ya, but sometimes people have to hear it twice (or more).
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dialog : How to update an Edit Box?

    Most likely the underlying problem is that you are performing the hardware work in the main UI thread. This is causing the UI to not process windows messages quickly enough.

    Pull your hardware code into a second thread, and post a message to the UI thread when data has arrived.

    See the articles in my signature line for a couple of these multithreading examples.

  13. #13
    Join Date
    Mar 2006
    Posts
    63

    Wink SOLVED Dialog : How to update an Edit Box?

    Thanks GCDEF and krmed, whereby krmed actually went to the trouble of posting how to UpdateWindow(). Actually, UpdateWindow() does nothing disecernible if entered verbatim - presumably it updates something to do with the main window. I had already tried that , before GCDEF suggested it. But without the detail provided by krmed, I wouldn't have got the result I need. Saved me clicking around with multiple threading and phase locked-loops, which are interesting in their own right, but I gotta get ma hardware workin sometime too.
    Thanks again!

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: SOLVED Dialog : How to update an Edit Box?

    Quote Originally Posted by crasher View Post
    Thanks GCDEF and krmed, whereby krmed actually went to the trouble of posting how to UpdateWindow(). Actually, UpdateWindow() does nothing disecernible if entered verbatim - presumably it updates something to do with the main window. I had already tried that , before GCDEF suggested it. But without the detail provided by krmed, I wouldn't have got the result I need.
    To be fair, GCDEF said:
    call UpdateWindow on your edit control
    I guess he thought that you can figure out how to get a handle to your edit control.


    Quote Originally Posted by crasher View Post
    Saved me clicking around with multiple threading and phase locked-loops, which are interesting in their own right, but I gotta get ma hardware workin sometime too.
    Although it looks like your immediate problem is solved with this UpdateWindow(), I predict that VERY soon you will find another problem with updating, for example, a progress bar or something. That one could also be solved by another strategically placed call to UpdateWindow(), but what would you do when you need to implement a “Cancel” (or “Stop”) button, or some other user control?

    There is only one correct way to implement time-consuming processing, as Arjay suggested, - on a separate (from main UI) thread.
    You can learn from other people experience, or you can make your own errors and learn that way (I hear you remember those things MUCH better).
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: SOLVED Dialog : How to update an Edit Box?

    Quote Originally Posted by VladimirF View Post
    There is only one correct way to implement time-consuming processing, as Arjay suggested, - on a separate (from main UI) thread.
    You can learn from other people experience, or you can make your own errors and learn that way (I hear you remember those things MUCH better).
    I still prefer message pumps. Easier to set up and less to coordinate.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured