简体   繁体   中英

Update user dialog in MFC

I want to update a user interface when I clicked a button. However, I'm not using a direct way inside CProjectDlg. I have a CMain class which will handle the operation.

Here is my code:

ProjectDlg.cpp

void CProjectDlg::OnBnClickedButton1()
{
    CMain *ptr = new CMain();

    ptr->Click();
    CString test = m_edit1;
}

Main.cpp

void CMain::Click()
{
    CProjecttDlg *ptr = new CProjectDlg();

    ptr->m_edit1.SetString(L"This is a test.");
}

In the debug mode, I found the address of m_edit1 is not same. So the function is useless.

I need to pass the same address of m_edit1 to the Click() function. How do I do that?

Thank you.

Each time you click, you create a new dialog.

CProjecttDlg *ptr = new CProjectDlg();

What you must do is to create it just once (maybe at CMain constructor? or the first time click is accessed). To store its value, just make ptr a member of CMain(define in .h, and so on) instead of a local variable.

You have a problem there. You are calling CMain::Click fron a CProjectDlg instance, but create a new instance of CProjectDlg inside CMain::Click, and set the edit box in that new dialog, not in the original one.

I don't know exactly what you are trying to do, but one thing that could work is to pass a pointer to the dialog to the CMain constructor, and then in CMain::Click use it ot set the edit box. Something like this:

//CMain.h
class CMain
{
public:
    CMain(CProjectDlg*);

    Click();
protected:
    CProjecDlg* m_Dlg;
}

// CMain.cpp
CMain::CMain(CProjectDlg* dlg)
{
    m_Dlg = dlg;
}

CMain::Click()
{
    m_Dlg->m_edit1.SetString(L"This is a test.");
}

Apart from that, I don't know if it would be necessary to create a new instance of CMain every time the user clicks the bottom.

And finally, the possible solution I've provided might work, but it might also not be "correct". Without more details as to what you are trying to do, there's not much more I can help you with, though.

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