简体   繁体   English

在MFC中更新用户对话框

[英]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. 但是,我没有在CProjectDlg中使用直接方法。 I have a CMain class which will handle the operation. 我有一个CMain类,它将处理该操作。

Here is my code: 这是我的代码:

ProjectDlg.cpp ProjectDlg.cpp

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

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

Main.cpp 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. 在调试模式下,我发现m_edit1的地址不相同。 So the function is useless. 因此该功能是无用的。

I need to pass the same address of m_edit1 to the Click() function. 我需要将m_edit1的相同地址m_edit1Click()函数。 How do I do that? 我怎么做?

Thank you. 谢谢。

Each time you click, you create a new dialog. 每次单击时,都会创建一个新对话框。

CProjecttDlg *ptr = new CProjectDlg(); CProjecttDlg * ptr =新的CProjectDlg();

What you must do is to create it just once (maybe at CMain constructor? or the first time click is accessed). 您必须做的是仅创建一次(可能是在CMain构造函数中?还是第一次访问该点击)。 To store its value, just make ptr a member of CMain(define in .h, and so on) instead of a local variable. 要存储其值,只需使ptr成为CMain的成员(在.h中定义,依此类推),而不是局部变量即可。

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. 您正在调用CMain :: Click到一个CProjectDlg实例,但是在CMain :: Click中创建了一个新的CProjectDlg实例,并在该新对话框中设置了编辑框,而不是在原始对话框中。

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. 我不知道您到底想做什么,但是可行的一件事是将指向对话框的指针传递给CMain构造函数,然后在CMain :: Click中使用它来设置编辑框。 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. 除此之外,我不知道是否每次用户单击底部时都需要创建一个新的CMain实例。

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. 但是,如果没有更多关于您要做什么的详细信息,我将为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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