如何从另一个子窗口访问子窗口控件 [英] How to access the child window control from another child window

查看:47
本文介绍了如何从另一个子窗口访问子窗口控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父窗口,上面有tabControl。然后我在另一个tabControl中显示了另外两个子窗口。在child1中,我有CEdit控件,因此在child2中也是如此。 child2中的CEdit是一个CEdit,它在child1中按下enter按钮时显示来自child1中CEdit的一些文本。问题是每当我点击child1中的输入按钮时,我都会收到错误。我在输入按钮中使用此代码:

I have parent window that has tabControl on it. Then i have made 2 other child window that shown in to that tabControl. In the child1, i have CEdit control and so also in the child2. CEdit in the child2 is a CEdit that show some text from CEdit in the child1 when press enter button in child1. the problem is that whenever i hit the enter button in child1 i get an error. I use this code in enter button :

void child_1::OnBnClickedEnter()
{
    CString CSText;
    m_EnterTextField.GetWindowText(CSText);
    m_View = (CEdit *) GetDlgItem(CE_Child2);//CE_Child2 is the ID from CEdit control in Child2
    m_View->SetWindowText(CSText);//the problem is in here
}



谢谢...:)


thanks... :)

推荐答案

GetDlgItem()函数检索指向窗口内指定控件或子窗口的指针,以便调用该函数。您正在使用此窗口中不存在的ID从 child_1 窗口调用此函数,该函数将返回NULL。要在另一个窗口中获取指向控件的指针,必须为该另一个窗口调用 GetDlgItem()

The GetDlgItem() function retrieves a pointer to the specified control or child window inside the window for that the function has been called. You are calling this function from the child_1 window with an ID that does not exist in this window and the function will return NULL. To get the pointer to a control in another window, you must call GetDlgItem() for that other window:
m_View = (CEdit *) child_2->GetDlgItem(CE_Child2);





[更新:详细解决方案]



[UPDATE: Detailed solution]

// child1.h
class child_2; // Forward declaration to avoid including child_2.h

class child_1
{
// ...
public:
    void SetChild2(child_2 *p) { m_child2 = p; }
protected:
    child_2 *m_child2;
}




// child1.cpp
#include <child_2.h>

child_1::child_1(void)
{
    m_child_2 = NULL;
}

void child_1::OnBnClickedEnter()
{
    // Check if SetChild2() has been called and the window is valid.
    ASSERT(m_child2 && ::IsWindow(m_child2->m_hWnd));
    m_View = (CEdit *) m_child2->GetDlgItem(CE_Child2);
}

在child_1和child_2的父对话框的 OnInitDialog()中调用 SetChild2() function。

From within OnInitDialog() of the parent dialog of child_1 and child_2 call the SetChild2() function.


在一个子窗口中,您只能访问属于该窗口的控件。

您得到的错误是因为你试图访问属于另一个孩子的控件。



完成你需要的一个好方法是从child1向父母发送一条消息,以便父级可以从child1检索编辑控件值,并使用子窗口中的方法将其设置为child2。
In one child window, you can only access controls that belong to that window.
The error you're getting is because you're trying to access a control that belongs to another child.

A good way to accomplish what you need would be to send a message from child1 to the parent, so that the parent can then retrieve the edit control value from child1 and set it to child2 using methods in the child windows.


这篇关于如何从另一个子窗口访问子窗口控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆