When I understand your question correctly, your two dialogs do not know of each other, but are both pages of tab control. So, your question would actually be: How can I get a pointer to Dlg1 while being in a member function of Dlg2?
There are several ways to do that, some more elegant than others:
1. You can deposite a pointer to Dlg1 in a global pointer variable and access that from Dlg2. For example:
Dlg1* ptrToDlg1 = 0;
Dlg1::Dlg1 (...)
{
ptrToDlg1 = this;
}
#include "Dlg1.h"
extern Dlg1* ptrToDlg1;
...
Dlg2::OnOk ()
{
CString fileName = ptrToDlg1->GetFileName();
...
}
Normally we try to avoid global pointers whenever possible. So here's a more elegant way:
b) At some place you create your two dialogs; probably in some code of your tab control. That is the write place to tell Dlg2 about where Dlg1 is:
...
Dlg1* ptrDlg1 = new Dlg1 (...);
Dlg2* ptrDlg2 = new Dlg2 (...);
ptrDlg2->SetPtrToDlg1 (ptrDlg1);
Now Dlg2 has a pointer to Dlg1 and can ask it at the right moment for the filename. ...
This is pretty much a long and detailed version of what Maximilien posted in Solution 1. I just thought, this might be the missing link and help you on your way.
[EDIT after OP amended his source code]
Here is a very detailed description of the changes you need to make.
In your Dlg2 class, add a function Dlg2::SetDlg1ptr (Dlg1* pDlg1) with code:
void Dlg2::SetDlg1Ptr (Dlg1* pDlg1)
{
m_ptrToDlg1 = pDlg1;
}
and add in the header file of Dlg2:
class Dlg1;
class Dlg2
{
public:
...
void SetDlg1Ptr (Dlg1* pDlg1);
protected:
Dlg1* m_ptrToDlg1;
...
};
You call this function in the constructor of MyTabCtrl:
m_Dialog[0] = new MyDlg1();
m_Dialog[1] = new MyDlg2();
m_Dialog[1]->SetDlg1Ptr (m_Dialog[0]);
From this moment on Dlg2 can call member functions of Dlg1 via this pointer.
Next, change your OnBnClickedButton6 function in Dlg1:
void MyDlg1::OnBnClickedButton6() {
if (FileDlg.DoModal() == IDOK)
{
m_pathName = FileDlg.GetPathName();
m_Edit4.SetWindowText (m_pathName);
}
}
and add the member variable m_pathName in Dlg2.h.
class Dlg1
{
public:
PCTSTR GetPathName () const;
...
protected:
CString m_pathName;
...
};
Then add an accessor function to m_pathName
PCTSTR Dlg1::GetPathName () const
{
return m_pathName;
}
And finally, you can access the path name in OnBnClickedButton2:
ShellExecute (NULL, .... , m_ptrDlg1->GetPathName(), ...);