简体   繁体   中英

MFC Dialog return vector

I am creating a modal dialog box and dynamically creating textboxes based on a user specified quantity. I then store the values of these textboxes in a vector

vector <CString*> textBoxText;

I want to pass the vector back when the dialog closes. I tried passing a pointer into the dialog and updating the pointer in: void CRadioDialog::OnBnClickedOk(). However, that did not work. I don't think I can do this with data exchange, is there a way for it to be done?

Thanks,

You can do it. Just ensure you return actual CString objects, not pointers!

vector <CString> textBoxText; 

What is in OnBnClickedOk ?

assuming you have a local member CRadioDialog.h:

std::vector <CString> textBoxText;

I suggest you to use CString heare instead of CString*

you can add a method to your CRadioDialog.h:

void fill_my_vector( std::vector<CString>& out_vector );

and CRadioDialog.cpp:

void CRadioDialog::fill_my_vector( std::vector<CString>& out_vector )
{
    std::copy ( textBoxText.begin(), textBoxText.end(), out_vector.begin() );
}

you already fill you local textBoxText with your cstrings on CRadioDialog::OnBnClickedOk()

calling code: void main_window::caller() { ...

    std::vector <CString> results;

    CRadioDialog dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        dlg.fill_my_vector( results );
        /* USE YOUR VECTOR */
    }

...
}

It's not the best optiomization method but it easy to undestand. Hope it helps.

Thanks for all the help, I ended up using this method which was very easy and recommended from a different forum:

I added this into the dialog.h file:

public:
    const std::vector<CString>& TextBoxTexts() const
    {
        return textBoxText;
    }

and called it in my main view:

CRadioDialog dialog; 
if(rDLG.DoModal() == IDOK)
{   
    vector<CString> text;
    text = dialog.TextBoxTexts();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM