更新 MFC 中的用户对话框
我想在单击按钮时更新用户界面。但是,我没有在 CProjectDlg 中使用直接方式。我有一个 CMain 类来处理该操作。
这是我的代码:
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.");
}
在调试模式下,我发现 m_edit1
的地址不一样。所以这个功能没什么用。
我需要将 m_edit1
的相同地址传递给 Click()
函数。我该怎么做?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次单击,都会创建一个新对话框。
您必须做的就是仅创建一次(也许在 CMain 构造函数中?或者第一次访问 click 时)。要存储其值,只需将 ptr 设为 CMain 的成员(在 .h 等中定义)而不是局部变量即可。
Each time you click, you create a new dialog.
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.
你那里有问题。您正在从 CProjectDlg 实例调用 CMain::Click,但在 CMain::Click 内创建 CProjectDlg 的新实例,并在该新对话框中设置编辑框,而不是在原始对话框中。
我不知道你到底想做什么,但可行的一件事是将指向对话框的指针传递给 CMain 构造函数,然后在 CMain::Click 中使用它或设置编辑框。像这样的事情:
除此之外,我不知道是否有必要在每次用户单击底部时创建一个新的 CMain 实例。
最后,我提供的可能的解决方案可能有效,但也可能不“正确”。不过,如果没有更多关于您想要做什么的详细信息,我也无法为您提供更多帮助。
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:
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.