Click here to Skip to main content
Licence 
First Posted 23 Feb 2004
Views 55,421
Bookmarked 20 times

Save and restore dialog controls' states

By | 28 Feb 2004 | Article
A simple but universal way to save and restore state of dialog controls

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCasting error Pinmemberinfinity2574:16 29 Mar '12  
QuestionCompiler error [modified] PinmemberWild Thing6:54 26 Feb '07  
AnswerRe: Compiler error PinmemberWild Thing7:12 26 Feb '07  
GeneralGreat Code! Pinmembermultivitaminmehrfruchtsaft23:52 21 Sep '06  
GeneralPerfect! Pinmemberfengyunxiao17:18 14 Jan '05  
GeneralNice code PinmemberSumit Kapoor22:00 25 Oct '04  
GeneralProject doesn't work Pinmemberritika.pande@wipro.com18:28 29 Feb '04  
GeneralRe: Project doesn't work PinsussAnonymous19:23 29 Feb '04  
GeneralRe: Project doesn't work Pinmemberianw.moore21:31 2 May '04  
GeneralRe: Project doesn't work PinsussAnonymous22:32 2 May '04  
GeneralMFC: small correction Pinmemberallelimo2:56 28 Feb '04  
GeneralRe: MFC: small correction Pinmemberuemaker3:24 28 Feb '04  
GeneralComments on the code Pinmemberkhb20:45 25 Feb '04  
GeneralRe: Comments on the code Pinmemberuemaker20:55 25 Feb '04  
GeneralRe: Comments on the code Pinmemberuemaker22:45 25 Feb '04  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 29 Feb 2004
Article Copyright 2004 by uemaker
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid