Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have 9 browse buttons on dlg1 (9 files) which i have to pass to dlg2 click button event.

C++
// MyTabControl.cpp

MyTabCtrl::MyTabCtrl()
{
	m_DialogID[0] =IDD_DIALOG1;
	m_DialogID[1] =IDD_DIALOG2;

	m_Dialog[0] = new MyDlg1();
	m_Dialog[1] = new MyDlg2();

    m_nPageCount = 2;

}

MyTabCtrl::~MyTabCtrl()
{
}

void MyTabCtrl::InitDialogs()
{
	m_Dialog[0]->Create(m_DialogID[0],GetParent());
	m_Dialog[1]->Create(m_DialogID[1],GetParent());

}


C++
// MyDlg.cpp

MyDlg1::MyDlg1(CWnd* pParent /*=NULL*/)
	: CDialog(MyDlg1::IDD, pParent)
{
	//{{AFX_DATA_INIT(MyDlg1)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	//  m_NumberOfEvents = 0;
	m_SetUp = 0;
	m_EdpCode = _T("");
}


void MyDlg1::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_CBString(pDX, IDC_EDP_CODE, m_EdpCode);
	DDV_MaxChars(pDX, m_EdpCode, 40);
	DDX_Control(pDX, IDC_LIST1, m_Events);
	DDX_Control(pDX, IDC_RADIO1, m_Radio1);

        DDX_Control(pDX, IDC_RUN, m_Combo1);
	DDX_Control(pDX, IDC_EDIT2, m_Edit2);
	DDX_Control(pDX, IDC_EDIT1, m_Edit1);

	DDX_Control(pDX, IDC_BROWSE7, m_Browse1);

}
	
BEGIN_MESSAGE_MAP(MyDlg1, CDialog)
	//{{AFX_MSG_MAP(MyDlg1)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &MyDlg1::OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO2, &MyDlg1::OnCbnSelchangeCombo2)
	ON_CBN_SELCHANGE(IDC_COMBO1, &MyDlg1::OnCbnSelchangeCombo1)
	
	ON_BN_CLICKED(IDC_RADIO9, &MyDlg1::OnBnClickedRadio9)

	ON_BN_CLICKED(IDC_BUTTON1, &MyDlg1::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON10, &MyDlg1::OnBnClickedButton10)
END_MESSAGE_MAP()


// MyDlg1 message handlers

void MyDlg1::OnOK() 
{
	// TODO: Add extra validation here
	MessageBox("Tab 1");
	
}

void MyDlg1::OnBnClickedButton6() // browse for file 
{
	// TODO: Add your control notification handler code here

    char strFilter[] = { "BCR Files (*.bcr)|*.bcr|All Files (*.*)|*.*||" };

    CFileDialog FileDlg(TRUE, ".bcr", NULL, 0, strFilter);
     
	if(FileDlg.DoModal() == IDOK)
    {
		 CString  m_PathName1 = FileDlg.GetPathName();
		 m_Edit4.SetWindowText( m_PathName1);
		
	}
}


C++
// MyDlg2.cpp

MyDlg2::MyDlg2(CWnd* pParent /*=NULL*/)
	: CDialog(MyDlg2::IDD, pParent)
{
	//{{AFX_DATA_INIT(MyDlg2)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void MyDlg2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(MyDlg2)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP

	DDX_Control(pDX, IDC_LIST2, m_List2);
	DDX_Control(pDX, IDC_BUTTON3, m_SaveDlg1);
	//  DDX_Control(pDX, IDC_BUTTON1, m_Calculate1);
}


BEGIN_MESSAGE_MAP(MyDlg2, CDialog)
	//{{AFX_MSG_MAP(MyDlg2)
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_CHOPPING, &MyDlg2::OnCbnSelchangeChopping)
	ON_WM_DRAWITEM()


	ON_BN_CLICKED(IDC_BUTTON3, &MyDlg2::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &MyDlg2::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &MyDlg2::OnBnClickedButton5)
	ON_NOTIFY(LVN_KEYDOWN, IDC_LIST2, &MyDlg2::OnKeydown)
	ON_BN_CLICKED(IDC_BUTTON2, &MyDlg2::OnBnClickedButton2)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST2, &MyDlg2::OnItemchangedList2)
	ON_BN_CLICKED(IDC_BUTTON1, &MyDlg2::OnBnClickedButton1)
END_MESSAGE_MAP()


// MyDlg2 message handlers

void MyDlg2::OnOK() 
{
	// TODO: Add extra validation here
	MessageBox("Tab 2");
	
}

void MyDlg2::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
   
ShellExecute(NULL, ("open"), ("C:\\ABC.exe"),here I have to Pass path from dlg1 ,NULL,SW_SHOWNORMAL);


}
Posted
Updated 18-Mar-13 11:51am
v5

What is the relation between the 2 dialogs ?

Do you create Dlg2 from Dlg1 ?

for example, one way of doing it (if Dlg2 is created from within Dlg1)

//...
void CDlg2::SetFileName( const CString& filename )
{
  m_FileName = filename;
}

//...
CString CDlg1::GetFileName( )
{
  return m_FileName;
}

//...
void CDlg1::CreateDlg2 ()
{
   CDlg2 dlg2;
   dlg2.SetFileName( GetFileName() );
   dlg2.DoModal();
}

//...
 
Share this answer
 
Comments
peoria123 14-Mar-13 14:57pm    
Thanks for the reply ..dlg2 is not created from dlg1..those are tabs..tab1 and tab2 in tab1 I have used FileOpenDialog to access the filename. I want to give selected filename to ShellExecute() which is on second tab..(buttonclickedevent)
Provide a GetFilename() accessor in the 'from' dialog and call it from the 'to' dialog:

C++
// Get Filename
CString CDlg1::GetFileName( )
{
    return m_FileName;
}


... or if you want to do it the other way, add a SetFilename() accessor in the 'to' dialog and call it from the 'from' dialog. In the accessor, do the ShellExecute() call. This also means that you will have to get the 2 dialogs to be able to identify themselves to each other (well actually in one direction only, the direction depending on which solution you choose)...
 
Share this answer
 
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:
C++
// In Dlg1.cpp:
Dlg1* ptrToDlg1 = 0;

Dlg1::Dlg1 (...)
{
   ptrToDlg1 = this;
}

//in Dlg2.cpp
#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:
C++
...
Dlg1* ptrDlg1 = new Dlg1 (...);
Dlg2* ptrDlg2 = new Dlg2 (...);

// tell Dlg2 about Dlg1
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:
C++
void Dlg2::SetDlg1Ptr (Dlg1* pDlg1)
{
    m_ptrToDlg1 = pDlg1;
}

and add in the header file of Dlg2:
C++
// in Dlg2.h

class Dlg1; // advance declaration

class Dlg2
{
public:
    ...
    void SetDlg1Ptr (Dlg1* pDlg1);

protected:
    Dlg1* m_ptrToDlg1;
    ...
};

You call this function in the constructor of MyTabCtrl:
C++
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:
C++
void MyDlg1::OnBnClickedButton6() // browse for file 
{
    if (FileDlg.DoModal() == IDOK)
    {
	m_pathName = FileDlg.GetPathName();
            // DO NOT DECLARE m_pathName as a local string!
	m_Edit4.SetWindowText (m_pathName);
    }
}

and add the member variable m_pathName in Dlg2.h.
C++
class Dlg1
{
public:
    PCTSTR GetPathName () const;
    ...
protected:
    CString m_pathName;
    ...
};

Then add an accessor function to m_pathName
C++
PCTSTR Dlg1::GetPathName () const
{
    return m_pathName;
}

And finally, you can access the path name in OnBnClickedButton2:
C++
ShellExecute (NULL, ....  , m_ptrDlg1->GetPathName(), ...);
 
Share this answer
 
v2
Comments
peoria123 15-Mar-13 11:35am    
void MyDlg1::OnBnClickedButton6()
{
// TODO: Add your control notification handler code here

char strFilter[] = { "BCR Files (*.bcr)|*.bcr|All Files (*.*)|*.*||" };

CFileDialog FileDlg(TRUE, ".bcr", NULL, 0, strFilter);

if(FileDlg.DoModal() == IDOK)
{
CString PathName1 = FileDlg.GetPathName();
m_Edit4.SetWindowText(PathName1);
}

}


CString MyDlg1::GetPathName()
{
return PathName1; //error undeclared variable

}
Thank you for the reply .how I can store PathName1 value to pass to dlg2 ?
nv3 15-Mar-13 11:55am    
Instead of PathName1 declare a member variable in your MyDlg1 with name m_pathName and assign to that. The value will be kept in their as long as the MyDlg1 object lives.
peoria123 15-Mar-13 15:46pm    
// Dialog1
void MyDlg1::OnBnClickedButton6()
{
// TODO: Add your control notification handler code here

char strFilter[] = { "BCR Files (*.bcr)|*.bcr|All Files (*.*)|*.*||" };

CFileDialog FileDlg(TRUE, ".bcr", NULL, 0, strFilter);

if(FileDlg.DoModal() == IDOK)
{
CString PathName1 = FileDlg.GetPathName();
m_Edit4.SetWindowText( PathName1 );

}
}
void MyDlg1::SetPathName(CString PathName1 )
{
m_PathName1=PathName1; // assign member variable to store pathname1
}

CString MyDlg1::GetPathName()
{
return m_PathName1;
}

// Dialog2
void MyDlg2::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
MyDlg1 Dlg1;
ShellExecute(NULL, ("open"), ("C:\\ABC.exe"),Dlg1.m_PathName1,NULL,SW_SHOWNORMAL);
}

dlg1.m_PathName1 value is not getting to shellexecute()
nv3 15-Mar-13 16:26pm    
1) In MyDlg1::OnBnClickedButton6 you should have called SetSpathName or simply set m_PathName1 directly. You can discard the PathName1 variable.

2) In MyDlg2::OnBnClickedButton you must not create a temporary Dlg1! That of course will not have the m_pathName1 variable set. Use a pointer to your original Dlg1 instead, as I have shown you in my solution.
peoria123 18-Mar-13 12:40pm    
Thanks for your solutions.. but no luck with assigning ponter to dlg1.I am getting error for 1st solution : ambiguous call to overloaded function Dlg1::Dlg1 (...)
{
ptrToDlg1 = this;
}
and for second solution
ptrDlg2->SetPtrToDlg1 (ptrDlg1); SetPtrToDlg1 is not member of Dlg1.

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