Click here to Skip to main content
Click here to Skip to main content

Save and restore dialog controls' states

By , 28 Feb 2004
Rate this:
Please Sign up or sign in to vote.

Sample image

Introduction

In some (or most?) cases, when we are using dialog windows to communicate with the user, we need to save/restore state of checkboxes, edit controls, radio buttons, etc. There are several possibilities of how to do it. You can program functions newly for every dialog window or use "universal" solution. I created for myself one simple class that I am using in most cases with no changes. All that you will need to implement this function into your application is to add five lines of code to your dialog source file.

Using the code

First of all, if you want to use CDialogSettings class in your application, you must include these header files:

#include "stdafx.h"
#include "resource.h"
#include "dlgset.h"    <= CDialogSettings class

To restore dialog controls' states (text of edit controls, checked/unchecked state of checkboxes, etc.) we will need to add the following two lines of code to the dialog initialization routine. If you are creating a non-MFC application, insert CDialogSettings class here:

INT_PTR CALLBACK dialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg) {
    case WM_INITDIALOG:
      {
        CDialogSettings ds(hwndDlg);
        ds.Load("Software\\CodeProject\\DialogAppExample");
      }
      break;
      ...

If you are creating an MFC application, insert CDialogSettings class here:

BOOL CDlgSetExampleDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  
  CDialogSettings ds(m_hWnd);
  ds.Load("Software\\CodeProject\\DialogAppExample");   

  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

Code that will store dialog controls' state (CDialogSettings class uses registry) you can add into the IDOK handler (you need to save settings only if the user confirms dialog), or into the WM_DESTROY / OnDestroy routine. In the non-MFC application, it will look like this:

case WM_DESTROY:
      {
        CDialogSettings ds(hwndDlg);
        ds.Save("Software\\CodeProject\\DialogAppExample");
      }
      break;

and in the MFC application:

void CDlgSetExampleDlg::OnDestroy() 
{
  CDialogSettings ds(m_hWnd);
  ds.Save("Software\\CodeProject\\DialogAppExample");

  CDialog::OnDestroy();  
}

That's all. I hope, you find this class useful.

This demo project (and also article) was created using umdev - multi-compiler IDE.

History

  • 24. February 2004 - First release.
  • 1. March 2004 - Updated project.

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

About the Author

uemaker
Web Developer
Slovakia Slovakia
No Biography provided

Comments and Discussions

 
QuestionCasting error Pinmemberinfinity25729-Mar-12 4:16 
QuestionCompiler error [modified] PinmemberWild Thing26-Feb-07 6:54 
AnswerRe: Compiler error PinmemberWild Thing26-Feb-07 7:12 
GeneralGreat Code! Pinmembermultivitaminmehrfruchtsaft21-Sep-06 23:52 
GeneralPerfect! Pinmemberfengyunxiao14-Jan-05 17:18 
GeneralNice code PinmemberSumit Kapoor25-Oct-04 22:00 
GeneralProject doesn't work Pinmemberritika.pande@wipro.com29-Feb-04 18:28 
GeneralRe: Project doesn't work PinsussAnonymous29-Feb-04 19:23 
GeneralRe: Project doesn't work Pinmemberianw.moore2-May-04 21:31 
GeneralRe: Project doesn't work PinsussAnonymous2-May-04 22:32 
GeneralMFC: small correction Pinmemberallelimo28-Feb-04 2:56 
GeneralRe: MFC: small correction Pinmemberuemaker28-Feb-04 3:24 
GeneralComments on the code Pinmemberkhb25-Feb-04 20:45 
GeneralRe: Comments on the code Pinmemberuemaker25-Feb-04 20:55 
GeneralRe: Comments on the code Pinmemberuemaker25-Feb-04 22:45 

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.140415.2 | Last Updated 29 Feb 2004
Article Copyright 2004 by uemaker
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid