Link to home
Start Free TrialLog in
Avatar of FreddieM
FreddieM

asked on

Access CDialog members?

I've got a Dialog-based Application.
By default VC created an App and a Dlg class.
In the App's InitInstance, to show the dialog box, there is a declaration CMyDlg dlg, then a dlg.DoModal().

My question is, how do I access dlg members from my other classes?  For instance, I will have a status on the dialog box, and I want other classes to have access to that status to write to it.

Help much appreciated

Fred
Avatar of pkreddy
pkreddy

hi,
  Please be more specfic about the problem.

 You can access this way or
  ((CDerivedApp*)AppAfxGetApp())->dlg.m_variable;

  ((CMyDlg*)(CAfxGetMainWnd())->dlg.m_variable;

-- PK
Avatar of FreddieM

ASKER

Thanks for your help pkreddy.

I tried your first suggestion
((CDerivedApp*)AppAfxGetApp())->dlg.m_variable;

before posting my question here.

But dlg isn't a public member of the App.  It's declared in InitInstance.
When I tried declaring it as a public member I got VC exceptions on startup - it didn't seem to like me declaring a dialog as a public member - it only accepted it in InitInstance.

For CAfxGetMainWnd - that didn't compile -
"error C2065: 'CAfxGetMainWnd' : undeclared identifier"

Anything else I can try?

Many thanks

Fred
ASKER CERTIFIED SOLUTION
Avatar of pkreddy
pkreddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Meir Rivkin
FreddieM: its quite simple, in order to give other classes an access to its data members u need to have some function which retrieves the data member.

class CMainDialog :: public CDialog
{
public:
CStatusbar& GetStatusar(){return m_wndStatusbar;}

protected:
CStatusbar m_wndStatusbar;
};


cheers
now, u can write to it:
GetStatusar().SetWindowText("write text here....");
Thanks for your comment sedgwick.

But it was the AfxGetMainWnd part that I didn't know.
(I've not done that much MFC).
I figured that I if I could get to a public member variable as a quick test, then I could design it better with access functions as you suggested, later.

Thanks again, but pkreddy answered the bit I really didn't know.
Excellent (after fixing the 'C' typo!!!)

Thanks very much pkreddy.

Fred