Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC VC++
I Have 9 browse buttons on dlg1 (9 files) which i have to pass to dlg2 click button event.
 
// 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());
 
}
 
// 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);
		
	}
}
 
// 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 14-Mar-13 9:21am
Edited 18-Mar-13 12:51pm
nv330.2K
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
}
 
//...
  Permalink  
Comments
peoria123 at 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)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Provide a GetFilename() accessor in the 'from' dialog and call it from the 'to' dialog:
 
// 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)...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
// 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:
   ...
   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:
void Dlg2::SetDlg1Ptr (Dlg1* pDlg1)
{
    m_ptrToDlg1 = pDlg1;
}
and add in the header file of Dlg2:
// 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:
     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() // 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.
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(), ...);
  Permalink  
v2
Comments
peoria123 at 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 at 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 at 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 at 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 at 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.
nv3 at 18-Mar-13 13:55pm
   
You haven't enter the three dots verbally in the parentheses, have you? These dots were meant as: Whatever arguments you have already defined in your constructor of Dlg1.
 
To see, what's really going wrong here, I'd need to see the full source of your Dlg1 and Dlg2 or at least the relevant parts of it. If you want to post that, use the gree Improve Question button below your original question, and mark the new by a separator line like "[edited: second attempt]".
 
nv3 at 18-Mar-13 18:34pm
   
I have seen the material you provided with Improve Question and added some <pre> commands to make look a little better.
 
Please see my solution, where I added a detailed description of what you need to do in your source. Hope that gets you on your way.
peoria123 at 19-Mar-13 16:48pm
   
Thank you very much for helping me out.
It is working now.
nv3 at 19-Mar-13 17:00pm
   
Anytime, I am glad I could help.

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

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100