Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 :
C#
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... :)
Posted
Updated 1-Sep-13 20:54pm
v2
Comments
Richard MacCutchan 2-Sep-13 3:11am    
What is the error?
Hyosoka Poipo 2-Sep-13 5:10am    
the error is ASSERT that something like this:

ASSERT(::IsWindow(m_hWnd));//program stop in this line
Hyosoka Poipo 2-Sep-13 5:13am    
i think that the child2 is not created when i wanna set a text in it's CEdit...
but until now i still do not know how to solve this... :-(
Nelek 2-Sep-13 7:03am    
You are working with MFC. Are you following the Doc/View structure?
Hyosoka Poipo 2-Sep-13 8:00am    
owh... Doc/View Structure....?
i don't understand what you mean Nelek....but i have found the solution that provided by Jochen Arndt....thanks so much... :-)

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]
C++
// 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;
}

C++
// 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);
}
From within OnInitDialog() of the parent dialog of child_1 and child_2 call the SetChild2() function.
 
Share this answer
 
v2
Comments
Hyosoka Poipo 2-Sep-13 5:01am    
Thanks for your answer Jochen Arndt...i have used your suggestion, but when i press enter button,,,there are an error said that "Unhandled exception at 0x50abeb2c(mfc100.dll)......"
then when i hit the break button, VS took me to this :

CWnd* CWnd::GetDlgItem(int nID) const
{
ASSERT(::IsWindow(m_hWnd));//program stop in this line

if (m_pCtrlCont == NULL)
return CWnd::FromHandle(::GetDlgItem(m_hWnd, nID));
else
return m_pCtrlCont->GetDlgItem(nID);
}
Jochen Arndt 2-Sep-13 5:11am    
You must off course pass a valid window pointer. It seems that child_2 is invalid here. How did you pass the child_2 pointer to child_1?

If your button handler may be called when child_2 does not exist, you must check this in your code or better use a method like explained by Superman in solution 2.
Hyosoka Poipo 2-Sep-13 5:36am    
i made an object of child2 in child1 in the header file
{in child1 header file}
#include "Child_2.h"
..
..
Child2 *chld2;
..
..

then in the cpp file:
void child_1::OnBnClickedEnter()
{
CString CSText;
m_EnterTextField.GetWindowText(CSText);
m_View = (CEdit *) chld2->GetDlgItem(CE_Child2);
m_View->SetWindowText(CSText);
}

did I make an invalid step Jochen ?
thanks... :-)
Jochen Arndt 2-Sep-13 6:13am    
Not an invalid step. But you missed the most important one:
Passing the pointer of your existing child_2 window to the member variable of child_1.

I will update by solution.
Hyosoka Poipo 2-Sep-13 6:22am    
owh..thanks Jochen Arndt... :-)
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.
 
Share this answer
 
Comments
Hyosoka Poipo 2-Sep-13 5:03am    
thanks _Superman_,,,em...btw how to send the message from child1 to parent?
i'm not an expert in MFC hehe... :-)
«_Superman_» 2-Sep-13 5:13am    
Use GetParent()->SendMessage(WM_XXX);
You would need to define your own custom message like this - #define WM_MYMESSAGE (WM_USER + 1)
Hyosoka Poipo 2-Sep-13 5:38am    
waawww... i really don't understand this code, :-(
Hyosoka Poipo 2-Sep-13 5:41am    
emmm....may be i have to read win32 programming instead of MFC...but thanks a lot _superman_ :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900