Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone,

Sorry for the repost of a question yesterday, I dint find the 'reply' link in my page, so only i went for that option.

Kindly glarify my doubt.I have a dialog class(CSampleDlg), in that I drag and drop, a ListControl(m_gridOutput) and a edit control. And I've added one class(CDerivedOne) derived from CSampleDlg.

In listcontrol, i have CheckBox items. Now i am trying to Check/Uncheck the Listcontrol item from Derived class through function call, like this.
m_gridOutput.SetCheck(Id,TRUE);

But i am getting assertion error at this point. The assertion error comes at the follwing line(in SetCheck function),
ASSERT(::IsWindow(m_hWnd));

Similarly if i try to access any control in my Dialog from derived class, the result will be the same.But there is no assertion, if i try to access within my dialog class.

What should i do, to access the controls in my dialog from any class, What i am missing here?

Any help is greatly appreciated.

Shiva.
Posted
Updated 7-Apr-11 18:38pm
v2
Comments
Olivier Levrey 8-Apr-11 6:09am    
You should write your comment under Eugen's answer, otherwise, he will never be notified. I did it for you.

If ASSERT(::IsWindow(m_hWnd)) failed, it means that the dialog (or the control) you are trying to access was not created yet.

It is often tricky to make a class inherit from an existing CDialog one. I would like to ask several questions:
- Is CSampleDlg a fully functionnal dialog? I mean can you display it using DoModal without problem?
- Why does CDerivedOne inherit from CSampleDlg?
--> do you use a different resource for CDerivedOne?
--> or is this class just to add some methods and members to CSampleDlg without modifying the UI?

Could you post the .h files and the .cpp files (at least the constructors, DoDataExchange, OnInitDialog, and macros like BEGIN_MESSAGE_MAP, DECLARE_DYNAMIC, ...)


----------------

I read your code from your comments and the problem is easy to fix:

void CMlkOne::ChangeVariableValue(int VarId, int VarValue)
{
    CSampleDialogLogicDlg objBaseDialog;
    objBaseDialog.ChangeCheckStatus(2); // Assertion Error bcos of this line
}


You have an asssertion here because even though you instanciated the dialog object,
the dialog resource is not created yet (it is created after DoModal is called for example).
Moreover, what you want here is not instanciating another object, but calling the base method.
So just replace your code by this one:

void CMlkOne::ChangeVariableValue(int VarId, int VarValue)
{
    //call the base class method
    CSampleDialogLogicDlg::ChangeCheckStatus(2);
}



-----------------------

Shiva, I just saw an error I hadn't seen before: you overrided OnInitDialog in CMlkOne class like this:
BOOL CMlkOne::OnInitDialog()
{
    return TRUE;
}

This can't work. All initialization is done in OnInitDialog and with your code, you just by-pass the initialization so your dialog is not created!
You must call the base method:
BOOL CMlkOne::OnInitDialog()
{
    return __super::OnInitDialog();
}

And if you want to call ChangeVariableValue in OnInitDialog, do like this:
BOOL CMlkOne::OnInitDialog()
{
    //initialize the dialog first
    __super::OnInitDialog();
    //now you can call this because all your controls are initialized
    ChangeVariableValue(2);
    return TRUE;
}


I hope it is clear this time.
 
Share this answer
 
v3
Comments
Albert Holguin 8-Apr-11 16:15pm    
see my answer and tell me what you think... i see this error commonly when people start to get too smart with MFC before they really know how it works.
Hyosoka Poipo 29-Aug-13 0:00am    
i have the same problem...
i'm using tabcontrol and have some child window....

the question is,,how do I access the child window control from other child window..?

i have read all of this conversation, but still didn't find a solution

thanks..
Shiva S.S 9-Apr-11 1:26am    
Thanks a lot Olivier, for your reply. Here I am answering your questions.
->Yes,CSampleDlg is fully functional one.I can display it, using DoModal.
->I am not using different resource for CDerivedOne,as you said it is just to add some methods.
Pls reply.
Olivier Levrey 30-Aug-13 18:57pm    
You should post your question in the Question section instead of adding a comment. If you post some code and give me the link to your question, I'll have a look at it.
Shiva S.S 11-Apr-11 6:46am    
Hi Olivier,
Thanks for your quick reply,the idea you gave me was very much helpful, i tried to replace that line, with your code. But it gives the following compile time error.

CSampleDialogLogicDlg::ChangeCheckStatus(2); //Compilation error at this line

error:'CSampleDialogLogicDlg::ChangeCheckStatus' : illegal call of non static member function.

What should i do to rectify this? Please reply.
Shiva.
Where are you calling m_gridOutput.SetCheck(Id,TRUE) from? If I had to guess what's happening, you're trying to do this at some point when the window hasn't been created. If you have a CDialog derived class, all GUI initialization should take place on the OnInitDialog() call and NOT the contructor. By the same token, when accessing these variables from a class who owns the CDialog, the window still has to be created before any of them can be accessed.

Those controls are typically private members, not allowing you to access them from a derived class or any other class other than the actual owner, this is meant to keep you from accessing those controls when the window doesn't exist, as is your case. If you REALLY know what you're doing, you can make them protected to access from derived classes or public to access from anywhere, but I'd only recommend that if you know MFC well enough.
 
Share this answer
 
Comments
Shiva S.S 9-Apr-11 1:39am    
Hi Albert,
Thanks a lot for your answer.So if i want to access any control(of CDialog)from my derived class ,before i need to create the window, here what is meant by creating the window?.

And ofcourse, i am doing all my GUI initialization in OnInitDialog(), not the constructor. Should i reinitilaize all my GUI controls in my derived class, if i want to access it from Derived one?.

Please glarify this, it will be very much helpful for me.And i already googled related to this, if you can please provide any articles related to this.

Shiva.
Albert Holguin 9-Apr-11 14:07pm    
Let me look for a good article on the subject for you... what is meant by creating the window, is actually creating the window itself, not constructing the object (i.e. calling CreateWindow() versus constructor CDialog())... as for your other question, if you've initialized controls in your base class, you don't have to reinitialize, you just have to call that method first (call CBaseDialog::OnInitDialog() as your first call within CDerivedDialog()::OnInitDialog)... your problem with this control is trivial and the cause can probably be easily spotted if you post more of your code.
Albert Holguin 10-Apr-11 15:19pm    
I can't really find a really good article on this, but MSDN provides a somewhat detailed explanation on the process of creating CDialog objects:
http://msdn.microsoft.com/en-us/library/132s802t.aspx
Shiva S.S 11-Apr-11 2:44am    
Thanks for your reply Albert. i shall post part of my code, please go through.
In Base.h file,
class CBaseDialogDlg : public CDialog
{
public:
CSampleDialogLogicDlg(CWnd* pParent = NULL); // standard constructor
virtual BOOL OnInitDialog();
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
/****Some Variable declaration********/
};
In my Base.cpp file,
CBaseDialogDlg::CBaseDialogDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBaseDialogDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBaseDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST2, m_gridCtrlEx); // List control
DDX_Control(pDX, IDC_EDIT1, t_findVar);
DDX_Control(pDX, IDC_LIST1, m_gridOutput); // List control2
DDX_Control(pDX, IDC_BUTTON1, b_startThread);
}
BOOL CBaseDialogDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Some auto code

//Inserting items into list control
m_gridOutput.SetExtendedStyle(m_gridCtrlEx.GetExtendedStyle() | LVS_EX_CHECKBOXES);

m_gridOutput.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE | LVS_LIST | LVS_EX_CHECKBOXES | DFCS_CHECKED,0,0);

for(int index = 0; index <(sizeof(Nv_OutputBits) / sizeof(int));index++)
{
m_gridOutput.InsertItem(index,Nv_OutputBits[index]);
}
ChangeCheckStatus(1); // NO PROBLEM WITH THIS CALL
}
void CBaseDialogDlg::ChangeCheckStatus(int VarId)
{
m_gridOutput.SetCheck(VarId,TRUE);
}

In my derived .h file,
class CMlkOne : public CBaseDialogDlg
{
public:
CMlkOne();
//Some code
virtual BOOL OnInitDialog();
protected:
virtual void DoDataExchange(CDataExchange* pDX);
};

In my derived cpp file,

CMlkOne::CMlkOne()
{

}
BOOL CMlkOne::OnInitDialog()
{
return TRUE;
}
void CMlkOne::DoDataExchange(CDataExchange* pDX)
{
CSampleDialogLogicDlg::DoDataExchange(pDX);
}
void CMlkOne::ChangeVariableValue(int VarId, int VarValue)
{
CSampleDialogLogicDlg objBaseDialog;
objBaseDialog.ChangeCheckStatus(2); // Assertion Error bcos of this line
}

Should i add some extra initialization code in DoDataExchange(Derived) or OnInitDialog(Derived)?, what i am missing in my derived class, i am struggling with this, Any help will be very much helpful.

Shiva.
Olivier Levrey 11-Apr-11 4:01am    
OK I got your mistake. See my updated answer. Albert, have a 5 for your good explanations.
Please post the code of the void CDerivedOne::DoDataExchange(CDataExchange* pDX) function here :)
 
Share this answer
 
Comments
Olivier Levrey 8-Apr-11 6:08am    
Op commented: Thank you so much Eugen. I cannot find, DoDataExchange in my derived class. Should i include it,by using properties window(or) it will be auto generated?. Pls reply.
Eugen Podsypalnikov 8-Apr-11 6:43am    
You could try to implement your derived function :) :
void CDerivedOne::DoDataExchange(CDataExchange* pDX)
{
CSampleDlg::DoDataExchange(pDX);
}

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