Solved

Dialog box communication

Posted on 1998-10-08
4
153 Views
Last Modified: 2013-11-20
I down loaded a piece of code coolbar from www.codeguru.com.
It can be found at codeguru in Dockable Window/DevStudio Like CControlBar 2
It comes with a downloadable project so you can see exactly what i am trying to do.

I would like to dynamically assign the tree view its branches without doing it at Initialisation.  So i cut and pasted his code so it looks like the following.  I also made a new public function Test() that should place the branches on the tree.

Now I called the tree from the CDialogControlView file using the following code:

void CDialogControlView::ViewTest(){

TestDialog dlg;
dlg.Test();

}

The CTestDialog code is as follows:



---CUT----
BOOL CTestDialog::OnInitDialog()
{
CDialog::OnInitDialog();

CRect sz;
GetClientRect(sz);
sz.DeflateRect(5,5);

m_bInit = TRUE;


return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}

void CTestDialog::OnOK()
{
// TODO: Add extra validation here

// CDialog::OnOK();
}

void CTestDialog::Test()
{
m_Edit = (CTreeCtrl*)GetDlgItem(IDC_EDIT2);

HTREEITEM i1 = m_Edit->InsertItem("Root 1");
m_Edit->InsertItem("Branch 1", i1);
m_Edit->InsertItem("Branch 2", i1);
m_Edit->InsertItem("Branch 3", i1);
HTREEITEM i2 = m_Edit->InsertItem("Root 2");
m_Edit->InsertItem("Branch 1", i2);
m_Edit->InsertItem("Branch 2", i2);
m_Edit->InsertItem("Branch 3", i2);
HTREEITEM i3 = m_Edit->InsertItem("Root 3");
m_Edit->InsertItem("Branch 1", i3);
m_Edit->InsertItem("Branch 2", i3);
m_Edit->InsertItem("Branch 3", i3);

}

Can anyone tell me what i am doing wrong? also I am having the same problem calling CDialogControlView from CTestDialog as i keep getting assertion failures:

THe code i use for calling back is

CDialogControlView* pview;
pview->GetDocument();

this produces an assertion error.

Thanks in advance mramsay.
0
Comment
Question by:marvel
  • 2
4 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1323122
You have to Create() or DoModal() your dialog before accessing its childs.
For example, m_Edit's m_hWnd member is NULL before you called Create() or DoModal().
I think that is your problem.
BTW: It would help more if you describe _what_ assertion you get :)
0
 
LVL 7

Accepted Solution

by:
psdavis earned 400 total points
ID: 1323123
void CDialogControlView::ViewTest()
{
   TestDialog dlg;
   dlg.Test();
}

OK, now you've lost all of your setup since after the 'Test' function is run, the object is deleted from the stack.  You can't do it this way!

void CTestDialog::Test()
{
m_Edit = (CTreeCtrl*)GetDlgItem(IDC_EDIT2);

HTREEITEM i1 = m_Edit->InsertItem("Root 1");
m_Edit->InsertItem("Branch 1", i1);
m_Edit->InsertItem("Branch 2", i1);
m_Edit->InsertItem("Branch 3", i1);
HTREEITEM i2 = m_Edit->InsertItem("Root 2");
m_Edit->InsertItem("Branch 1", i2);
m_Edit->InsertItem("Branch 2", i2);
m_Edit->InsertItem("Branch 3", i2);
HTREEITEM i3 = m_Edit->InsertItem("Root 3");
m_Edit->InsertItem("Branch 1", i3);
m_Edit->InsertItem("Branch 2", i3);
m_Edit->InsertItem("Branch 3", i3);
}

This won't work either.  GetDlgItem(IDC_EDIT2) doesn't exist yet.  It HAS to be done no earlier than in OnInitDialog!

Instead, it should look more like this...

void CDialogControlView::ViewTest()
{
   TestDialog dlg;
   dlg.DoModal( );
}

void CTestDialog::OnInitDialog()
{
  CDialog::OnInitDialog( );
  CTreeCtrl* pTreeEdit = (CTreeCtrl*)GetDlgItem(IDC_EDIT2);

  HTREEITEM i1 = pTreeEdit->InsertItem("Root 1");
  pTreeEdit->InsertItem("Branch 1", i1);
.
}

and it should appear correctly!

Phillip


0
 
LVL 7

Expert Comment

by:psdavis
ID: 1323124
> Can anyone tell me what i am doing wrong? also I am having the same problem calling CDialogControlView from CTestDialog as i keep getting assertion failures: The code i use for calling back is

> CDialogControlView* pview;
> pview->GetDocument();

Of course this won't work! What is your pview set to.  In this case, it's nothing!! it's null!! it's zip!!

If you want to pass a document pointer to your dialog, then do so during the initialization.  Try this.  Add a document pointer to the dialog's header file.  Something like

CDialogControlDoc* m_pDoc;

In your dialog's constructor...

m_pDoc = NULL;

and when you create your dialog...

void CDialogControlView::ViewTest()
{
   TestDialog dlg;
   dlg.m_pDoc = GetDocument( );
   dlg.Test();
}

Then you can call your functions much more effectively...

CTestDialog::DoSomething( )
{
   m_pDoc->DoSomethingInDoc( );
}

Phillip
0
 

Author Comment

by:marvel
ID: 1323125
close... but i found another solution
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now