Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys...I am designing an MFC application...I have a head-ache in passing variable from one class to another..
I will explian my problem clearly...I have two PropertyPages attached to a PropertySheet

BOOL CExtractorFinalUIApp::InitInstance()
{
	
	CMyPropertySheet  propSheet(L"Self Extractor") ;//PropertyClass

	CMyPropertyPage1 page1;
	CMyPropertyPage2 page2;
	
		
	propSheet.AddPage(&page1);
	propSheet.AddPage(&page2);
	
	m_pMainWnd = &propSheet;
	propSheet.SetWizardMode();
	int nResponse = propSheet.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}
	return FALSE;
}

As u can see above I have set the CPropertySheet in Wizard mode..so if I click "NEXT "..it moves to the next page
that I have attached and now heres the deal...I have a variable "CString myapp" in CMyPropertyPage1 (which is the class for PropertyPage1)
and this variable holds some value which I wish to pass to the 2nd PropertyPage Class ( i.e) to CMyPropertyPage2(to one of its function)...
I have done this using static functions...but I cannot write functions for all the variables that I am tryin 2 pass...and off-course
thats not the healthy way to code I beleive...I heard some thing about the DDX..i think it will work only for control variables such as
edit control...checkbox and such...is ther a way that I can fix this with any one of ur assistance?
Posted

1 solution

You may:
  1. Add public get/set methods to the CMyPropertySheet (you may use a std::map to hold {name, value} pairs).
  2. On creation of the pages, pass the instance of the sheet to the pages themselves. This way each page may call the get/set methods of the sheet.
  3. For instance
    // header file of CMyPropertySheet
    #include <map>
    using namespace std;
    
    CMyPropertySheet:public CPropertySheet
    {
      //...
    private:
      map< CString, int > m_ItemMap;
      //...
    };

    // source file of CMyPropertySheet
    void CMyPropertySheet::setItem(CString key, int value)
    {
      m_ItemMap.insert( make_pair(key, value) );
    }
    bool CMyPropertySheet::getItem(CString key, int & value)
    {
      bool found = false;
      map< CString, int >::iterator it = m_ItemMap.find( key );
      if ( it != m_ItemMap.end() )
      {
        value = it->second;
        found = true;
      }
      return found;
    }

    // header file of CMyPropertyPage1
    CMyPropertyPage1 : public CPropertyPage
    {
    private:
      CMyPropertySheet * m_pSheet;
    public:
      CMyPropertyPage1(CMyPropertySheet * pSheet): m_pSheet(pSheet){}
    }

    and a similar modification to CMyPropertyPage2.

    Now you may do:
     CMyPropertySheet  propSheet(L"Self Extractor") ;//PropertyClass
    
     CMyPropertyPage1 page1(&propSheet);
     CMyPropertyPage2 page2(&propSheet);
    
     propSheet.AddPage(&page1);
     propSheet.AddPage(&page2);
    // ..
    


    And each property page may call, when needed setItem or getItem, for instance:
    CMyPropertyPage1::OnMyBtnDown()
    {
      m_pSheet->setItem("FirstVar", 100);
    }


    :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900