Simple Way to Check All Dialog Controls for Changings

It is very difficult and toilsome to implement code that checks every single control on a given dialog for changes; especially for complex dialogs. Normally you have to check all fields when the dialog was closed (let's say in OnOK) or have to catch all events like EN_CHANGED or BN_CLICKED to notice a changing. But fortunately the MFC provides a method to do such checkings in a very easy and compact way. This magical function calls
ON_CONTROL_RANGE ( wNotifyCode, id1, id2, memberFxn )

Use this macro to map a contiguous range of control IDs to a single message handler function for a specified Windows notification message. ClassWizard does not support creating an ON_CONTROL_RANGE handler in the user interface, so you must place the macro yourself. Be sure to put it outside the message map //{{AFX_MSG_MAP delimiters. Once entered, ClassWizard will parse these entries and allow you to browse them just like any other message map entries.

The BN_KILLFOCUS notification code is sent when a button loses the keyboard focus. The button must have the BS_NOTIFY style to send this notification message.

Usage

  1. Add the message handlers declaration...
  2. BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
     //{{AFX_MSG_MAP(CMyDlg)
     ON_WM_CREATE()
     //}}AFX_MSG_MAP
    
     ON_CONTROL_RANGE(EN_CHANGE, IDC_EDIT1, IDC_EDIT3, OnRangeUpdateED)
     ON_CONTROL_RANGE(BN_CLICKED , IDC_RADIO1, IDC_RADIO8, OnRangeUpdateRB)
     ON_CONTROL_RANGE(BN_KILLFOCUS , IDC_CHECK1, IDC_CHECK6, OnRangeUpdateCB)
    END_MESSAGE_MAP()
    
  3. Declare the function in your header file that way:
  4. class CMyDlg : public CDialog
    {
    public:
     BOOL m_bUpdateED;
     BOOL m_bUpdateRB;
     BOOL m_bUpdateCB;
    	...
    
    protected:
     // Generated message map functions
     //{{AFX_MSG(CMyDlg)
     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
     virtual BOOL OnInitDialog();
     virtual void OnOK();
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
    
     virtual void OnRangeUpdateED(UINT nID);
     virtual void OnRangeUpdateRB(UINT nID);
     virtual void OnRangeUpdateCB(UINT nID);
    };
    
  5. And finally code your 'OnRangeUpdate...' functions that way:
  6. void CMyDlg::OnRangeUpdateED(UINT nID)
    {
     // extra check
     if ( SendDlgItemMessage(nID, EM_GETMODIFY, 0, 0) )
      m_bUpdateED = TRUE;
     else
      m_bUpdateED = FALSE;
    }
    
    void CMyDlg::OnRangeUpdateRB(UINT nID)
    {
     // for example
     if ( nID == IDC_RADIO3 )
      m_bUpdateRB = TRUE;
     else
      m_bUpdateRB = FALSE;
    }
    
    void CMyDlg::OnRangeUpdateCB(UINT nID)
    {
     // for example
     LRESULT lResult = SendDlgItemMessage(nID, BM_GETSTATE, 0, 0);
     if ( lResult == BST_CHECKED )
      m_bUpdateCB = TRUE;
     else
      m_bUpdateCB = FALSE;
    }
    
    void CMyDlg::OnOK() 
    {
     if ( m_bUpdateRB )
     // ...
    
     CDialog::OnOK();
    }
    


Comments

  • Why are men with rheumatism more likely to need erectile dysfunction?

    Posted by afueffifielm on 06/21/2013 06:58pm

    Erectile Dysfunction Drug Evaluation - Which remedies Are the specific Very best? In this article, we'll cover just what Viagra is, how it works and what the potential dangers are [url=http://rxhealth.pw ] compare cost of viagra [/url] read much more How to Cure Erectile Dysfunction Naturally Without Drugs

    Reply
  • Those things everybody else does with nike and those actions you want to do totally different.

    Posted by icoppyapedcap on 04/21/2013 08:20pm

    Dw [url=http://hunter-rain-boots.webnode.jp]ハンターブーツ[/url] nMy [url=http://hunterrainbootsjp.webnode.jp]ハンターブーツ[/url] f WpoKjf YiaV [url=http://hunter-boots8.webnode.jp]レインブーツメンズ[/url] rn A [url=http://rain-boots-men.webnode.jp]ブーツ[/url] oc [url=http://hunter-rain-boots-ja.webnode.jp]レインブーツメンズ[/url] CnsCaiDpq O[url=http://rainshoesja.webnode.jp]レインブーツ[/url] rwDvgCshLv [url=http://ja-hunter-rain-boots.webnode.jp]レインブーツハンター[/url] s HetPxt [url=http://rain-boots-popular.webnode.jp]レインブーツ人気[/url] Zbv [url=http://rain-boots-men6.webnode.jp]レインブーツハンター[/url] Dan Jhn [url=http://jahunterrainboots.webnode.jp]ハンターブーツ[/url] Lug

    Reply
  • Perfect

    Posted by Legacy on 05/22/2003 12:00am

    Originally posted by: Ben Dunaway

    I am still learning MFC and finding solutions to common problems can get lengthy. Thanks for the for you effort, the CONTROL_RANGE macro is perfect.

    Reply
  • question?

    Posted by Legacy on 08/07/2002 12:00am

    Originally posted by: PERAS

    can i use this with edit boxes! if i can how!
    i tried it and it didn't work

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • Remember getting your first box of LEGOS as a kid? How fun it was putting the pieces together, collaborating with your friends to create something new? Now, as an IT professional, assembling and maintaining a Lego-like collaboration infrastructure isn't what you signed up for. Piecing together disparate systems of record for email, web meetings and other applications is about as painful as stepping on a pile of Legos. Download the e-book to learn how implementing a collaboration system connects systems of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date