Click here to Skip to main content
Click here to Skip to main content
Go to top

Updating modal dialog contents using callbacks

, 7 Dec 1999
Rate this:
Please Sign up or sign in to vote.
How to update a modal dialog contents using a callback function
  • Download demo project - 16 Kb
  • <!-- Article Starts -->

    This article shows how to update the contents of the modal dialog using a callback functions.

    Some background first. When developing communication applications, it is usually necessary to display a modal dialog box that contains some information about what is happening in the background (statistics, state of the communication etc). Most common solution is to pass all the knowledge about the background process(es) to the modal dialog and then use a timer to periodically update the contents of the dialog controls. This solution is Ok but complicates the design of the dialog and makes it very difficult to have a generic dialog that displays different kind of information depending on the place where it is created. Important side-effect of this solution is either the need to provide access to all data members of the background process classes to the dialog or the need to declare all data members public. Both options complicate the source code.

    The solution presented here uses a callback function given to the dialog via its constructor. Callback function is a member function that belongs to a CWnd derived class (exactly the same technique may be used with a callback function belonging to the CObject derived class). The responsibility of the dialog is to create a timer and periodically (from within OnTimer() handler) call the supplied callback function. Dialog may give some information to the callback function. It will collect dta from the callback function and use it to update the contents of its controls. Contrary to the common solution presented above, only the callback function needs to have access to background process and since it is a callback function it may as well be a part of the background process.

    This design reduces the complexity of the dialog and enables a generic dialog to display different kind of information using different types of callback functions. Another important side-effect is that the application design is simpler and more robust.

    First, we need to create a callback function type:

    typedef BOOL (CWnd::*TWindowUpdate)(int& nCount, CString& sText, DWORD dwData);
    

    Arguments of this function are application specific. Then we modify the constructor of the dialog:

    // pWindow is a pointer to the CWnd derived object that implements the callback function
    TTestDialog(TWindowUpdate pUpdateCallback, CWnd *pWindow,
                DWORD dwData = 0, CWnd* pParent = NULL)  // standard constructor
    

    At last, we call the callback function (most often from the OnTimer() handler).

    if (m_pWindow && m_pWindowUpdateCallback) 
    {
      int nCount = 0;
      CString str;
      if ((m_pWindow->*m_pWindowUpdateCallback)(nCount, str, m_dwData)) 
      {
         CString strTemp;
         strTemp.Format("%d", nCount);
         m_ItemCount.SetWindowText(strTemp);
         m_LastItem.SetWindowText(str);
      }
    }
    

    Callback function is implemented in a CWnd derived window (main frame, view etc).

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    Share

    About the Author

    Zoran M. Todorovic
    Web Developer SCA d.o.o.
    Serbia Serbia
    I am a cofounder of SCA Software, company that specializes in software for process control, visualization and communication. Programming for the last 10 years in C++, Delphi. Visual C++ for the last 6 years. Degree in Electronics Engineering and Telecommunications.

    Comments and Discussions

     
    GeneralGreat tutorial PinmemberAnthony_Yio20-Nov-03 16:26 
    GeneralIs there a way to find all the modal dialog boxes open within an application PinmemberHawkeye10-Sep-02 10:02 
    GeneralRe: Is there a way to find all the modal dialog boxes open within an application PinmemberGary R. Wheeler21-Nov-02 13:50 
    GeneralBug PinmemberSancy26-Aug-02 20:08 
    GeneralRe: Bug PinmemberAnthony_Yio20-Nov-03 16:23 
    GeneralNice job PinsussThird21-Aug-02 5:54 
    GeneralWell Done Pinmemberhamid24-Sep-01 20:00 
    General[Q] Pinmemberlinugee24-Jul-01 22:34 
    GeneralCorrection PinsussZoran M.Todorovic23-Jan-00 11:30 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web02 | 2.8.140916.1 | Last Updated 8 Dec 1999
    Article Copyright 1999 by Zoran M. Todorovic
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid