5

I'm working on MFC win32 project. I have dialog with 2 CMFCEditBrowseCtrl controls. After user specifies files on these controls, how to get file paths from these controls?

Update: here is my code

SpecifyInputDialog dlg; // this is my dialog inherited from CDialogEx
dlg.DoModal();
CString strText;
dlg.inFileCtrl.GetWindowTextA(strText.GetBuffer(), 500); // inFileCtrl is CMFCEditBrowseCtrl object

Results in "Debug Assertion Failed" error on last line...

Update 2:

CString strText;
dlg.inFileCtrl.GetWindowText(strText);

The same "Debug Assertion Failed" error. I will try to get text while dialog not dissmissed.

Update 3 (solved):

I managed to get path text by implementing callback

BEGIN_MESSAGE_MAP(SpecifyInputDialog, CDialogEx)
  ON_EN_CHANGE(IDC_MFCEDITBROWSE1, &SpecifyInputDialog::OnEnChangeMfceditbrowse1)
END_MESSAGE_MAP()  

And in handler method:

void SpecifyInputDialog::OnEnChangeMfceditbrowse1()
{
    this->inFileCtrl.GetWindowText(this->inFileString);
}

So your thought about getting text while dialog are not closed yet was right. Please update your answer thus I could mark it as solution.

4

3 に答える 3

4

CMFCEditBrowseCtrl is extended from CEdit and you can use GetWindowText / SetWindowText to access the currently displayed filename.

Update

Just do:

 CString strText;
 dlg.inFileCtrl.GetWindowText(strText);

The failed assertion could be due to any number of reasons (trace into it to see the cause). You may have to grab the text in the dialog code before the dialog closes.

于 2012-04-30T13:28:41.447 に答える
0

You cannot call dlg.(any control).GetWindowTextA AFTER DoModal - in this time the dialog window (as well as all child controls) is no longer exists. Please try to use MFC's DDX (bind required control to CString) or override OnOk method in your dialog - inside this method controls are accessible.

于 2012-05-07T14:10:15.780 に答える
0

You are getting an error because the window is closed after DoModal() returns, and GetWindowTextA is a generic function that gets the text from the window handle. Instead, you want to put this value in a string during the MFC's DDX exchange. Using the Class Wizard, select your SpecifyInputDialog class, then choose the Member Variables tab (the default is the Commands tab), and under that choose the control ID for the browse edit control, and choose Add Variable. Under Category, change Control to Value. This will change the variable type from CMFCEditBrowseCtrl to CString. Give your CString a name, (say inFileText), and the rest is automatic. You get to this string like this:

SpecifyInputDialog dlg; // this is my dialog inherited from CDialogEx
dlg.DoModal();
CString strText;
strText = dlg.inFileText; // after the data exchange, this has what you need

The Code Wizard-generated DDX looks like this:

void SpecifyInputDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_BROWSE, inFileText);
}
于 2017-05-30T18:42:44.580 に答える