MemoryLeak
MemoryLeak

Reputation: 7318

In MFC program, how to pass data between different dialog?

In web development, when we want to pass something between different pages, we might use the Session to save the data. But in MFC, what can we use to store these things?

Thanks!

Upvotes: 3

Views: 4682

Answers (2)

Kieveli
Kieveli

Reputation: 11075

Typical MFC Applications will have a Document-View-Frame architecture. Data is stored in the Document object, and accessed globally. You can access it anywhere via AfxGetMainWnd().

AfxGetApp() will also get you a pointer to your main application, which is another good spot to store data if you're not using a Document View architecture. If there is a lot of data, you can construct a class to hold the data, then add an instance as a member variable to the CWinApp in your project.

Another option, which I don't recommend but I have seen, is to have the dialogs themselves as member variables in the CWinApp, and then each dialog can reference the other. Basically, the user clicks 'ok', but then the dialog disappears, but is not deleted. This means all the data they entered is still accessable via the dialog variable.

Upvotes: 3

i_am_jorf
i_am_jorf

Reputation: 54600

There are a ton of ways to share data between dialogs. You may need to be more specific about your needs.

  1. Store it in a global variable.
  2. Store it in thread local storage (TLS).
  3. Have one Dialog send a window message via SendMessage() or PostMessage().

Things get more complicated from there.

Upvotes: 1

Related Questions